Skip to content

Commit

Permalink
Use set operations instead of side-effects
Browse files Browse the repository at this point in the history
It's less confusing not to mutate the data structures being processed.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jul 18, 2023
1 parent 0ef6880 commit 63af50a
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions scripts/monitor_dbus_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,9 @@ def __repr__(self):
f"{self.old_value!r})"
)

def _check_props(object_path, ifn, old_props, new_props):
def _check_props(
object_path, ifn, old_props, new_props
): # pylint: disable=too-many-locals
"""
Find differences between two sets of properties.
Expand All @@ -546,11 +548,17 @@ def _check_props(object_path, ifn, old_props, new_props):
string_data = _INTROSPECTABLE.Methods.Introspect(proxy, {})
xml_data = ET.fromstring(string_data)

for key, new_value in new_props.items():
if key not in old_props:
diffs.append(AddedProperty(object_path, ifn, key, new_value))
continue
old_props_keys = frozenset(old_props.keys())
new_props_keys = frozenset(new_props.keys())

for key in old_props_keys - new_props_keys:
diffs.append(RemovedProperty(object_path, ifn, key, old_props[key]))

for key in new_props_keys - old_props_keys:
diffs.append(AddedProperty(object_path, ifn, key, new_props[key]))

for key in new_props_keys & old_props_keys:
new_value = new_props[key]
old_value = old_props[key]

if new_value != old_value:
Expand Down Expand Up @@ -586,11 +594,6 @@ def _check_props(object_path, ifn, old_props, new_props):
ChangedProperty(object_path, ifn, key, old_value, new_value)
)

del old_props[key]

for key, old_value in old_props.items():
diffs.append(RemovedProperty(object_path, ifn, key, old_value))

return diffs

def _check():
Expand Down Expand Up @@ -618,32 +621,38 @@ def _check():
mos = _MAKE_MO() # pylint: disable=not-callable

diffs = []
for object_path, new_data in mos.items():
if object_path not in _MO: # pylint: disable=unsupported-membership-test
diffs.append(AddedObjectPath(object_path, new_data))
continue

old_object_paths = frozenset(_MO.keys())
new_object_paths = frozenset(mos.keys())

for object_path in old_object_paths - new_object_paths:
diffs.append(
# pylint: disable=unsubscriptable-object
RemovedObjectPath(object_path, _MO[object_path])
)

for object_path in new_object_paths - old_object_paths:
diffs.append(AddedObjectPath(object_path, mos[object_path]))

for object_path in new_object_paths & old_object_paths:
old_data = _MO[object_path] # pylint: disable=unsubscriptable-object
new_data = mos[object_path]

for ifn, new_props in new_data.items():
if ifn not in old_data:
diffs.append(AddedInterface(object_path, ifn, new_props))
continue
old_ifns = frozenset(old_data.keys())
new_ifns = frozenset(new_data.keys())

old_props = old_data[ifn]
prop_diffs = _check_props(object_path, ifn, old_props, new_props)
diffs.extend(prop_diffs)
del old_data[ifn]
for ifn in new_ifns - old_ifns:
diffs.append(AddedInterface(object_path, ifn, new_data[ifn]))

for ifn, old_props in old_data.items():
diffs.append(RemovedInterface(object_path, ifn, old_props))
for ifn in old_ifns - new_ifns:
diffs.append(RemovedInterface(object_path, ifn, old_data[ifn]))

del _MO[object_path] # pylint: disable=unsupported-delete-operation
for ifn in old_ifns & new_ifns:
old_props = old_data[ifn]
new_props = new_data[ifn]

assert isinstance(_MO, dict)
if _MO:
for object_path, old_data in _MO.items():
diffs.append(RemovedObjectPath(object_path, old_data))
prop_diffs = _check_props(object_path, ifn, old_props, new_props)
diffs.extend(prop_diffs)

return diffs

Expand Down

0 comments on commit 63af50a

Please sign in to comment.