631
626
def _get_all_dbus_properties(self):
632
627
"""Returns a generator of (name, attribute) pairs
634
return ((prop.__get__(self)._dbus_name, prop.__get__(self))
635
for cls in self.__class__.__mro__
629
return ((prop._dbus_name, prop)
636
630
for name, prop in
637
inspect.getmembers(cls, self._is_dbus_property))
631
inspect.getmembers(self, self._is_dbus_property))
639
633
def _get_dbus_property(self, interface_name, property_name):
640
634
"""Returns a bound method if one exists which is a D-Bus
641
635
property with the specified name and interface.
643
for cls in self.__class__.__mro__:
644
for name, value in (inspect.getmembers
645
(cls, self._is_dbus_property)):
646
if (value._dbus_name == property_name
647
and value._dbus_interface == interface_name):
648
return value.__get__(self)
637
for name in (property_name,
638
property_name + "_dbus_property"):
639
prop = getattr(self, name, None)
641
or not self._is_dbus_property(prop)
642
or prop._dbus_name != property_name
643
or (interface_name and prop._dbus_interface
644
and interface_name != prop._dbus_interface)):
650
647
# No such property
651
648
raise DBusPropertyNotFound(self.dbus_object_path + ":"
652
649
+ interface_name + "."
761
758
return dbus.String(dt.isoformat(),
762
759
variant_level=variant_level)
764
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
766
"""Applied to an empty subclass of a D-Bus object, this metaclass
767
will add additional D-Bus attributes matching a certain pattern.
769
def __new__(mcs, name, bases, attr):
770
# Go through all the base classes which could have D-Bus
771
# methods, signals, or properties in them
772
for base in (b for b in bases
773
if issubclass(b, dbus.service.Object)):
774
# Go though all attributes of the base class
775
for attrname, attribute in inspect.getmembers(base):
776
# Ignore non-D-Bus attributes, and D-Bus attributes
777
# with the wrong interface name
778
if (not hasattr(attribute, "_dbus_interface")
779
or not attribute._dbus_interface
780
.startswith("se.recompile.Mandos")):
782
# Create an alternate D-Bus interface name based on
784
alt_interface = (attribute._dbus_interface
785
.replace("se.recompile.Mandos",
786
"se.bsnet.fukt.Mandos"))
787
# Is this a D-Bus signal?
788
if getattr(attribute, "_dbus_is_signal", False):
789
# Extract the original non-method function by
791
nonmethod_func = (dict(
792
zip(attribute.func_code.co_freevars,
793
attribute.__closure__))["func"]
795
# Create a new, but exactly alike, function
796
# object, and decorate it to be a new D-Bus signal
797
# with the alternate D-Bus interface name
798
new_function = (dbus.service.signal
800
attribute._dbus_signature)
802
nonmethod_func.func_code,
803
nonmethod_func.func_globals,
804
nonmethod_func.func_name,
805
nonmethod_func.func_defaults,
806
nonmethod_func.func_closure)))
807
# Define a creator of a function to call both the
808
# old and new functions, so both the old and new
809
# signals gets sent when the function is called
810
def fixscope(func1, func2):
811
"""This function is a scope container to pass
812
func1 and func2 to the "call_both" function
813
outside of its arguments"""
814
def call_both(*args, **kwargs):
815
"""This function will emit two D-Bus
816
signals by calling func1 and func2"""
817
func1(*args, **kwargs)
818
func2(*args, **kwargs)
820
# Create the "call_both" function and add it to
822
attr[attrname] = fixscope(attribute,
824
# Is this a D-Bus method?
825
elif getattr(attribute, "_dbus_is_method", False):
826
# Create a new, but exactly alike, function
827
# object. Decorate it to be a new D-Bus method
828
# with the alternate D-Bus interface name. Add it
830
attr[attrname] = (dbus.service.method
832
attribute._dbus_in_signature,
833
attribute._dbus_out_signature)
835
(attribute.func_code,
836
attribute.func_globals,
838
attribute.func_defaults,
839
attribute.func_closure)))
840
# Is this a D-Bus property?
841
elif getattr(attribute, "_dbus_is_property", False):
842
# Create a new, but exactly alike, function
843
# object, and decorate it to be a new D-Bus
844
# property with the alternate D-Bus interface
845
# name. Add it to the class.
846
attr[attrname] = (dbus_service_property
848
attribute._dbus_signature,
849
attribute._dbus_access,
851
._dbus_get_args_options
854
(attribute.func_code,
855
attribute.func_globals,
857
attribute.func_defaults,
858
attribute.func_closure)))
859
return type.__new__(mcs, name, bases, attr)
861
762
class ClientDBus(Client, DBusObjectWithProperties):
862
763
"""A Client class using D-Bus
888
789
def notifychangeproperty(transform_func,
889
790
dbus_name, type_func=lambda x: x,
890
791
variant_level=1):
891
""" Modify a variable so that it's a property which announces
894
transform_fun: Function that takes a value and transforms it
896
dbus_name: D-Bus name of the variable
792
""" Modify a variable so that its a property that announce its
794
transform_fun: Function that takes a value and transform it to
796
dbus_name: DBus name of the variable
897
797
type_func: Function that transform the value before sending it
898
to the D-Bus. Default: no transform
899
variant_level: D-Bus variant level. Default: 1
799
variant_level: DBus variant level. default: 1
901
attrname = "_{0}".format(dbus_name)
902
802
def setter(self, value):
803
old_value = real_value[0]
804
real_value[0] = value
903
805
if hasattr(self, "dbus_object_path"):
904
if (not hasattr(self, attrname) or
905
type_func(getattr(self, attrname, None))
906
!= type_func(value)):
907
dbus_value = transform_func(type_func(value),
806
if type_func(old_value) != type_func(real_value[0]):
807
dbus_value = transform_func(type_func(real_value[0]),
909
809
self.PropertyChanged(dbus.String(dbus_name),
911
setattr(self, attrname, value)
913
return property(lambda self: getattr(self, attrname), setter)
812
return property(lambda self: real_value[0], setter)
916
815
expires = notifychangeproperty(datetime_to_dbus, "Expires")
921
820
last_enabled = notifychangeproperty(datetime_to_dbus,
923
822
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
924
type_func = lambda checker:
823
type_func = lambda checker: checker is not None)
926
824
last_checked_ok = notifychangeproperty(datetime_to_dbus,
928
last_approval_request = notifychangeproperty(
929
datetime_to_dbus, "LastApprovalRequest")
826
last_approval_request = notifychangeproperty(datetime_to_dbus,
827
"LastApprovalRequest")
930
828
approved_by_default = notifychangeproperty(dbus.Boolean,
931
829
"ApprovedByDefault")
932
approval_delay = notifychangeproperty(dbus.UInt16,
935
_timedelta_to_milliseconds)
936
approval_duration = notifychangeproperty(
937
dbus.UInt16, "ApprovalDuration",
938
type_func = _timedelta_to_milliseconds)
830
approval_delay = notifychangeproperty(dbus.UInt16, "ApprovalDelay",
831
type_func = _timedelta_to_milliseconds)
832
approval_duration = notifychangeproperty(dbus.UInt16, "ApprovalDuration",
833
type_func = _timedelta_to_milliseconds)
939
834
host = notifychangeproperty(dbus.String, "Host")
940
835
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
942
_timedelta_to_milliseconds)
943
extended_timeout = notifychangeproperty(
944
dbus.UInt16, "ExtendedTimeout",
945
type_func = _timedelta_to_milliseconds)
946
interval = notifychangeproperty(dbus.UInt16,
949
_timedelta_to_milliseconds)
836
type_func = _timedelta_to_milliseconds)
837
extended_timeout = notifychangeproperty(dbus.UInt16, "ExtendedTimeout",
838
type_func = _timedelta_to_milliseconds)
839
interval = notifychangeproperty(dbus.UInt16, "Interval",
840
type_func = _timedelta_to_milliseconds)
950
841
checker_command = notifychangeproperty(dbus.String, "Checker")
952
843
del notifychangeproperty
1678
1562
"dress: %s", fpr, address)
1679
1563
if self.use_dbus:
1680
1564
# Emit D-Bus signal
1681
mandos_dbus_service.ClientNotFound(fpr,
1565
mandos_dbus_service.ClientNotFound(fpr, address[0])
1683
1566
parent_pipe.send(False)
1686
1569
gobject.io_add_watch(parent_pipe.fileno(),
1687
1570
gobject.IO_IN | gobject.IO_HUP,
1688
1571
functools.partial(self.handle_ipc,
1572
parent_pipe = parent_pipe,
1573
client_object = client))
1693
1574
parent_pipe.send(True)
1694
# remove the old hook in favor of the new above hook on
1575
# remove the old hook in favor of the new above hook on same fileno
1697
1577
if command == 'funcall':
1698
1578
funcname = request[1]
1699
1579
args = request[2]
1700
1580
kwargs = request[3]
1702
parent_pipe.send(('data', getattr(client_object,
1582
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1706
1584
if command == 'getattr':
1707
1585
attrname = request[1]
1708
1586
if callable(client_object.__getattribute__(attrname)):
1709
1587
parent_pipe.send(('function',))
1711
parent_pipe.send(('data', client_object
1712
.__getattribute__(attrname)))
1589
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1714
1591
if command == 'setattr':
1715
1592
attrname = request[1]