Deprecate property-del:name events and introduce property-reset:name instead

And the same for -pre- events.

The property-del name is really confusing (it makes sense only for those
with deep knowledge of the implementation), because the property isn't
really deleted - it is only reverted to the "default" state (which most
properties have). So, name the event property-reset, intentionally
similar to property-set, as it is also kind of a value change.

Additionally the property-reset event is meant to be called when the
(dynamic) default value changes. Due to the current implementation, it
is a manual process so it can't be guaranteed to be called in all those
cases, but lets try to cover as much as possible.

Fixes QubesOS/qubes-issues#5834
This commit is contained in:
Marek Marczykowski-Górecki 2020-05-21 04:24:55 +02:00
parent c7d3635972
commit b06f831528
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 50 additions and 0 deletions

View File

@ -284,6 +284,10 @@ class property: # pylint: disable=redefined-builtin,invalid-name
has_oldvalue = False has_oldvalue = False
if has_oldvalue: if has_oldvalue:
instance.fire_event('property-pre-reset:' + self.__name__,
pre_event=True,
name=self.__name__, oldvalue=oldvalue)
# deprecated, to be removed in Qubes 5.0
instance.fire_event('property-pre-del:' + self.__name__, instance.fire_event('property-pre-del:' + self.__name__,
pre_event=True, pre_event=True,
name=self.__name__, oldvalue=oldvalue) name=self.__name__, oldvalue=oldvalue)
@ -291,13 +295,23 @@ class property: # pylint: disable=redefined-builtin,invalid-name
delattr(instance, self._attr_name) delattr(instance, self._attr_name)
except AttributeError: except AttributeError:
pass pass
instance.fire_event('property-reset:' + self.__name__,
name=self.__name__, oldvalue=oldvalue)
# deprecated, to be removed in Qubes 5.0
instance.fire_event('property-del:' + self.__name__, instance.fire_event('property-del:' + self.__name__,
name=self.__name__, oldvalue=oldvalue) name=self.__name__, oldvalue=oldvalue)
else: else:
instance.fire_event('property-pre-reset:' + self.__name__,
pre_event=True,
name=self.__name__)
# deprecated, to be removed in Qubes 5.0
instance.fire_event('property-pre-del:' + self.__name__, instance.fire_event('property-pre-del:' + self.__name__,
pre_event=True, pre_event=True,
name=self.__name__) name=self.__name__)
instance.fire_event('property-reset:' + self.__name__,
name=self.__name__)
# deprecated, to be removed in Qubes 5.0
instance.fire_event('property-del:' + self.__name__, instance.fire_event('property-del:' + self.__name__,
name=self.__name__) name=self.__name__)
@ -470,6 +484,9 @@ class PropertyHolder(qubes.events.Emitter):
Fired when property gets deleted (is set to default). Signature is Fired when property gets deleted (is set to default). Signature is
variable, *oldvalue* is present only if there was an old value. variable, *oldvalue* is present only if there was an old value.
This event is deprecated and will be removed in Qubes 5.0.
Use property-reset instead.
:param name: Property name :param name: Property name
:param oldvalue: Old value of the property :param oldvalue: Old value of the property
@ -479,6 +496,31 @@ class PropertyHolder(qubes.events.Emitter):
Fired before property gets deleted (is set to default). Signature Fired before property gets deleted (is set to default). Signature
is variable, *oldvalue* is present only if there was an old value. is variable, *oldvalue* is present only if there was an old value.
This event is deprecated and will be removed in Qubes 5.0.
Use property-pre-reset instead.
:param name: Property name
:param oldvalue: Old value of the property
.. event:: property-reset:<propname> \
(subject, event, name[, oldvalue])
Fired when property gets reset to the (possibly dynamic) default.
This even may be also fired when the property is already in
"default" state, but the calculated default value changes.
Signature is variable, *oldvalue* is present only if there was an
old value.
:param name: Property name
:param oldvalue: Old value of the property
.. event:: property-pre-reset:<propname> \
(subject, event, name[, oldvalue])
Fired before property gets reset to the (possibly dynamic) default.
Signature is variable, *oldvalue* is present only if there was an
old value.
:param name: Property name :param name: Property name
:param oldvalue: Old value of the property :param oldvalue: Old value of the property

View File

@ -170,8 +170,12 @@ class TC_10_property(qubes.tests.QubesTestCase):
self.assertEventFired(self.holder, 'property-pre-del:testprop1', self.assertEventFired(self.holder, 'property-pre-del:testprop1',
kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'}) kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
self.assertEventFired(self.holder, 'property-pre-reset:testprop1',
kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
self.assertEventFired(self.holder, 'property-del:testprop1', self.assertEventFired(self.holder, 'property-del:testprop1',
kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'}) kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
self.assertEventFired(self.holder, 'property-reset:testprop1',
kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
def test_081_delete_by_assign(self): def test_081_delete_by_assign(self):
self.holder.testprop1 = 'testvalue' self.holder.testprop1 = 'testvalue'
@ -203,8 +207,12 @@ class TC_10_property(qubes.tests.QubesTestCase):
self.assertEqual(holder.testprop1, 'defaultvalue') self.assertEqual(holder.testprop1, 'defaultvalue')
self.assertEventFired(holder, 'property-pre-del:testprop1', kwargs={ self.assertEventFired(holder, 'property-pre-del:testprop1', kwargs={
'name': 'testprop1', 'oldvalue': 'testvalue'}) 'name': 'testprop1', 'oldvalue': 'testvalue'})
self.assertEventFired(holder, 'property-pre-reset:testprop1', kwargs={
'name': 'testprop1', 'oldvalue': 'testvalue'})
self.assertEventFired(holder, 'property-del:testprop1', kwargs={ self.assertEventFired(holder, 'property-del:testprop1', kwargs={
'name': 'testprop1', 'oldvalue': 'testvalue'}) 'name': 'testprop1', 'oldvalue': 'testvalue'})
self.assertEventFired(holder, 'property-reset:testprop1', kwargs={
'name': 'testprop1', 'oldvalue': 'testvalue'})
def test_090_write_once_set(self): def test_090_write_once_set(self):
class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder): class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):