def apply_signature(value, sig, utf8_strings=False): """Casts basic types to the right dbus types and packs them into containers with the right signature, so they get validated on sending.""" # dbus properties are variant, but have a signature defined, so # we have to convert manually here. if utf8_strings and sig == "s": return dbus.UTF8String(value) elif sig in TYPE_MAP: return TYPE_MAP[sig](value) elif sig.startswith("a{"): return dbus.Dictionary(value, signature=sig[2:-1]) elif sig.startswith("a("): return dbus.Struct(value, signature=sig[2:-1]) elif sig.startswith("a"): return dbus.Array(value, signature=sig[1:]) elif utf8_strings and sig == "s": return dbus.UTF8String(value) else: return TYPE_MAP[sig](value) # Unknown type, just return as is return value
def make_fake_attribute_with_result(result, attribute_type='wait_for', typeid=None): """Make a fake attribute with the given result. This will either return a callable, or an attribute patched with a wait_for method, according to the current test scenario. """ class FakeObject(DBusIntrospectionObject): def __init__(self, props): super(FakeObject, self).__init__( props, b"/FakeObject", backends.FakeBackend([(dbus.String('/FakeObject'), props)])) if attribute_type == 'callable': return lambda: result elif attribute_type == 'wait_for': if isinstance(result, str): obj = FakeObject(dict(id=[0, 123], attr=[0, dbus.String(result)])) return obj.attr elif isinstance(result, bytes): obj = FakeObject( dict(id=[0, 123], attr=[0, dbus.UTF8String(result)])) return obj.attr elif typeid is not None: obj = FakeObject(dict(id=[0, 123], attr=[typeid] + result)) return obj.attr else: obj = FakeObject(dict(id=[0, 123], attr=[0, dbus.Boolean(result)])) return obj.attr
def runStreetlightd(context, serviceName): #TODO make it independent of working directory context.dbus = subprocess.Popen( ['python', 'steps/streetlight.py', serviceName]) bus = dbus.SessionBus() while not (dbus.UTF8String(serviceName) in bus.list_names()): time.sleep(0.01)
def translator(c, pack): """A translator for L{Change}s""" if pack: if isinstance(c.edit, Insertion): return dbus.Struct( (dbus.Int64(c.unique_id), dbus.Int64(c.parent), dbus.Int16(0), dbus.Int64(c.edit.position), dbus.UTF8String(c.edit.text)), signature='xxnxs') elif isinstance(c.edit, Deletion): return dbus.Struct( (dbus.Int64(c.unique_id), dbus.Int64(c.parent), dbus.Int16(1), dbus.Int64(c.edit.position), dbus.Int64(c.edit.length)), signature='xxnxx') elif isinstance(c.edit, Removal): return dbus.Struct( (dbus.Int64(c.unique_id), dbus.Int64(c.parent), dbus.Int16(2), dbus.Int64(c.edit.position), dbus.Int64(c.edit.length)), signature='xxnxx') else: raise Unimplemented else: if c[2] == 0: ed = Insertion(int(c[3]), str(c[4])) elif c[2] == 1: ed = Deletion(int(c[3]), int(c[4])) elif c[2] == 2: ed = Removal(int(c[3]), int(c[4])) else: raise "unknown type" return Change(int(c[0]), int(c[1]), ed)
def startStreetlightd(context, layers): context.application = subprocess.Popen([ 'streetlightd', '--host=localhost', '--user=lamp1', '--external-timer' ] + layers) bus = dbus.SessionBus() while not (dbus.UTF8String('ch.bbv.streetlightd') in bus.list_names()): time.sleep(0.01)
def testStrings(self): self.assertEqual(utils.coerceDbusType(dbus.UTF8String("blah")), "blah") self.assertEqual(utils.coerceDbusType(dbus.ByteArray(u"blah")), "blah") self.assertEqual(utils.coerceDbusType(dbus.String(u"blah")), u"blah") self.assertEqual(utils.coerceDbusType(dbus.Signature(u"ssi")), u"ssi") self.assertEqual( utils.coerceDbusType( dbus.ObjectPath(u"/com/googlecode/debmarshal/Privops")), u"/com/googlecode/debmarshal/Privops")
def py2_dbus_utf8string(self, prop): return dbus.UTF8String(prop)
def waitForDbusService(): bus = dbus.SessionBus() while not (dbus.UTF8String('ch.bbv.brightness') in bus.list_names()): time.sleep(0.01)