Improve clarity of the recursive_dict_ helper functions & usage.
This commit is contained in:
parent
3b3c690ae3
commit
53c86c30a1
2 changed files with 20 additions and 9 deletions
|
@ -325,7 +325,17 @@ def read_filebytes(filepath, range_min=0, range_max=None, chunk_size=bytestring.
|
||||||
def recursive_dict_update(target, supply):
|
def recursive_dict_update(target, supply):
|
||||||
'''
|
'''
|
||||||
Update target using supply, but when the value is a dictionary update the
|
Update target using supply, but when the value is a dictionary update the
|
||||||
insides instead of replacing the dictionary itself.
|
insides instead of replacing the dictionary itself. This prevents keys that
|
||||||
|
exist in the target but don't exist in the supply from being erased.
|
||||||
|
Note that we are modifying target in place.
|
||||||
|
|
||||||
|
eg:
|
||||||
|
target = {'hi': 'ho', 'neighbor': {'name': 'Wilson'}}
|
||||||
|
supply = {'neighbor': {'behind': 'fence'}}
|
||||||
|
|
||||||
|
result: {'hi': 'ho', 'neighbor': {'name': 'Wilson', 'behind': 'fence'}}
|
||||||
|
whereas a regular dict.update would have produced:
|
||||||
|
{'hi': 'ho', 'neighbor': {'behind': 'fence'}}
|
||||||
'''
|
'''
|
||||||
for (key, value) in supply.items():
|
for (key, value) in supply.items():
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
|
@ -333,7 +343,7 @@ def recursive_dict_update(target, supply):
|
||||||
if existing is None:
|
if existing is None:
|
||||||
target[key] = value
|
target[key] = value
|
||||||
else:
|
else:
|
||||||
recursive_dict_update(existing, value)
|
recursive_dict_update(target=existing, supply=value)
|
||||||
else:
|
else:
|
||||||
target[key] = value
|
target[key] = value
|
||||||
|
|
||||||
|
@ -343,11 +353,7 @@ def recursive_dict_keys(d):
|
||||||
all other dictionaries that appear as values within. The subkeys will use \\
|
all other dictionaries that appear as values within. The subkeys will use \\
|
||||||
to indicate their lineage.
|
to indicate their lineage.
|
||||||
|
|
||||||
{
|
{'hi': {'ho': 'neighbor'}}
|
||||||
'hi': {
|
|
||||||
'ho': 'neighbor'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
returns
|
returns
|
||||||
|
|
||||||
|
|
|
@ -1530,9 +1530,14 @@ class PhotoDB(
|
||||||
if user_config_exists:
|
if user_config_exists:
|
||||||
with open(self.config_filepath.absolute_path, 'r', encoding='utf-8') as handle:
|
with open(self.config_filepath.absolute_path, 'r', encoding='utf-8') as handle:
|
||||||
user_config = json.load(handle)
|
user_config = json.load(handle)
|
||||||
my_keys = helpers.recursive_dict_keys(config)
|
|
||||||
|
# If the default config has been updated and contains new keys,
|
||||||
|
# then they will not yet exist in the user's config, and we should
|
||||||
|
# save the file after giving it those default values.
|
||||||
|
default_keys = helpers.recursive_dict_keys(config)
|
||||||
stored_keys = helpers.recursive_dict_keys(user_config)
|
stored_keys = helpers.recursive_dict_keys(user_config)
|
||||||
needs_dump = not my_keys.issubset(stored_keys)
|
needs_dump = default_keys > stored_keys
|
||||||
|
|
||||||
helpers.recursive_dict_update(target=config, supply=user_config)
|
helpers.recursive_dict_update(target=config, supply=user_config)
|
||||||
else:
|
else:
|
||||||
needs_dump = True
|
needs_dump = True
|
||||||
|
|
Loading…
Reference in a new issue