314
308
"host", "interval", "last_checked_ok",
315
309
"last_enabled", "name", "timeout")
312
def _timedelta_to_milliseconds(td):
313
"Convert a datetime.timedelta() to milliseconds"
314
return ((td.days * 24 * 60 * 60 * 1000)
315
+ (td.seconds * 1000)
316
+ (td.microseconds // 1000))
317
318
def timeout_milliseconds(self):
318
319
"Return the 'timeout' attribute in milliseconds"
319
return _timedelta_to_milliseconds(self.timeout)
320
return self._timedelta_to_milliseconds(self.timeout)
321
322
def extended_timeout_milliseconds(self):
322
323
"Return the 'extended_timeout' attribute in milliseconds"
323
return _timedelta_to_milliseconds(self.extended_timeout)
324
return self._timedelta_to_milliseconds(self.extended_timeout)
325
326
def interval_milliseconds(self):
326
327
"Return the 'interval' attribute in milliseconds"
327
return _timedelta_to_milliseconds(self.interval)
328
return self._timedelta_to_milliseconds(self.interval)
329
330
def approval_delay_milliseconds(self):
330
return _timedelta_to_milliseconds(self.approval_delay)
331
return self._timedelta_to_milliseconds(self.approval_delay)
332
333
def __init__(self, name = None, disable_hook=None, config=None):
333
334
"""Note: the 'checker' key in 'config' sets the
785
777
("/clients/" + client_object_name))
786
778
DBusObjectWithProperties.__init__(self, self.bus,
787
779
self.dbus_object_path)
789
def notifychangeproperty(transform_func,
790
dbus_name, type_func=lambda x: x,
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
797
type_func: Function that transform the value before sending it
799
variant_level: DBus variant level. default: 1
802
def setter(self, value):
803
old_value = real_value[0]
804
real_value[0] = value
805
if hasattr(self, "dbus_object_path"):
806
if type_func(old_value) != type_func(real_value[0]):
807
dbus_value = transform_func(type_func(real_value[0]),
809
self.PropertyChanged(dbus.String(dbus_name),
812
return property(lambda self: real_value[0], setter)
815
expires = notifychangeproperty(datetime_to_dbus, "Expires")
816
approvals_pending = notifychangeproperty(dbus.Boolean,
819
enabled = notifychangeproperty(dbus.Boolean, "Enabled")
820
last_enabled = notifychangeproperty(datetime_to_dbus,
822
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
823
type_func = lambda checker: checker is not None)
824
last_checked_ok = notifychangeproperty(datetime_to_dbus,
826
last_approval_request = notifychangeproperty(datetime_to_dbus,
827
"LastApprovalRequest")
828
approved_by_default = notifychangeproperty(dbus.Boolean,
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)
834
host = notifychangeproperty(dbus.String, "Host")
835
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
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)
841
checker_command = notifychangeproperty(dbus.String, "Checker")
843
del notifychangeproperty
780
def _set_expires(self, value):
781
old_value = getattr(self, "_expires", None)
782
self._expires = value
783
if hasattr(self, "dbus_object_path") and old_value != value:
784
dbus_time = (self._datetime_to_dbus(self._expires,
786
self.PropertyChanged(dbus.String("Expires"),
788
expires = property(lambda self: self._expires, _set_expires)
791
def _get_approvals_pending(self):
792
return self._approvals_pending
793
def _set_approvals_pending(self, value):
794
old_value = self._approvals_pending
795
self._approvals_pending = value
797
if (hasattr(self, "dbus_object_path")
798
and bval is not bool(old_value)):
799
dbus_bool = dbus.Boolean(bval, variant_level=1)
800
self.PropertyChanged(dbus.String("ApprovalPending"),
803
approvals_pending = property(_get_approvals_pending,
804
_set_approvals_pending)
805
del _get_approvals_pending, _set_approvals_pending
808
def _datetime_to_dbus(dt, variant_level=0):
809
"""Convert a UTC datetime.datetime() to a D-Bus type."""
811
return dbus.String("", variant_level = variant_level)
812
return dbus.String(dt.isoformat(),
813
variant_level=variant_level)
816
oldstate = getattr(self, "enabled", False)
817
r = Client.enable(self)
818
if oldstate != self.enabled:
820
self.PropertyChanged(dbus.String("Enabled"),
821
dbus.Boolean(True, variant_level=1))
822
self.PropertyChanged(
823
dbus.String("LastEnabled"),
824
self._datetime_to_dbus(self.last_enabled,
828
def disable(self, quiet = False):
829
oldstate = getattr(self, "enabled", False)
830
r = Client.disable(self, quiet=quiet)
831
if not quiet and oldstate != self.enabled:
833
self.PropertyChanged(dbus.String("Enabled"),
834
dbus.Boolean(False, variant_level=1))
845
837
def __del__(self, *args, **kwargs):
870
865
return Client.checker_callback(self, pid, condition, command,
868
def checked_ok(self, *args, **kwargs):
869
Client.checked_ok(self, *args, **kwargs)
871
self.PropertyChanged(
872
dbus.String("LastCheckedOK"),
873
(self._datetime_to_dbus(self.last_checked_ok,
876
def need_approval(self, *args, **kwargs):
877
r = Client.need_approval(self, *args, **kwargs)
879
self.PropertyChanged(
880
dbus.String("LastApprovalRequest"),
881
(self._datetime_to_dbus(self.last_approval_request,
873
885
def start_checker(self, *args, **kwargs):
874
886
old_checker = self.checker
875
887
if self.checker is not None:
882
894
and old_checker_pid != self.checker.pid):
883
895
# Emit D-Bus signal
884
896
self.CheckerStarted(self.current_checker_command)
897
self.PropertyChanged(
898
dbus.String("CheckerRunning"),
899
dbus.Boolean(True, variant_level=1))
902
def stop_checker(self, *args, **kwargs):
903
old_checker = getattr(self, "checker", None)
904
r = Client.stop_checker(self, *args, **kwargs)
905
if (old_checker is not None
906
and getattr(self, "checker", None) is None):
907
self.PropertyChanged(dbus.String("CheckerRunning"),
908
dbus.Boolean(False, variant_level=1))
887
911
def _reset_approved(self):
888
912
self._approved = None
996
1025
def ApprovalDelay_dbus_property(self, value=None):
997
1026
if value is None: # get
998
1027
return dbus.UInt64(self.approval_delay_milliseconds())
1028
old_value = self.approval_delay
999
1029
self.approval_delay = datetime.timedelta(0, 0, 0, value)
1031
if old_value != self.approval_delay:
1032
self.PropertyChanged(dbus.String("ApprovalDelay"),
1033
dbus.UInt64(value, variant_level=1))
1001
1035
# ApprovalDuration - property
1002
1036
@dbus_service_property(_interface, signature="t",
1003
1037
access="readwrite")
1004
1038
def ApprovalDuration_dbus_property(self, value=None):
1005
1039
if value is None: # get
1006
return dbus.UInt64(_timedelta_to_milliseconds(
1040
return dbus.UInt64(self._timedelta_to_milliseconds(
1007
1041
self.approval_duration))
1042
old_value = self.approval_duration
1008
1043
self.approval_duration = datetime.timedelta(0, 0, 0, value)
1045
if old_value != self.approval_duration:
1046
self.PropertyChanged(dbus.String("ApprovalDuration"),
1047
dbus.UInt64(value, variant_level=1))
1010
1049
# Name - property
1011
1050
@dbus_service_property(_interface, signature="s", access="read")
1023
1062
def Host_dbus_property(self, value=None):
1024
1063
if value is None: # get
1025
1064
return dbus.String(self.host)
1065
old_value = self.host
1026
1066
self.host = value
1068
if old_value != self.host:
1069
self.PropertyChanged(dbus.String("Host"),
1070
dbus.String(value, variant_level=1))
1028
1072
# Created - property
1029
1073
@dbus_service_property(_interface, signature="s", access="read")
1030
1074
def Created_dbus_property(self):
1031
return dbus.String(datetime_to_dbus(self.created))
1075
return dbus.String(self._datetime_to_dbus(self.created))
1033
1077
# LastEnabled - property
1034
1078
@dbus_service_property(_interface, signature="s", access="read")
1035
1079
def LastEnabled_dbus_property(self):
1036
return datetime_to_dbus(self.last_enabled)
1080
return self._datetime_to_dbus(self.last_enabled)
1038
1082
# Enabled - property
1039
1083
@dbus_service_property(_interface, signature="b",
1092
1141
+ datetime.timedelta(milliseconds = time_to_die))
1093
1142
self.disable_initiator_tag = (gobject.timeout_add
1094
1143
(time_to_die, self.disable))
1096
1145
# ExtendedTimeout - property
1097
1146
@dbus_service_property(_interface, signature="t",
1098
1147
access="readwrite")
1099
1148
def ExtendedTimeout_dbus_property(self, value=None):
1100
1149
if value is None: # get
1101
1150
return dbus.UInt64(self.extended_timeout_milliseconds())
1151
old_value = self.extended_timeout
1102
1152
self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1154
if old_value != self.extended_timeout:
1155
self.PropertyChanged(dbus.String("ExtendedTimeout"),
1156
dbus.UInt64(value, variant_level=1))
1104
1158
# Interval - property
1105
1159
@dbus_service_property(_interface, signature="t",
1106
1160
access="readwrite")
1107
1161
def Interval_dbus_property(self, value=None):
1108
1162
if value is None: # get
1109
1163
return dbus.UInt64(self.interval_milliseconds())
1164
old_value = self.interval
1110
1165
self.interval = datetime.timedelta(0, 0, 0, value)
1167
if old_value != self.interval:
1168
self.PropertyChanged(dbus.String("Interval"),
1169
dbus.UInt64(value, variant_level=1))
1111
1170
if getattr(self, "checker_initiator_tag", None) is None:
1113
1172
# Reschedule checker run
1115
1174
self.checker_initiator_tag = (gobject.timeout_add
1116
1175
(value, self.start_checker))
1117
1176
self.start_checker() # Start one now, too
1119
1178
# Checker - property
1120
1179
@dbus_service_property(_interface, signature="s",
1121
1180
access="readwrite")
1122
1181
def Checker_dbus_property(self, value=None):
1123
1182
if value is None: # get
1124
1183
return dbus.String(self.checker_command)
1184
old_value = self.checker_command
1125
1185
self.checker_command = value
1187
if old_value != self.checker_command:
1188
self.PropertyChanged(dbus.String("Checker"),
1189
dbus.String(self.checker_command,
1127
1192
# CheckerRunning - property
1128
1193
@dbus_service_property(_interface, signature="b",