1021
1021
variant_level=variant_level)
1024
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
1026
"""Applied to an empty subclass of a D-Bus object, this metaclass
1027
will add additional D-Bus attributes matching a certain pattern.
1024
def alternate_dbus_interfaces(alt_interface_names, deprecate=True):
1025
"""A class decorator; applied to a subclass of
1026
dbus.service.Object, it will add alternate D-Bus attributes with
1027
interface names according to the "alt_interface_names" mapping.
1030
@alternate_dbus_names({"org.example.Interface":
1031
"net.example.AlternateInterface"})
1032
class SampleDBusObject(dbus.service.Object):
1033
@dbus.service.method("org.example.Interface")
1034
def SampleDBusMethod():
1037
The above "SampleDBusMethod" on "SampleDBusObject" will be
1038
reachable via two interfaces: "org.example.Interface" and
1039
"net.example.AlternateInterface", the latter of which will have
1040
its D-Bus annotation "org.freedesktop.DBus.Deprecated" set to
1041
"true", unless "deprecate" is passed with a False value.
1043
This works for methods and signals, and also for D-Bus properties
1044
(from DBusObjectWithProperties) and interfaces (from the
1045
dbus_interface_annotations decorator).
1029
def __new__(mcs, name, bases, attr):
1030
# Go through all the base classes which could have D-Bus
1031
# methods, signals, or properties in them
1032
old_interface_names = []
1033
for base in (b for b in bases
1034
if issubclass(b, dbus.service.Object)):
1035
# Go though all attributes of the base class
1036
for attrname, attribute in inspect.getmembers(base):
1048
for orig_interface_name, alt_interface_name in (
1049
alt_interface_names.iteritems()):
1051
interface_names = set()
1052
# Go though all attributes of the class
1053
for attrname, attribute in inspect.getmembers(cls):
1037
1054
# Ignore non-D-Bus attributes, and D-Bus attributes
1038
1055
# with the wrong interface name
1039
1056
if (not hasattr(attribute, "_dbus_interface")
1040
1057
or not attribute._dbus_interface
1041
.startswith("se.recompile.Mandos")):
1058
.startswith(orig_interface_name)):
1043
1060
# Create an alternate D-Bus interface name based on
1044
1061
# the current name
1045
1062
alt_interface = (attribute._dbus_interface
1046
.replace("se.recompile.Mandos",
1047
"se.bsnet.fukt.Mandos"))
1048
if alt_interface != attribute._dbus_interface:
1049
old_interface_names.append(alt_interface)
1063
.replace(orig_interface_name,
1064
alt_interface_name))
1065
interface_names.add(alt_interface)
1050
1066
# Is this a D-Bus signal?
1051
1067
if getattr(attribute, "_dbus_is_signal", False):
1052
1068
# Extract the original non-method function by
1074
1090
except AttributeError:
1076
1092
# Define a creator of a function to call both the
1077
# old and new functions, so both the old and new
1078
# signals gets sent when the function is called
1093
# original and alternate functions, so both the
1094
# original and alternate signals gets sent when
1095
# the function is called
1079
1096
def fixscope(func1, func2):
1080
1097
"""This function is a scope container to pass
1081
1098
func1 and func2 to the "call_both" function
1088
1105
return call_both
1089
1106
# Create the "call_both" function and add it to
1091
attr[attrname] = fixscope(attribute,
1108
attr[attrname] = fixscope(attribute, new_function)
1093
1109
# Is this a D-Bus method?
1094
1110
elif getattr(attribute, "_dbus_is_method", False):
1095
1111
# Create a new, but exactly alike, function
1151
1167
attribute.func_name,
1152
1168
attribute.func_defaults,
1153
1169
attribute.func_closure)))
1154
# Deprecate all old interfaces
1155
iname="_AlternateDBusNamesMetaclass_interface_annotation{0}"
1156
for old_interface_name in old_interface_names:
1157
@dbus_interface_annotations(old_interface_name)
1159
return { "org.freedesktop.DBus.Deprecated": "true" }
1160
# Find an unused name
1161
for aname in (iname.format(i) for i in itertools.count()):
1162
if aname not in attr:
1165
return type.__new__(mcs, name, bases, attr)
1171
# Deprecate all alternate interfaces
1172
iname="_AlternateDBusNames_interface_annotation{0}"
1173
for interface_name in interface_names:
1174
@dbus_interface_annotations(interface_name)
1176
return { "org.freedesktop.DBus.Deprecated":
1178
# Find an unused name
1179
for aname in (iname.format(i)
1180
for i in itertools.count()):
1181
if aname not in attr:
1185
# Replace the class with a new subclass of it with
1186
# methods, signals, etc. as created above.
1187
cls = type(b"{0}Alternate".format(cls.__name__),
1193
@alternate_dbus_interfaces({"se.recompile.Mandos":
1194
"se.bsnet.fukt.Mandos"})
1168
1195
class ClientDBus(Client, DBusObjectWithProperties):
1169
1196
"""A Client class using D-Bus