1 # vim:set sw=4 sts=4 ft=pyrex:
2 # Types defined in _dbus_bindings and imported into dbus.types.
4 __docformat__ = 'restructuredtext'
7 """A D-Bus object path, e.g. ``/org/example/foo/FooBar``."""
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.
16 class SignatureIter(object):
17 """An iterator over complete types in a D-Bus signature."""
19 def __init__(self, string):
27 self.remaining = string
30 """Return the next complete type, e.g. ``b``, ``ab`` or ``a{sv}``."""
31 if self.remaining == '':
34 signature = self.remaining
39 for marker in range(0, end):
40 cur_sig = ord(signature[marker])
42 if cur_sig == TYPE_ARRAY:
44 elif cur_sig == DICT_ENTRY_BEGIN or cur_sig == STRUCT_BEGIN:
45 if block_type == None:
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
59 elif cur_sig == STRUCT_END:
60 if block_type == STRUCT_BEGIN:
61 block_depth = block_depth - 1
73 self.remaining = signature[end:]
74 return Signature(signature[0:end])
77 """An iterable method signature. Iterating gives the signature of each
81 """Return an iterator over complete types in the signature."""
82 return SignatureIter(self)
85 """An unsigned byte"""
86 # FIXME: this should subclass str and force length 1, judging by the rest
93 """A signed 16-bit integer"""
96 """An unsigned 16-bit integer"""
97 def __init__(self, value):
99 raise TypeError('Unsigned integers must not have a negative value')
100 int.__init__(self, value)
103 """An signed 32-bit integer"""
106 """An unsigned 32-bit integer"""
107 def __init__(self, value):
109 raise TypeError('Unsigned integers must not have a negative value')
110 long.__init__(self, value)
113 """A signed 64-bit integer"""
116 """An unsigned 64-bit integer"""
117 def __init__(self, value):
119 raise TypeError('Unsigned integers must not have a negative value')
120 long.__init__(self, value)
123 """A double-precision floating point number"""
125 class String(unicode):
126 """A Unicode string"""
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')
135 self.signature = signature
136 list.__init__(self, value)
139 """A generic wrapper for values of any other basic D-Bus type"""
140 def __init__(self, value, type=None, signature=None):
142 if signature and type:
143 raise TypeError('Can not mix type and signature arguments in a D-BUS Variant')
146 self.signature = signature
149 return repr(self.value)
152 return str(self.value)
155 """An immutable structure containing D-Bus values, possibly of
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).
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')
171 self.key_type = key_type
172 self.value_type = value_type
173 self.signature = signature
174 dict.__init__(self, value)