dbus/service.py, dbus/_dbus_bindings-types.pxi: Move VariantSignature
[dbus-python-phuang.git] / dbus / _dbus_bindings-types.pxi
blobbbc77f10ae694844f273a2500281421e2d96439a
1 # vim:set sw=4 sts=4 ft=pyrex:
2 # Types defined in _dbus_bindings and imported into dbus.types.
4 __docformat__ = 'restructuredtext'
6 class ObjectPath(str):
7     """A D-Bus object path, e.g. ``/org/example/foo/FooBar``."""
9 class ByteArray(str):
10     """A byte array represented as an 8-bit string.
12     Used to avoid having to construct a list of integers when passing
13     byte-array (``ay``) parameters to D-Bus methods.
14     """
16 class SignatureIter(object):
17     """An iterator over complete types in a D-Bus signature."""
19     def __init__(self, string):
20         """Constructor.
22         :Parameters:
23              `string` : str
24                 A D-Bus signature
25         """
26         object.__init__(self)
27         self.remaining = string
29     def next(self):
30         """Return the next complete type, e.g. ``b``, ``ab`` or ``a{sv}``."""
31         if self.remaining == '':
32             raise StopIteration
34         signature = self.remaining
35         block_depth = 0
36         block_type = None
37         end = len(signature)
39         for marker in range(0, end):
40             cur_sig = ord(signature[marker])
42             if cur_sig == TYPE_ARRAY:
43                 pass
44             elif cur_sig == DICT_ENTRY_BEGIN or cur_sig == STRUCT_BEGIN:
45                 if block_type == None:
46                     block_type = cur_sig
48                 if block_type == cur_sig:
49                     block_depth = block_depth + 1
51             elif cur_sig == DICT_ENTRY_END:
52                 if block_type == DICT_ENTRY_BEGIN:
53                     block_depth = block_depth - 1
55                 if block_depth == 0:
56                     end = marker
57                     break
59             elif cur_sig == STRUCT_END:
60                 if block_type == STRUCT_BEGIN:
61                     block_depth = block_depth - 1
63                 if block_depth == 0:
64                     end = marker
65                     break
67             else:
68                 if block_depth == 0:
69                     end = marker
70                     break
72         end = end + 1
73         self.remaining = signature[end:]
74         return Signature(signature[0:end])
76 class Signature(str):
77     """An iterable method signature. Iterating gives the signature of each
78     argument in turn."""
80     def __iter__(self):
81         """Return an iterator over complete types in the signature."""
82         return SignatureIter(self)
84 class Byte(int):
85     """An unsigned byte"""
86     # FIXME: this should subclass str and force length 1, judging by the rest
87     # of the API!
89 class Boolean(int):
90     """A Boolean value"""
92 class Int16(int):
93     """A signed 16-bit integer"""
95 class UInt16(int):
96     """An unsigned 16-bit integer"""
97     def __init__(self, value):
98         if value < 0:
99             raise TypeError('Unsigned integers must not have a negative value')
100         int.__init__(self, value)
102 class Int32(int):
103     """An signed 32-bit integer"""
105 class UInt32(long):
106     """An unsigned 32-bit integer"""
107     def __init__(self, value):
108         if value < 0:
109             raise TypeError('Unsigned integers must not have a negative value')
110         long.__init__(self, value)
112 class Int64(long):
113     """A signed 64-bit integer"""
115 class UInt64(long):
116     """An unsigned 64-bit integer"""
117     def __init__(self, value):
118         if value < 0:
119             raise TypeError('Unsigned integers must not have a negative value')
120         long.__init__(self, value)
122 class Double(float):
123     """A double-precision floating point number"""
125 class String(unicode):
126     """A Unicode string"""
128 class Array(list):
129     """An array of values of the same type"""
130     def __init__(self, value, type=None, signature=None):
131         if signature and type:
132             raise TypeError('Can not mix type and signature arguments in a D-BUS Array')
133     
134         self.type = type
135         self.signature = signature
136         list.__init__(self, value)
138 class Variant:
139     """A generic wrapper for values of any other basic D-Bus type"""
140     def __init__(self, value, type=None, signature=None):
141         self.value = value
142         if signature and type:
143             raise TypeError('Can not mix type and signature arguments in a D-BUS Variant')
145         self.type = type
146         self.signature = signature
148     def __repr__(self):
149         return repr(self.value)
151     def __str__(self):
152         return str(self.value)
154 class Struct(tuple):
155     """An immutable structure containing D-Bus values, possibly of
156     different types.
157     """
159 class Dictionary(dict):
160     """A mapping from distinct keys (all of the same type) to values (all
161     of the same type, which need not be the same type as the values).
162     """
163     def __init__(self, value, key_type=None, value_type=None, signature=None):
164         if key_type and not value_type:
165              raise TypeError('When specifying a key_type you must also have a value_type in a D-BUS Dictionary')
166         elif value_type and not key_type:
167              raise TypeError('When specifying a value_type you must also have a key_type in a D-BUS Dictionary')
168         elif key_type and signature:
169               raise TypeError('Can not mix type arguments with signature arguments in a D-BUS Dictionary')
170               
171         self.key_type = key_type
172         self.value_type = value_type
173         self.signature = signature
174         dict.__init__(self, value)