def __setitem__(self, key, value): # set_value() aborts the program on an unknown key if key not in self: raise KeyError('unknown key: %r' % (key,)) # determine type string of this key range = self.get_range(key) type_ = range.get_child_value(0).get_string() v = range.get_child_value(1) if type_ == 'type': # v is boxed empty array, type of its elements is the allowed value type type_str = v.get_child_value(0).get_type_string() assert type_str.startswith('a') type_str = type_str[1:] elif type_ == 'enum': # v is an array with the allowed values assert v.get_child_value(0).get_type_string().startswith('a') type_str = v.get_child_value(0).get_child_value(0).get_type_string() allowed = v.unpack() if value not in allowed: raise ValueError('value %s is not an allowed enum (%s)' % (value, allowed)) else: raise NotImplementedError('Cannot handle allowed type range class ' + str(type_)) self.set_value(key, GLib.Variant(type_str, value))
def __call__(self, *args, **kwargs): # the first positional argument is the signature, unless we are calling # a method without arguments; then signature is implied to be '()'. if args: signature = args[0] args = args[1:] if not isinstance(signature, str): raise TypeError('first argument must be the method signature string: %r' % signature) else: signature = '()' arg_variant = GLib.Variant(signature, tuple(args)) if 'result_handler' in kwargs: # asynchronous call user_data = (kwargs['result_handler'], kwargs.get('error_handler'), kwargs.get('user_data')) self.dbus_proxy.call(self.method_name, arg_variant, kwargs.get('flags', 0), kwargs.get('timeout', -1), None, self.__async_result_handler, user_data) else: # synchronous call result = self.dbus_proxy.call_sync(self.method_name, arg_variant, kwargs.get('flags', 0), kwargs.get('timeout', -1), None) return self._unpack_result(result)
def set_attribute(self, attributes): for (name, format_string, value) in attributes: self.set_attribute_value(name, GLib.Variant(format_string, value))