Skip to content

Commit

Permalink
utils: Add get_config_action
Browse files Browse the repository at this point in the history
This abstracts away the functionality to resolve a given config option
to an action in a pre-defined dict.

Co-authored-by: Christian Hoffmann <[email protected]>
  • Loading branch information
pabera and hoffie committed Apr 12, 2024
1 parent 3865c5a commit aa22ebe
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/jukebox/jukebox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ def rpc_call_to_str(cfg_rpc_call: Dict, with_args=True) -> str:
readable = f"{package}.{plugin}{method_str}"
return readable

def get_config_action(cfg, section, option, default, valid_actions_dict, logger):
"""
Looks up the given {section}.{option} config option and returns
the associated entry from valid_actions_dict, if valid. Falls back to the given
default otherwise.
"""
is valid according to valid_actions_dict
action = cfg.setndefault(section, option, value='').lower()
if action not in valid_actions_dict:
logger.error(f"Config {section}.{option} must be one of {valid_actions_dict.keys()}. Using default '{default}'")
action = default
return valid_actions_dict[action]

def indent(doc, spaces=4):
lines = doc.split('\n')
Expand Down

0 comments on commit aa22ebe

Please sign in to comment.