1 # Written by Ingar Arntzen
2 # see LICENSE.txt for license information
5 This module implements marshaling and unmarshalling
6 between string values in and python objects, in
7 accordance with the UPnP specification.
13 class MarshalError(exceptions.Exception):
15 Error associated with marshalling and unmarshalling.
19 ##############################################
21 ##############################################
23 def loads(type_, data):
24 """Load string data and return value of given type."""
25 if type_ == types.IntType:
27 elif type_ in types.StringTypes:
29 elif type_ == types.BooleanType:
30 if data in ['1', 'true', 'True', 'yes']:
32 elif data in ['0', 'false', 'False', 'no']:
35 raise MarshalError, "Loads: Boolean failed %s" % data
37 raise MarshalError, "Loads: Unsupported Type %s" % type_
39 def loads_data_by_upnp_type(upnp_type_string, data_string):
40 """Loads string data into a python value given a string definition
41 of the UPnP data type.
43 if upnp_type_string == 'boolean':
44 if data_string in ['1', 'true', 'True', 'yes']:
46 elif data_string in ['0', 'false', 'False', 'no']:
48 elif upnp_type_string == 'int':
49 return int(data_string)
50 elif upnp_type_string == 'string':
51 return str(data_string)
52 elif upnp_type_string == 'ui1':
53 # Unsigned 1 byte integer
54 return int(data_string) & 0xFF
55 elif upnp_type_string == 'ui2':
56 # Unsigned 2 byte integer
57 return int(data_string) & 0xFFFF
58 elif upnp_type_string == 'ui4':
59 # Unsigned 1 byte integer
60 return int(data_string) & 0xFFFFFFFF
62 raise MarshalError, "Loads: Unsupported Type %s" % upnp_type_string
65 ##############################################
67 ##############################################
69 def dumps_by_upnp_type(upnp_type_string, value):
70 """Dumps python value into a string according to upnp_type_string."""
71 if isinstance(value, types.BooleanType) and upnp_type_string == "boolean":
72 return u'1' if value == True else u'0'
73 elif isinstance(value, types.StringTypes) and upnp_type_string == 'string':
74 return unicode("<![CDATA[%s]]>" % value)
75 elif isinstance(value, types.IntType) and upnp_type_string == 'ui1':
76 return unicode(value & 0xFF)
77 elif isinstance(value, types.IntType) and upnp_type_string == 'ui2':
78 return unicode(value & 0xFFFF)
79 elif isinstance(value, types.IntType) and upnp_type_string == 'ui4':
80 return unicode(value & 0xFFFFFFFF)
81 elif isinstance(value, types.IntType) and upnp_type_string == 'int':
84 msg = "Dumps: Unsupported Type %s" % str(value)
85 raise MarshalError, msg
89 """Dump typed value to unicode string"""
90 if isinstance(value, types.BooleanType):
91 return u'1' if value == True else u'0'
92 elif isinstance(value, types.StringTypes):
93 return unicode("<![CDATA[%s]]>" % value)
94 elif isinstance(value, types.IntType):
97 msg = "Dumps: Unsupported Type %s" % str(value)
98 raise MarshalError, msg
100 ##############################################
102 ##############################################
104 def dumps_data_type(python_type):
105 """Converts a python type object to a string,
106 according to UPnP specification."""
107 if python_type == types.BooleanType:
109 elif python_type == types.IntType:
111 elif python_type == types.StringType:
114 msg = "Dumps Datatype: Unsupported Type %s" % str(python_type)
115 raise MarshalError, msg
117 def loads_python_type(type_string):
118 """Converts a UPnP variable type string to a python type object."""
119 if type_string == 'boolean':
120 return types.BooleanType
121 elif type_string in ['int', 'ui1', 'ui2', 'ui4']:
123 elif type_string == u'string':
124 return types.StringType
126 msg = "Loads Datatype: Unsupported Type %s" % type_string
127 raise MarshalError, msg
132 ##############################################
134 ##############################################
136 if __name__ == '__main__':