~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/rpc/drivers/xmlrpc/py_xmlrpc/typeconv.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# GNU Enterprise RPC interface - Py-XMLRPC type conversion rules
 
2
#
 
3
# Copyright 2001-2005 Free Software Foundation
 
4
#
 
5
# This file is part of GNU Enterprise.
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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.
 
21
#
 
22
# $Id: typeconv.py 7017 2005-02-12 22:45:04Z reinhard $
 
23
 
 
24
import types
 
25
import mx.DateTime
 
26
import xmlrpc
 
27
 
 
28
# -----------------------------------------------------------------------------
 
29
# Convert native Python type to xmlrpc's type
 
30
# -----------------------------------------------------------------------------
 
31
 
 
32
def python_to_rpc (value, exception):
 
33
  print "converting value %s of type %s " % (value,type(value))
 
34
  # None
 
35
  if value is None:
 
36
    return ''
 
37
 
 
38
  # String
 
39
  elif isinstance (value, types.StringType):
 
40
    return value
 
41
  elif isinstance (value, types.UnicodeType):
 
42
    return value.encode ('utf-8')
 
43
 
 
44
  # Boolean (has to be checked before Integer, as it inherites int)
 
45
  elif hasattr (types, 'BooleanType') and \
 
46
       isinstance (value, types.BooleanType):
 
47
    if value:
 
48
      return xmlrpc.boolean (True)
 
49
    else:
 
50
      return xmlrpc.boolean (False)
 
51
 
 
52
  # Number
 
53
  elif isinstance (value, types.IntType):
 
54
    return value
 
55
  elif isinstance (value, types.FloatType):
 
56
    return value
 
57
 
 
58
  # Date/Time
 
59
  elif isinstance (value, mx.DateTime.DateTimeType):
 
60
    # mx.DateTime uses date 1/1/1 for time-only values. py_xmlrpc doesn't like
 
61
    # that.
 
62
    y = value.year
 
63
    m = value.month
 
64
    d = value.day
 
65
    if (y, m, d) == (1, 1, 1): (y, m, d) = (9999, 1, 1)
 
66
 
 
67
    return xmlrpc.dateTime (y, m, d, value.hour, value.minute,
 
68
                            int (value.second))
 
69
 
 
70
  # List
 
71
  elif isinstance (value, types.ListType):
 
72
    return [python_to_rpc (element, exception) for element in value]
 
73
 
 
74
  # Tuple
 
75
  elif isinstance (value, types.TupleType):
 
76
    result = ()
 
77
    for element in value:
 
78
      result += (python_to_rpc (element, exception), )
 
79
    return result
 
80
 
 
81
  # Dictionary
 
82
  elif isinstance (value, types.DictionaryType):
 
83
    result = {}
 
84
    for (key, val) in value.items ():
 
85
      result [python_to_rpc (key, exception)] = python_to_rpc (val, exception)
 
86
    return result
 
87
 
 
88
  else:
 
89
    raise exception, repr (value)
 
90
 
 
91
# -----------------------------------------------------------------------------
 
92
# Convert xmlrpc's type to native Python type
 
93
# -----------------------------------------------------------------------------
 
94
 
 
95
def rpc_to_python (value, exception):
 
96
 
 
97
  # None or String
 
98
  if isinstance (value, types.StringType):
 
99
    if value:
 
100
      return unicode (value, 'utf-8')
 
101
    else:
 
102
      return None
 
103
 
 
104
  # Number
 
105
  elif isinstance (value, types.IntType):
 
106
    return value
 
107
  elif isinstance (value, types.FloatType):
 
108
    return value
 
109
 
 
110
  # Date/Time
 
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)
 
116
 
 
117
  # Boolean
 
118
  elif type (value) == type (xmlrpc.boolean (0)):
 
119
    if value:
 
120
      return True
 
121
    else:
 
122
      return False
 
123
 
 
124
  # List
 
125
  elif isinstance (value, types.ListType):
 
126
    return [rpc_to_python (element, exception) for element in value]
 
127
 
 
128
  # Tuple
 
129
  elif isinstance (value, types.TupleType):
 
130
    result = ()
 
131
    for element in value:
 
132
      result += (rpc_to_python (element, exception), )
 
133
    return result
 
134
 
 
135
  # Dictionary
 
136
  elif isinstance (value, types.DictionaryType):
 
137
    result = {}
 
138
    for (key, val) in value.items ():
 
139
      result [rpc_to_python (key, exception)] = rpc_to_python (val, exception)
 
140
    return result
 
141
 
 
142
  else:
 
143
    raise exception, repr (value)