我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用dbus.String()。
def unwrap(self, val): if isinstance(val, dbus.ByteArray): return "".join([str(x) for x in val]) if isinstance(val, (dbus.Array, list, tuple)): return [self.unwrap(x) for x in val] if isinstance(val, (dbus.Dictionary, dict)): return dict([(self.unwrap(x), self.unwrap(y)) for x, y in val.items()]) if isinstance(val, dbus.ObjectPath): if val.startswith('/org/freedesktop/NetworkManager/'): classname = val.split('/')[4] classname = { 'Settings': 'Connection', 'Devices': 'Device', }.get(classname, classname) return globals()[classname](val) if isinstance(val, (dbus.Signature, dbus.String)): return unicode(val) if isinstance(val, dbus.Boolean): return bool(val) if isinstance(val, (dbus.Int16, dbus.UInt16, dbus.Int32, dbus.UInt32, dbus.Int64, dbus.UInt64)): return int(val) if isinstance(val, dbus.Byte): return bytes([int(val)]) return val
def dict_to_string(self, d): # Try to trivially translate a dictionary's elements into nice string # formatting. dstr="" for key in d: val=d[key] str_val="" add_string=True if type(val)==type(dbus.Array([])): for elt in val: if type(elt)==type(dbus.Byte(1)): str_val+="%s " % int(elt) elif type(elt)==type(dbus.String("")): str_val+="%s" % elt elif type(val)==type(dbus.Dictionary({})): dstr+=self.dict_to_string(val) add_string=False else: str_val=val if add_string: dstr+="%s: %s\n" % ( key, str_val) return dstr
def result(operation_id, code, text): """Encode a result This method encodes operation id, result code and result text into a dictionary of dbus types. @param operation_id Id of the operation for which the result is reported @param code Result code of the operation (one of SWMResult) @param text Text message @return Result dictionary using dbus types. """ if not SWMResult.isValid(code): code = SWMResult.SWM_GENERAL_ERROR return { 'id': dbus.String(operation_id, variant_level=1), 'result_code': dbus.Int32(code, variant_level=1), 'result_text': dbus.String(text, variant_level=1) }
def _return_credentials(self): c = dbus.Dictionary( { dbus.String(u'consumer_secret'): dbus.String( self._random_unicode_string(30)), dbus.String(u'token'): dbus.String( self._random_unicode_string(50)), dbus.String(u'consumer_key'): dbus.String( self._random_unicode_string(7)), dbus.String(u'name'): dbus.String('Ubuntu Software Center @ ' + self._random_unicode_string(6)), dbus.String(u'token_secret'): dbus.String( self._random_unicode_string(50)) }, signature=dbus.Signature('ss') ) return c
def convert(dbus_obj): """Converts dbus_obj from dbus type to python type. :param dbus_obj: dbus object. :returns: dbus_obj in python type. """ _isinstance = partial(isinstance, dbus_obj) ConvertType = namedtuple('ConvertType', 'pytype dbustypes') pyint = ConvertType(int, (dbus.Byte, dbus.Int16, dbus.Int32, dbus.Int64, dbus.UInt16, dbus.UInt32, dbus.UInt64)) pybool = ConvertType(bool, (dbus.Boolean, )) pyfloat = ConvertType(float, (dbus.Double, )) pylist = ConvertType(lambda _obj: list(map(convert, dbus_obj)), (dbus.Array, )) pytuple = ConvertType(lambda _obj: tuple(map(convert, dbus_obj)), (dbus.Struct, )) types_str = (dbus.ObjectPath, dbus.Signature, dbus.String) pystr = ConvertType(str, types_str) pydict = ConvertType( lambda _obj: dict(list(zip(list(map(convert, dbus_obj.keys())), list(map(convert, dbus_obj.values())) )) ), (dbus.Dictionary, ) ) for conv in (pyint, pybool, pyfloat, pylist, pytuple, pystr, pydict): if any(map(_isinstance, conv.dbustypes)): return conv.pytype(dbus_obj) else: return dbus_obj
def convert(dbus_obj): """Converts dbus_obj from dbus type to python type. :param dbus_obj: dbus object. :returns: dbus_obj in python type. """ _isinstance = partial(isinstance, dbus_obj) ConvertType = namedtuple('ConvertType', 'pytype dbustypes') pyint = ConvertType(int, (dbus.Byte, dbus.Int16, dbus.Int32, dbus.Int64, dbus.UInt16, dbus.UInt32, dbus.UInt64)) pybool = ConvertType(bool, (dbus.Boolean, )) pyfloat = ConvertType(float, (dbus.Double, )) pylist = ConvertType(lambda _obj: list(map(convert, dbus_obj)), (dbus.Array, )) pytuple = ConvertType(lambda _obj: tuple(map(convert, dbus_obj)), (dbus.Struct, )) types_str = (dbus.ObjectPath, dbus.Signature, dbus.String) pystr = ConvertType(str, types_str) pydict = ConvertType( lambda _obj: dict(zip(map(convert, dbus_obj.keys()), map(convert, dbus_obj.values()) ) ), (dbus.Dictionary, ) ) for conv in (pyint, pybool, pyfloat, pylist, pytuple, pystr, pydict): if any(map(_isinstance, conv.dbustypes)): return conv.pytype(dbus_obj) else: return dbus_obj
def base_to_python(val): if isinstance(val, dbus.ByteArray): return "".join([str(x) for x in val]) if isinstance(val, (dbus.Array, list, tuple)): return [fixups.base_to_python(x) for x in val] if isinstance(val, (dbus.Dictionary, dict)): return dict([(fixups.base_to_python(x), fixups.base_to_python(y)) for x, y in val.items()]) if isinstance(val, dbus.ObjectPath): for obj in (NetworkManager, Settings, AgentManager): if val == obj.object_path: return obj if val.startswith('/org/freedesktop/NetworkManager/'): classname = val.split('/')[4] classname = { 'Settings': 'Connection', 'Devices': 'Device', }.get(classname, classname) try: return globals()[classname](val) except ObjectVanished: return None if val == '/': return None if isinstance(val, (dbus.Signature, dbus.String)): return six.text_type(val) if isinstance(val, dbus.Boolean): return bool(val) if isinstance(val, (dbus.Int16, dbus.UInt16, dbus.Int32, dbus.UInt32, dbus.Int64, dbus.UInt64)): return int(val) if isinstance(val, dbus.Byte): return six.int2byte(int(val)) return val
def test_dbus_wrap_string(self): value = 'text' dbus_value = dbus_mqtt.wrap_dbus_value(value) self.assertIsInstance(dbus_value, dbus.String) self.assertEqual(dbus.String(value, variant_level=1), dbus_value)
def test_dbus_wrap_dict(self): value = {'a' : 3, 'b': 7.0} dbus_value = dbus_mqtt.wrap_dbus_value(value) print(dbus_value) self.assertIsInstance(dbus_value, dbus.Dictionary) self.assertEqual(dbus.Dictionary({ dbus.String('a', variant_level=1): dbus.Int32(3, variant_level=1), dbus.String('b', variant_level=1): dbus.Double(7.0, variant_level=1)}, variant_level=1), dbus_value)
def test_dbus_unwrap_string(self): dbus_value = dbus.String('abcd', variant_level=1) value = dbus_mqtt.unwrap_dbus_value(dbus_value) self.assertIsInstance(value, unicode) self.assertEqual(str(dbus_value), value)
def test_dbus_unwrap_dict(self): dbus_value = dbus.Dictionary({ dbus.String('a', variant_level=1): dbus.Double(3.2, variant_level=1), dbus.String('b', variant_level=1): dbus.Double(3.7, variant_level=1)}, variant_level=1) value = dbus_mqtt.unwrap_dbus_value(dbus_value) self.assertIsInstance(value, dict) self.assertEqual({'a':3.2, 'b':3.7}, value)
def get_debug_level(self): return self.__get_property(dbus.String("DebugLevel"))
def set_debug_level(self, parameter): self.__set_property(dbus.String("DebugLevel"), dbus.String(parameter))
def get_debug_timestamp(self): return self.__get_property(dbus.String("DebugTimestamp"))
def set_debug_level(self, parameter): self.__set_property(dbus.String("DebugTimestamp"), dbus.Boolean(parameter))
def get_debug_show_keys(self): return self.__get_property(dbus.String("DebugShowKeys"))
def get_interfaces(self): return self.__get_property(dbus.String("Interfaces"))
def get_EAP_methods(self): return self.__get_property(dbus.String("EapMethods"))
def get_capabilities(self): return self.__get_property(dbus.String("Capabilities"))
def get_WFDIEs(self): return self.__get_property(dbus.String("WFDIEs"))
def set_WFDIEs(self, parameter): self.__set_property(dbus.String("WFDIEs"), dbus.Array(parameter, "y"))