485
477
return now < (self.last_checked_ok + self.timeout)
488
def dbus_service_property(dbus_interface, signature=u"v",
489
access=u"readwrite", byte_arrays=False):
490
"""Decorators for marking methods of a DBusObjectWithProperties to
491
become properties on the D-Bus.
493
The decorated method will be called with no arguments by "Get"
494
and with one argument by "Set".
496
The parameters, where they are supported, are the same as
497
dbus.service.method, except there is only "signature", since the
498
type from Get() and the type sent to Set() is the same.
501
func._dbus_is_property = True
502
func._dbus_interface = dbus_interface
503
func._dbus_signature = signature
504
func._dbus_access = access
505
func._dbus_name = func.__name__
506
if func._dbus_name.endswith(u"_dbus_property"):
507
func._dbus_name = func._dbus_name[:-14]
508
func._dbus_get_args_options = {u'byte_arrays': byte_arrays }
513
class DBusPropertyException(dbus.exceptions.DBusException):
514
"""A base class for D-Bus property-related exceptions
516
def __unicode__(self):
517
return unicode(str(self))
520
class DBusPropertyAccessException(DBusPropertyException):
521
"""A property's access permissions disallows an operation.
526
class DBusPropertyNotFound(DBusPropertyException):
527
"""An attempt was made to access a non-existing property.
532
class DBusObjectWithProperties(dbus.service.Object):
533
"""A D-Bus object with properties.
535
Classes inheriting from this can use the dbus_service_property
536
decorator to expose methods as D-Bus properties. It exposes the
537
standard Get(), Set(), and GetAll() methods on the D-Bus.
541
def _is_dbus_property(obj):
542
return getattr(obj, u"_dbus_is_property", False)
544
def _get_all_dbus_properties(self):
545
"""Returns a generator of (name, attribute) pairs
547
return ((prop._dbus_name, prop)
549
inspect.getmembers(self, self._is_dbus_property))
551
def _get_dbus_property(self, interface_name, property_name):
552
"""Returns a bound method if one exists which is a D-Bus
553
property with the specified name and interface.
555
for name in (property_name,
556
property_name + u"_dbus_property"):
557
prop = getattr(self, name, None)
559
or not self._is_dbus_property(prop)
560
or prop._dbus_name != property_name
561
or (interface_name and prop._dbus_interface
562
and interface_name != prop._dbus_interface)):
566
raise DBusPropertyNotFound(self.dbus_object_path + u":"
567
+ interface_name + u"."
570
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature=u"ss",
572
def Get(self, interface_name, property_name):
573
"""Standard D-Bus property Get() method, see D-Bus standard.
575
prop = self._get_dbus_property(interface_name, property_name)
576
if prop._dbus_access == u"write":
577
raise DBusPropertyAccessException(property_name)
579
if not hasattr(value, u"variant_level"):
581
return type(value)(value, variant_level=value.variant_level+1)
583
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature=u"ssv")
584
def Set(self, interface_name, property_name, value):
585
"""Standard D-Bus property Set() method, see D-Bus standard.
587
prop = self._get_dbus_property(interface_name, property_name)
588
if prop._dbus_access == u"read":
589
raise DBusPropertyAccessException(property_name)
590
if prop._dbus_get_args_options[u"byte_arrays"]:
591
value = dbus.ByteArray(''.join(unichr(byte)
595
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature=u"s",
596
out_signature=u"a{sv}")
597
def GetAll(self, interface_name):
598
"""Standard D-Bus property GetAll() method, see D-Bus
601
Note: Will not include properties with access="write".
604
for name, prop in self._get_all_dbus_properties():
606
and interface_name != prop._dbus_interface):
607
# Interface non-empty but did not match
609
# Ignore write-only properties
610
if prop._dbus_access == u"write":
613
if not hasattr(value, u"variant_level"):
616
all[name] = type(value)(value, variant_level=
617
value.variant_level+1)
618
return dbus.Dictionary(all, signature=u"sv")
620
@dbus.service.method(dbus.INTROSPECTABLE_IFACE,
622
path_keyword='object_path',
623
connection_keyword='connection')
624
def Introspect(self, object_path, connection):
625
"""Standard D-Bus method, overloaded to insert property tags.
627
xmlstring = dbus.service.Object.Introspect(self, object_path,
629
document = xml.dom.minidom.parseString(xmlstring)
631
def make_tag(document, name, prop):
632
e = document.createElement(u"property")
633
e.setAttribute(u"name", name)
634
e.setAttribute(u"type", prop._dbus_signature)
635
e.setAttribute(u"access", prop._dbus_access)
637
for if_tag in document.getElementsByTagName(u"interface"):
638
for tag in (make_tag(document, name, prop)
640
in self._get_all_dbus_properties()
641
if prop._dbus_interface
642
== if_tag.getAttribute(u"name")):
643
if_tag.appendChild(tag)
644
xmlstring = document.toxml(u"utf-8")
649
class ClientDBus(Client, DBusObjectWithProperties):
480
class ClientDBus(Client, dbus.service.Object):
650
481
"""A Client class using D-Bus
614
# GetAllProperties - method
615
@dbus.service.method(_interface, out_signature=u"a{sv}")
616
def GetAllProperties(self):
618
return dbus.Dictionary({
619
dbus.String(u"name"):
620
dbus.String(self.name, variant_level=1),
621
dbus.String(u"fingerprint"):
622
dbus.String(self.fingerprint, variant_level=1),
623
dbus.String(u"host"):
624
dbus.String(self.host, variant_level=1),
625
dbus.String(u"created"):
626
self._datetime_to_dbus(self.created,
628
dbus.String(u"last_enabled"):
629
(self._datetime_to_dbus(self.last_enabled,
631
if self.last_enabled is not None
632
else dbus.Boolean(False, variant_level=1)),
633
dbus.String(u"enabled"):
634
dbus.Boolean(self.enabled, variant_level=1),
635
dbus.String(u"last_checked_ok"):
636
(self._datetime_to_dbus(self.last_checked_ok,
638
if self.last_checked_ok is not None
639
else dbus.Boolean (False, variant_level=1)),
640
dbus.String(u"timeout"):
641
dbus.UInt64(self.timeout_milliseconds(),
643
dbus.String(u"interval"):
644
dbus.UInt64(self.interval_milliseconds(),
646
dbus.String(u"checker"):
647
dbus.String(self.checker_command,
649
dbus.String(u"checker_running"):
650
dbus.Boolean(self.checker is not None,
652
dbus.String(u"object_path"):
653
dbus.ObjectPath(self.dbus_object_path,
657
# IsStillValid - method
658
@dbus.service.method(_interface, out_signature=u"b")
659
def IsStillValid(self):
660
return self.still_valid()
783
662
# PropertyChanged - signal
784
663
@dbus.service.signal(_interface, signature=u"sv")
785
664
def PropertyChanged(self, property, value):
680
# SetChecker - method
681
@dbus.service.method(_interface, in_signature=u"s")
682
def SetChecker(self, checker):
683
"D-Bus setter method"
684
self.checker_command = checker
686
self.PropertyChanged(dbus.String(u"checker"),
687
dbus.String(self.checker_command,
691
@dbus.service.method(_interface, in_signature=u"s")
692
def SetHost(self, host):
693
"D-Bus setter method"
696
self.PropertyChanged(dbus.String(u"host"),
697
dbus.String(self.host, variant_level=1))
699
# SetInterval - method
700
@dbus.service.method(_interface, in_signature=u"t")
701
def SetInterval(self, milliseconds):
702
self.interval = datetime.timedelta(0, 0, 0, milliseconds)
704
self.PropertyChanged(dbus.String(u"interval"),
705
(dbus.UInt64(self.interval_milliseconds(),
709
@dbus.service.method(_interface, in_signature=u"ay",
711
def SetSecret(self, secret):
712
"D-Bus setter method"
713
self.secret = str(secret)
715
# SetTimeout - method
716
@dbus.service.method(_interface, in_signature=u"t")
717
def SetTimeout(self, milliseconds):
718
self.timeout = datetime.timedelta(0, 0, 0, milliseconds)
720
self.PropertyChanged(dbus.String(u"timeout"),
721
(dbus.UInt64(self.timeout_milliseconds(),
801
724
# Enable - method
802
725
@dbus.service.method(_interface)
803
726
def Enable(self):
821
744
def StopChecker(self):
822
745
self.stop_checker()
825
@dbus_service_property(_interface, signature=u"s", access=u"read")
826
def name_dbus_property(self):
827
return dbus.String(self.name)
829
# fingerprint - property
830
@dbus_service_property(_interface, signature=u"s", access=u"read")
831
def fingerprint_dbus_property(self):
832
return dbus.String(self.fingerprint)
835
@dbus_service_property(_interface, signature=u"s",
837
def host_dbus_property(self, value=None):
838
if value is None: # get
839
return dbus.String(self.host)
842
self.PropertyChanged(dbus.String(u"host"),
843
dbus.String(value, variant_level=1))
846
@dbus_service_property(_interface, signature=u"s", access=u"read")
847
def created_dbus_property(self):
848
return dbus.String(self._datetime_to_dbus(self.created))
850
# last_enabled - property
851
@dbus_service_property(_interface, signature=u"s", access=u"read")
852
def last_enabled_dbus_property(self):
853
if self.last_enabled is None:
854
return dbus.String(u"")
855
return dbus.String(self._datetime_to_dbus(self.last_enabled))
858
@dbus_service_property(_interface, signature=u"b",
860
def enabled_dbus_property(self, value=None):
861
if value is None: # get
862
return dbus.Boolean(self.enabled)
868
# last_checked_ok - property
869
@dbus_service_property(_interface, signature=u"s",
871
def last_checked_ok_dbus_property(self, value=None):
872
if value is not None:
875
if self.last_checked_ok is None:
876
return dbus.String(u"")
877
return dbus.String(self._datetime_to_dbus(self
881
@dbus_service_property(_interface, signature=u"t",
883
def timeout_dbus_property(self, value=None):
884
if value is None: # get
885
return dbus.UInt64(self.timeout_milliseconds())
886
self.timeout = datetime.timedelta(0, 0, 0, value)
888
self.PropertyChanged(dbus.String(u"timeout"),
889
dbus.UInt64(value, variant_level=1))
890
if getattr(self, u"disable_initiator_tag", None) is None:
893
gobject.source_remove(self.disable_initiator_tag)
894
self.disable_initiator_tag = None
896
_timedelta_to_milliseconds((self
902
# The timeout has passed
905
self.disable_initiator_tag = (gobject.timeout_add
906
(time_to_die, self.disable))
908
# interval - property
909
@dbus_service_property(_interface, signature=u"t",
911
def interval_dbus_property(self, value=None):
912
if value is None: # get
913
return dbus.UInt64(self.interval_milliseconds())
914
self.interval = datetime.timedelta(0, 0, 0, value)
916
self.PropertyChanged(dbus.String(u"interval"),
917
dbus.UInt64(value, variant_level=1))
918
if getattr(self, u"checker_initiator_tag", None) is None:
920
# Reschedule checker run
921
gobject.source_remove(self.checker_initiator_tag)
922
self.checker_initiator_tag = (gobject.timeout_add
923
(value, self.start_checker))
924
self.start_checker() # Start one now, too
927
@dbus_service_property(_interface, signature=u"s",
929
def checker_dbus_property(self, value=None):
930
if value is None: # get
931
return dbus.String(self.checker_command)
932
self.checker_command = value
934
self.PropertyChanged(dbus.String(u"checker"),
935
dbus.String(self.checker_command,
938
# checker_running - property
939
@dbus_service_property(_interface, signature=u"b",
941
def checker_running_dbus_property(self, value=None):
942
if value is None: # get
943
return dbus.Boolean(self.checker is not None)
949
# object_path - property
950
@dbus_service_property(_interface, signature=u"o", access=u"read")
951
def object_path_dbus_property(self):
952
return self.dbus_object_path # is already a dbus.ObjectPath
955
@dbus_service_property(_interface, signature=u"ay",
956
access=u"write", byte_arrays=True)
957
def secret_dbus_property(self, value):
958
self.secret = str(value)