1
# GNU Enterprise RPC interface - Py-XMLRPC type conversion rules
3
# Copyright 2001-2005 Free Software Foundation
5
# This file is part of GNU Enterprise.
7
# GNU Enterprise is free software; you can redistribute it
8
# and/or modify it under the terms of the GNU General Public
9
# License as published by the Free Software Foundation; either
10
# version 2, or (at your option) any later version.
12
# GNU Enterprise is distributed in the hope that it will be
13
# useful, but WITHOUT ANY WARRANTY; without even the implied
14
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
# PURPOSE. See the GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public
18
# License along with program; see the file COPYING. If not,
19
# write to the Free Software Foundation, Inc., 59 Temple Place
20
# - Suite 330, Boston, MA 02111-1307, USA.
22
# $Id: typeconv.py 7017 2005-02-12 22:45:04Z reinhard $
28
# -----------------------------------------------------------------------------
29
# Convert native Python type to xmlrpc's type
30
# -----------------------------------------------------------------------------
32
def python_to_rpc (value, exception):
33
print "converting value %s of type %s " % (value,type(value))
39
elif isinstance (value, types.StringType):
41
elif isinstance (value, types.UnicodeType):
42
return value.encode ('utf-8')
44
# Boolean (has to be checked before Integer, as it inherites int)
45
elif hasattr (types, 'BooleanType') and \
46
isinstance (value, types.BooleanType):
48
return xmlrpc.boolean (True)
50
return xmlrpc.boolean (False)
53
elif isinstance (value, types.IntType):
55
elif isinstance (value, types.FloatType):
59
elif isinstance (value, mx.DateTime.DateTimeType):
60
# mx.DateTime uses date 1/1/1 for time-only values. py_xmlrpc doesn't like
65
if (y, m, d) == (1, 1, 1): (y, m, d) = (9999, 1, 1)
67
return xmlrpc.dateTime (y, m, d, value.hour, value.minute,
71
elif isinstance (value, types.ListType):
72
return [python_to_rpc (element, exception) for element in value]
75
elif isinstance (value, types.TupleType):
78
result += (python_to_rpc (element, exception), )
82
elif isinstance (value, types.DictionaryType):
84
for (key, val) in value.items ():
85
result [python_to_rpc (key, exception)] = python_to_rpc (val, exception)
89
raise exception, repr (value)
91
# -----------------------------------------------------------------------------
92
# Convert xmlrpc's type to native Python type
93
# -----------------------------------------------------------------------------
95
def rpc_to_python (value, exception):
98
if isinstance (value, types.StringType):
100
return unicode (value, 'utf-8')
105
elif isinstance (value, types.IntType):
107
elif isinstance (value, types.FloatType):
111
elif type (value) == type (xmlrpc.dateTime (1999, 12, 31, 23, 59, 59)):
112
(year, month, day, hour, minute, second) = value.date ()
113
# 1/1/1 (i.e. time without date) is transported as 1/1/9999
114
if (year, month, day) == (9999, 1, 1): (year, month, day) = (1, 1, 1)
115
return mx.DateTime.DateTime (year, month, day, hour, minute, second)
118
elif type (value) == type (xmlrpc.boolean (0)):
125
elif isinstance (value, types.ListType):
126
return [rpc_to_python (element, exception) for element in value]
129
elif isinstance (value, types.TupleType):
131
for element in value:
132
result += (rpc_to_python (element, exception), )
136
elif isinstance (value, types.DictionaryType):
138
for (key, val) in value.items ():
139
result [rpc_to_python (key, exception)] = rpc_to_python (val, exception)
143
raise exception, repr (value)