From 318e45e2adf9e21ed8c8e8a3947c3d092436f461 Mon Sep 17 00:00:00 2001 From: Suhas Date: Sun, 31 Jan 2021 20:31:23 -0500 Subject: [PATCH] variable renames --- jrnl/args.py | 24 ++++++++++++++---------- jrnl/override.py | 14 +++++++------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/jrnl/args.py b/jrnl/args.py index c54b6816..4e7c1199 100644 --- a/jrnl/args.py +++ b/jrnl/args.py @@ -27,19 +27,23 @@ def deserialize_config_args(input: list) -> dict: :return: A single level dict of the configuration keys in dot-notation and their respective desired values :rtype: dict """ + assert len(input) == 2 runtime_modifications = {} - l = input[0] - r = input[1] - r = r.strip() - if r.isdigit(): - r = int(r) - elif r.lower() == "true": - r = True - elif r.lower() == "false": - r = False - runtime_modifications[l] = r + cfg_key = input[0] + cfg_value = input[1] + cfg_value = cfg_value.strip() + + # Convert numbers and booleans + if cfg_value.isdigit(): + cfg_value = int(cfg_value) + elif cfg_value.lower() == "true": + cfg_value = True + elif cfg_value.lower() == "false": + cfg_value = False + + runtime_modifications[cfg_key] = cfg_value return runtime_modifications diff --git a/jrnl/override.py b/jrnl/override.py index 01904710..5eb00a39 100644 --- a/jrnl/override.py +++ b/jrnl/override.py @@ -11,13 +11,13 @@ def apply_overrides(overrides: list, base_config: dict) -> dict: """ config = base_config.copy() for pairs in overrides: - k, v = list(pairs.items())[0] - nodes = k.split(".") - config = _recursively_apply(config, nodes, v) + key_as_dots, override_value = list(pairs.items())[0] + keys = key_as_dots.split(".") + config = _recursively_apply(config, keys, override_value) return config -def _recursively_apply(config: dict, nodes: list, override_value) -> dict: +def _recursively_apply(tree: dict, nodes: list, override_value) -> dict: """Recurse through configuration and apply overrides at the leaf of the config tree Credit to iJames on SO: https://stackoverflow.com/a/47276490 for algorithm @@ -29,12 +29,12 @@ def _recursively_apply(config: dict, nodes: list, override_value) -> dict: """ key = nodes[0] if len(nodes) == 1: - config[key] = override_value + tree[key] = override_value else: next_key = nodes[1:] - _recursively_apply(_get_config_node(config, key), next_key, override_value) + _recursively_apply(_get_config_node(tree, key), next_key, override_value) - return config + return tree def _get_config_node(config: dict, key: str):