tr11 / python-configuration

A Python library to load configuration parameters

Home Page:https://tr11.github.io/python-configuration/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I change the config value and write it back to the config file?

programus opened this issue · comments

This is a great library and I am using it in my project.

I am using this to read 3 configs, a yml, an internal dict and an optional json. I want to use the last json file as a customizable config and allow user to change the value through UI.

How can I change the config value and write it back to the json file?

This is the code I build the ConfigurationSet:

config_list = [
    cfg_lib.config_from_yaml(util.resource_path('res/secret.yml'), read_from_file=True),
    cfg_lib.config_from_python(internal_config, separator='___'),
]

try:
    if os.path.exists(internal_config.local_config_file):
        config_list.append(cfg_lib.config_from_json(internal_config.local_config_file))
    else:
        logging.warning(f'config file {internal_config.local_config_file} does not exits yet.')
except json.JSONDecodeError:
    logging.exception(f'config file {internal_config.local_config_file} is bad json format.')
finally:
    config = cfg_lib.ConfigurationSet(*config_list)

I don't think this is possible today, but maybe we could add a diff method that would traverse the nested dicts and give you what the differences are -- that would be your "changed" config.

I'm going to close the issue for now, feel free to re-open if this is something you'd like implemented.