Skip to content

Commit

Permalink
Fix 'get' implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cphyc committed Nov 25, 2020
1 parent 323793d commit c37e1e5
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions yt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,41 +110,41 @@ def __init__(self, defaults=None):
self.values = {}
self.update(defaults)

def get(self, section, *path, strict=True, **kwargs):
def get(self, section, *option, strict=True, **kwargs):
config = self.values[section]
*option_path, option_name = option

# This works as follow: if we try to access
# field > gas > density > lognorm
# we try in this order:
# field > gas > density > lognorm
# field > gas > lognorm
# field > lognorm
if len(path) == 0:
return config

ok = False
node = None
node = {}
use_fallback = "fallback" in kwargs
fallback = kwargs.pop("fallback", None)
while len(path) > 0:
first_pass = True
while len(option_path) > 0 or first_pass:
first_pass = False
try:
node = config
for k in path:
for k in option_path:
node = node[k]
ok = True
break
return node[option_name]
except KeyError as e:
if strict and not use_fallback:
raise e
else:
path = path[:-1]
# Shorten the path and try again
option_path = option_path[:-1]

if not ok and use_fallback:
if use_fallback:
return fallback
elif not ok:
raise KeyError(f"Could not find {section}, {path} in configuration.")

return node
else:
raise KeyError(
f"Could not find {section}:{'.'.join(option)} " "in configuration."
)

def update(self, new_values):
def copy_helper(dict_a, dict_b):
Expand Down

0 comments on commit c37e1e5

Please sign in to comment.