improve config lookup when there are multiple possible locations

This specifically applies to all Mastodon extractors and all
extractors with a 'basecategory', i.e. 'booru', 'foolslide', etc.

Values inside those general config locations wouldn't be recognized
when a value with the same was set on the 'extractor' level.

For example 'extractor.mastodon.directory' should be used over
'extractor.directory' when both are set, but this was impossible
with the previous implementation.

(fixes #843)
This commit is contained in:
Mike Fährmann
2020-06-20 21:41:59 +02:00
parent 1b3870a4be
commit 53cc498d9c
4 changed files with 73 additions and 9 deletions

View File

@@ -108,6 +108,38 @@ def interpolate(path, key, default=None, *, conf=_config):
return default
def interpolate_common(common, paths, key, default=None, *, conf=_config):
"""Interpolate the value of 'key'
using multiple 'paths' along a 'common' ancestor
"""
if key in conf:
return conf[key]
# follow the common path
try:
for p in common:
conf = conf[p]
if key in conf:
default = conf[key]
except Exception:
return default
# try all paths until a value is found
value = util.SENTINEL
for path in paths:
c = conf
try:
for p in path:
c = c[p]
if key in c:
value = c[key]
except Exception:
pass
if value is not util.SENTINEL:
return value
return default
def set(path, key, value, *, conf=_config):
"""Set the value of property 'key' for this session"""
for p in path: