Move this config prep code into voussoirkit as configlayers.

This commit is contained in:
voussoir 2020-04-03 17:09:01 -07:00
parent 9c96522cfc
commit 930960e22a
2 changed files with 6 additions and 63 deletions

View file

@ -341,50 +341,6 @@ def read_filebytes(filepath, range_min=0, range_max=None, chunk_size=bytestring.
yield chunk
sent_amount += len(chunk)
def recursive_dict_update(target, supply):
'''
Update target using supply, but when the value is a dictionary update the
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():
if isinstance(value, dict):
existing = target.get(key, None)
if existing is None:
target[key] = value
else:
recursive_dict_update(target=existing, supply=value)
else:
target[key] = value
def recursive_dict_keys(d):
'''
Given a dictionary, return a set containing all of its keys and the keys of
all other dictionaries that appear as values within. The subkeys will use \\
to indicate their lineage.
{'hi': {'ho': 'neighbor'}}
returns
{'hi', 'hi\\ho'}
'''
keys = set(d.keys())
for (key, value) in d.items():
if isinstance(value, dict):
subkeys = {f'{key}\\{subkey}' for subkey in recursive_dict_keys(value)}
keys.update(subkeys)
return keys
def remove_characters(text, characters):
translator = {ord(c): None for c in characters}
text = text.translate(translator)

View file

@ -10,6 +10,7 @@ import tempfile
import time
from voussoirkit import cacheclass
from voussoirkit import configlayers
from voussoirkit import expressionmatch
from voussoirkit import pathclass
from voussoirkit import ratelimiter
@ -1621,27 +1622,13 @@ class PhotoDB(
yield thing
def load_config(self):
config = copy.deepcopy(constants.DEFAULT_CONFIGURATION)
user_config_exists = self.config_filepath.is_file
needs_dump = False
if user_config_exists:
with open(self.config_filepath.absolute_path, 'r', encoding='utf-8') as handle:
user_config = json.load(handle)
# 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)
needs_dump = default_keys > stored_keys
helpers.recursive_dict_update(target=config, supply=user_config)
else:
needs_dump = True
(config, needs_rewrite) = configlayers.load_file(
filepath=self.config_filepath,
defaults=constants.DEFAULT_CONFIGURATION,
)
self.config = config
if needs_dump:
if needs_rewrite:
self.save_config()
def save_config(self):