-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADCM-6277 Change diff calculation algorithm (#75)
Co-authored-by: Araslanov Egor <[email protected]>
- Loading branch information
Showing
5 changed files
with
88 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,35 @@ | ||
from adcm_aio_client.config._types import ( | ||
ConfigSchema, | ||
FullConfigDifference, | ||
GenericConfigData, | ||
LevelNames, | ||
ValueChange, | ||
full_name_to_level_names, | ||
) | ||
from contextlib import suppress | ||
|
||
from adcm_aio_client.config._types import ConfigSchema, GenericConfigData, LevelNames, ParameterChange | ||
|
||
|
||
# Difference | ||
def find_config_difference( | ||
previous: GenericConfigData, current: GenericConfigData, schema: ConfigSchema | ||
) -> FullConfigDifference: | ||
diff = FullConfigDifference(schema=schema) | ||
|
||
_fill_values_diff_at_level(level=(), diff=diff, previous=previous.values, current=current.values) | ||
_fill_attributes_diff(diff=diff, previous=previous.attributes, current=current.attributes) | ||
|
||
return diff | ||
|
||
) -> dict[LevelNames, ParameterChange]: | ||
diff = {} | ||
|
||
def _fill_values_diff_at_level(level: LevelNames, diff: FullConfigDifference, previous: dict, current: dict) -> None: | ||
missing = object() | ||
for key, cur_value in current.items(): | ||
level_names = (*level, key) | ||
prev_value = previous.get(key, missing) | ||
for names, _ in schema.iterate_parameters(): | ||
prev = {"value": None, "attrs": {}} | ||
cur = {"value": None, "attrs": {}} | ||
|
||
if prev_value is missing: | ||
# there may be collision between two None's, but for now we'll consider it a "special case" | ||
diff.values[level_names] = ValueChange(previous=None, current=cur_value) | ||
continue | ||
# TypeError / KeyError may occur when `None` is in values | ||
# (e.g. structure with dict as root item and with None value) | ||
if not schema.is_group(names): | ||
with suppress(TypeError, KeyError): | ||
prev["value"] = previous.get_value(names) | ||
|
||
if cur_value == prev_value: | ||
continue | ||
with suppress(TypeError, KeyError): | ||
cur["value"] = current.get_value(names) | ||
else: | ||
prev.pop("value") | ||
cur.pop("value") | ||
|
||
if not (diff.schema.is_group(level_names) and isinstance(prev_value, dict) and (isinstance(cur_value, dict))): | ||
diff.values[level_names] = ValueChange(previous=prev_value, current=cur_value) | ||
continue | ||
attr_key = f"/{'/'.join(names)}" | ||
prev["attrs"] = previous.attributes.get(attr_key, {}) | ||
cur["attrs"] = current.attributes.get(attr_key, {}) | ||
|
||
_fill_values_diff_at_level(diff=diff, level=level_names, previous=prev_value, current=cur_value) | ||
if prev != cur: | ||
diff[names] = ParameterChange(previous=prev, current=cur) | ||
|
||
|
||
def _fill_attributes_diff(diff: FullConfigDifference, previous: dict, current: dict) -> None: | ||
missing = object() | ||
for full_name, cur_value in current.items(): | ||
prev_value = previous.get(full_name, missing) | ||
if cur_value == prev_value: | ||
continue | ||
|
||
level_names = full_name_to_level_names(full_name) | ||
|
||
if prev_value is missing: | ||
prev_value = None | ||
|
||
diff.attributes[level_names] = ValueChange(previous=prev_value, current=cur_value) | ||
return diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters