instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / UPnP / common / upnpmarshal.py
1 # Written by Ingar Arntzen
2 # see LICENSE.txt for license information
3
4 """
5 This module implements marshaling and unmarshalling 
6 between string values in and python objects, in
7 accordance with the UPnP specification.
8 """
9
10 import types
11 import exceptions
12
13 class MarshalError(exceptions.Exception): 
14     """
15     Error associated with marshalling and unmarshalling.
16     """
17     pass
18
19 ##############################################
20 # LOADS
21 ##############################################
22
23 def loads(type_, data):
24     """Load string data and return value of given type."""
25     if type_ == types.IntType:
26         return int(data)
27     elif type_ in types.StringTypes:
28         return str(data)
29     elif type_ == types.BooleanType:
30         if data in ['1', 'true', 'True', 'yes']: 
31             return True
32         elif data in ['0', 'false', 'False', 'no']: 
33             return False
34         else : 
35             raise MarshalError, "Loads: Boolean failed %s" % data
36     else: 
37         raise MarshalError, "Loads: Unsupported Type %s" % type_
38
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.
42     """
43     if upnp_type_string == 'boolean':
44         if data_string in ['1', 'true', 'True', 'yes']: 
45             return True
46         elif data_string in ['0', 'false', 'False', 'no']: 
47             return False
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
61     else: 
62         raise MarshalError, "Loads: Unsupported Type %s" % upnp_type_string
63
64
65 ##############################################
66 # DUMPS
67 ##############################################
68
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':
82         return unicode(value)
83     else: 
84         msg = "Dumps: Unsupported Type %s" % str(value)
85         raise MarshalError, msg
86     
87
88 def dumps(value):
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):
95         return unicode(value)
96     else: 
97         msg = "Dumps: Unsupported Type %s" % str(value)
98         raise MarshalError, msg
99
100 ##############################################
101 # DATATYPES
102 ##############################################
103
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:
108         return u'boolean'
109     elif python_type == types.IntType:
110         return u'int'
111     elif python_type == types.StringType:
112         return u'string'
113     else: 
114         msg = "Dumps Datatype: Unsupported Type %s" % str(python_type)
115         raise MarshalError, msg
116
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']:
122         return types.IntType
123     elif type_string == u'string':
124         return types.StringType
125     else: 
126         msg = "Loads Datatype: Unsupported Type %s" % type_string
127         raise MarshalError, msg
128
129
130     
131
132 ##############################################
133 # MAIN
134 ##############################################
135
136 if __name__ == '__main__':
137     pass
138     
139