1018
1021
variant_level=variant_level)
1021
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
1023
"""Applied to an empty subclass of a D-Bus object, this metaclass
1024
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).
1026
def __new__(mcs, name, bases, attr):
1027
# Go through all the base classes which could have D-Bus
1028
# methods, signals, or properties in them
1029
old_interface_names = []
1030
for base in (b for b in bases
1031
if issubclass(b, dbus.service.Object)):
1032
# Go though all attributes of the base class
1033
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):
1034
1054
# Ignore non-D-Bus attributes, and D-Bus attributes
1035
1055
# with the wrong interface name
1036
1056
if (not hasattr(attribute, "_dbus_interface")
1037
1057
or not attribute._dbus_interface
1038
.startswith("se.recompile.Mandos")):
1058
.startswith(orig_interface_name)):
1040
1060
# Create an alternate D-Bus interface name based on
1041
1061
# the current name
1042
1062
alt_interface = (attribute._dbus_interface
1043
.replace("se.recompile.Mandos",
1044
"se.bsnet.fukt.Mandos"))
1045
if alt_interface != attribute._dbus_interface:
1046
old_interface_names.append(alt_interface)
1063
.replace(orig_interface_name,
1064
alt_interface_name))
1065
interface_names.add(alt_interface)
1047
1066
# Is this a D-Bus signal?
1048
1067
if getattr(attribute, "_dbus_is_signal", False):
1049
1068
# Extract the original non-method function by
1071
1090
except AttributeError:
1073
1092
# Define a creator of a function to call both the
1074
# old and new functions, so both the old and new
1075
# 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
1076
1096
def fixscope(func1, func2):
1077
1097
"""This function is a scope container to pass
1078
1098
func1 and func2 to the "call_both" function
1085
1105
return call_both
1086
1106
# Create the "call_both" function and add it to
1088
attr[attrname] = fixscope(attribute,
1108
attr[attrname] = fixscope(attribute, new_function)
1090
1109
# Is this a D-Bus method?
1091
1110
elif getattr(attribute, "_dbus_is_method", False):
1092
1111
# Create a new, but exactly alike, function
1148
1167
attribute.func_name,
1149
1168
attribute.func_defaults,
1150
1169
attribute.func_closure)))
1151
# Deprecate all old interfaces
1152
basename="_AlternateDBusNamesMetaclass_interface_annotation{0}"
1153
for old_interface_name in old_interface_names:
1154
@dbus_interface_annotations(old_interface_name)
1156
return { "org.freedesktop.DBus.Deprecated": "true" }
1157
# Find an unused name
1158
for aname in (basename.format(i) for i in
1160
if aname not in attr:
1163
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"})
1166
1195
class ClientDBus(Client, DBusObjectWithProperties):
1167
1196
"""A Client class using D-Bus
2349
2374
(stored_state))
2350
2375
os.remove(stored_state_path)
2351
2376
except IOError as e:
2352
logger.warning("Could not load persistent state: {0}"
2354
if e.errno != errno.ENOENT:
2377
if e.errno == errno.ENOENT:
2378
logger.warning("Could not load persistent state: {0}"
2379
.format(os.strerror(e.errno)))
2381
logger.critical("Could not load persistent state:",
2356
2384
except EOFError as e:
2357
2385
logger.warning("Could not load persistent state: "
2358
"EOFError: {0}".format(e))
2386
"EOFError:", exc_info=e)
2360
2388
with PGPEngine() as pgp:
2361
2389
for client_name, client in clients_data.iteritems():
2559
2586
pickle.dump((clients, client_settings), stored_state)
2560
2587
os.rename(tempname, stored_state_path)
2561
2588
except (IOError, OSError) as e:
2562
logger.warning("Could not save persistent state: {0}"
2566
2591
os.remove(tempname)
2567
2592
except NameError:
2569
if e.errno not in set((errno.ENOENT, errno.EACCES,
2594
if e.errno in (errno.ENOENT, errno.EACCES, errno.EEXIST):
2595
logger.warning("Could not save persistent state: {0}"
2596
.format(os.strerror(e.errno)))
2598
logger.warning("Could not save persistent state:",
2573
2602
# Delete all clients, and settings from config