1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libxml2dom/rpc.py Sat Oct 06 19:33:22 2007 +0000
1.3 @@ -0,0 +1,102 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Conversion functions and data used by XML-RPC and SOAP.
1.8 +
1.9 +Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU Lesser General Public License as published by the Free
1.13 +Software Foundation; either version 3 of the License, or (at your option) any
1.14 +later version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU Lesser General Public License along
1.22 +with this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +import datetime
1.26 +
1.27 +# Utility classes.
1.28 +
1.29 +class ParameterName(object):
1.30 +
1.31 + "A method parameter name."
1.32 +
1.33 + def __init__(self, ns, name):
1.34 + self.ns = ns
1.35 + self.name = name
1.36 +
1.37 + def __eq__(self, other):
1.38 + other_ns, other_name = other
1.39 + return self.ns, to_localName(self.name) == other_ns, to_localName(other_name)
1.40 +
1.41 + def __hash__(self):
1.42 + return hash(self.ns + to_localName(self.name))
1.43 +
1.44 + def __repr__(self):
1.45 + return "ParameterName(%s, %s)" % (repr(self.ns), repr(self.name or None))
1.46 +
1.47 + # Sequence emulation.
1.48 +
1.49 + def __len__(self):
1.50 + return 2
1.51 +
1.52 + def __getitem__(self, i):
1.53 + return (self.ns, self.name)[i]
1.54 +
1.55 +def to_localName(name):
1.56 + return (name or "").split(":")[-1] or None
1.57 +
1.58 +def convert(parameters, converters=None):
1.59 +
1.60 + """
1.61 + Convert the 'parameters', returning a list of name, value items where the
1.62 + names are the plain names for each parameter, and the values may be
1.63 + converted from strings to other data types.
1.64 + """
1.65 +
1.66 + conv = default_converters
1.67 + conv.update(converters or {})
1.68 + results = []
1.69 + for parameter_name, parameter_value in parameters:
1.70 + typename, name = parameter_name
1.71 + localName = to_localName(name)
1.72 + if isinstance(parameter_value, list):
1.73 + value = convert(parameter_value, converters)
1.74 + else:
1.75 + functions = conv.get(typename, {})
1.76 + function = functions.get(localName) or functions.get(None, unicode)
1.77 + value = function(parameter_value)
1.78 + results.append((localName, value))
1.79 + return results
1.80 +
1.81 +# Utility functions.
1.82 +
1.83 +def boolean(s):
1.84 + if s.lower() == "true":
1.85 + return True
1.86 + elif s.lower() == "false":
1.87 + return False
1.88 + else:
1.89 + raise ValueError, "String value %s not convertable to boolean." % repr(s)
1.90 +
1.91 +def iso8601(s):
1.92 + # NOTE: To be written.
1.93 + return s
1.94 +
1.95 +default_converters = {
1.96 + "string" : {None : unicode},
1.97 + "int" : {None : int},
1.98 + "i4" : {None : int},
1.99 + "double" : {None : float},
1.100 + "boolean" : {None : boolean},
1.101 + "dateTime.iso8601" : {None : iso8601},
1.102 + "base64" : {None : str}
1.103 + }
1.104 +
1.105 +# vim: tabstop=4 expandtab shiftwidth=4