def _parse_args(args: list[str]) -> Box: """Parses arguments into nested dict. Args: args (list[str]): List of all arguments passed to program. Use it like this: `parse_args(sys.argv[1:])`. Args must start with one or two dashes and only contain lower case chars, period and underscores. Returns: Box: Lowercased. Box instead of dict. Already nested. Can be used just like a dictionary. Read more [here](https://github.com/cdgriffith/Box). Type casting is NOT done here. `box_dots` is `True`. """ logger.bind(args_to_parse=args).debug("Parse list of arguments.") if len(args) % 2 != 0: raise ValueError("Number of args must be not odd.") names, values = args[::2], args[1::2] cli_args_dict = {} for idx in range(len(names)): name = names[idx] value = values[idx] if name.startswith("--"): cli_args_dict[name[2:]] = value elif name.startswith("-"): cli_args_dict[name[1:]] = value return Box(settings_utils.unflatten(cli_args_dict), box_dots=True)
def test_unflatten_dotted_path_expansion_multi_overwrite(): assert settings_utils.unflatten({ "a.b.c": 12, "a.b": 10 }) == { "a": { "b": 10 } }
def test_unflatten_dotted_path_expansion_multi(): assert settings_utils.unflatten({ "a.b.c": 12, "a.d": 10 }) == { "a": { "b": { "c": 12 }, "d": 10 } }
def _parse_env_vars(all_env_vars: dict[str, str]) -> Box: """Extracts and transforms given dict of env vars. Args: all_env_vars (dict[str, str]): Environment variables. Returns: Box: Lowercased. Box instead of dict. Already nested. Can be used just like a dictionary. Read more [here](https://github.com/cdgriffith/Box). Type casting is NOT done here. `box_dots` is `True`. """ logger.bind(all_env_vars=all_env_vars).debug("Parse env vars.") env_vars = {} for name, value in all_env_vars.items(): if name.startswith("PROMAC__") and len(name) > 8: env_vars[name[8:].lower().replace("__", ".")] = value return Box(settings_utils.unflatten(env_vars), box_dots=True)
def test_unflatten_dotted_path_expansion(): assert settings_utils.unflatten({"a.b.c": 12}) == {"a": {"b": {"c": 12}}}
def test_unflatten_no_expansion(): assert settings_utils.unflatten({"a": 12}) == {"a": 12}