Package: config-man
Config-ManConfig-Man is a configuration manager for python projects. It helps you handle your project's runtime configurations in
an easy and clean way. It also supports multiple config sources likejson file,environment variables,hard coded defaults, etc.InstallationSimply install using pip:pipinstallconfig-manKeep in mind that Config-Man usesPython type annotations (PEP 484), andf-Strings (PEP 498)so you'll need Python 3.6 or higher.UsageDefining configIn Config-Man, configuration is defined using a class subclassed fromconfigman.ConfigMan.
In this class, configurations are static members withtype hints.Currently Config-Man only supports primitives (bool, int, float, str) and subtypes ofconfigman.ConfigMan:fromconfigmanimportConfigManclassServerConfig(ConfigMan):port:intlog_level:strFor creation of nested configs (for more organized code) simply do the following:fromconfigmanimportConfigManclassLoggingConfig(ConfigMan):log_level:strclassConfig(ConfigMan):port:intlogging:LoggingConfigYou can add default values during definition simply by assigning a value to it:fromconfigmanimportConfigManclassServerConfig(ConfigMan):port:int=80log_level:str="error"Loading configFirst of all, you need to create an instance of your main config:config=Config()Config SourcesThen you need to tell it where to look for configurations. Config-Man supports multiple config sources. Currently it
supports hard-coded, environment variables, json config files and arguments. If there is a config source that you need
and Config-Man does not support feel free to open an issue.1. hard-codedApart from the default value you set during config definition, you can add an other default value during config load
process simply by assigning the default value to it:config.port=4432. Environment VariablesConfig-Man can read configurations from environment variables.One way to use env as a source is to assign a specific env to a config:config.set_env("logging.log_level")By default all dots "." in a variable path will be replaced by double under scores "__", Sologging.log_levelwill
be filled by the value oflogging__log_level.You can also set a specific name for the env:config.set_env("logging.log_level","LOG_LEVEL")Another way is to tell Config-Man to load all possible configs from envconfig.set_auto_env()In order to avoid collisions between different programs, you can add a prefix to all envs (in auto_env):config.set_auto_env("MY_PROGRAM")Now when you load the config, Config-Man tries to readMY_PROGRAM__PORTandMY_PROGRAM__logging__log_leveland put
their values into the corresponding variables.3. Config FileCurrently Config-Man only supports json config files. You can set config file using:config.set_config_file("config.json")4. ArgumentsYou can tell Config-Man to read a specific config from arguments using:importargparseparser=argparse.ArgumentParser()config.set_arg("logging.log_level","log_level",parser)Config-Man automatically adds needed argument to parser. If necessary, you can also defineaction,help, andrequired.Loading ConfigsFinally you can load the config itself by calling:config.load()By default configs from file overrides config from env and config from args overrides everything else.If you like to do things in a different way, you can runload_from_env,load_from_fileandload_from_argsby
yourself in any order to desire.Creating an empty config fileIf you wish to create an empty config file, you can do so usingto_dict:importjsonconfig=Config()withopen("config.json","w")asf:json.dump(f,config.to_dict(),indent=2)This way config.json will contain an empty config ready for you to fill.Full exampleimportargparsefromconfigmanimportConfigManclassLoggingConfig(ConfigMan):log_level:str="error"classConfig(ConfigMan):port:intlogging:LoggingConfigconfig=Config()parser=argparse.ArgumentParser()config.port=443config.set_auto_env("MY_PROGRAM")config.set_env("logging.log_level","LOG_LEVEL")config.set_config_file("config.json")config.set_arg("logging.log_level","--log_level","-l",parser)args=parser.parse_args()config.load(args)