Package: generalize-config
generalize-configRead and generalize the configurationOverviewA list of variables required for a program's execution environment can be obtained in several ways:Environment VariablesConfiguration filesCommand-line argumentsetc ...Especially if you're deploying as a container,
you need to reference a file like/run/secrets/...(docker swarm secret)
to read the variable.I developed thegeneralize-configlibrary because I needed a way to unify environment variables from multiple places into one.FeaturesAll results are returned asargparse.Namespace.json,yaml,cfgfiles are supported.argparse.Namespacemerge.Environment variable filtering and file reader.Installationpipinstallgeneralize-configUsageConfiguration fileReading extension-based configuration filesenvs:host:localhostport:8080fromargparseimportNamespacefromgeneralize_configimportread_config_fileconfig=read_config_file("/path/config/file.yml","envs",encoding="utf-8")assertisinstance(config,Namespace)assert"localhost"==config.hostassert8080==config.portThe supported extension types are:YAML extensions:.yml,.yamlJSON extensions:.jsonCFG extensions:.cfg,.ini:warning: When using a CFG file, there must be 1 subsection argument.Environment variableEnvironment Variable FilteringfromargparseimportNamespacefromgeneralize_configimportread_os_envs# APP_HTTP_HOST_VALUE=localhostconfig=read_os_envs(prefix="APP_",suffix="_VALUE")assertisinstance(config,Namespace)assert"localhost"==config.http_hostFilter environment variables and read files:fromargparseimportNamespacefromgeneralize_configimportread_os_envs_file# APP_DATABASE_PASSWORD_FILE=/run/secrets/passwordconfig=read_os_envs_file(prefix="APP_",suffix="_FILE")assertisinstance(config,Namespace)assertisinstance(config.database_password,str)Merge namespacesfromargparseimportNamespace,ArgumentParserfromgeneralize_configimportmerge_left_firstparser=ArgumentParser()# add_argument ...args=parser.parse_known_args()[0]result=Namespace()merge_left_first(result,args,...)print(result)Generalized methodThe recommended reading order is:Fixed variable (higher priority)Command line argumentsConfiguration file received as a command line argumentEnvironment VariablesConfiguration file received as environment variableEnvironment variable pointing to fileDefault variable (low priority)The function that implements the above isread_generalize_configs:fromargparseimportArgumentParser,Namespacefromgeneralize_configimportread_generalize_configsparser=ArgumentParser()# add_argument ...default_args=Namespace(...)result=read_generalize_configs(parser=parser,subsection="application",env_prefix="ENV_",env_suffix="_FILE",config_key="config",default=default_args,)print(result)Things to knowWhen usingargparse.ArgumentParseryou need to make sure that all values not entered are returned asNone.
Otherwise, themerge_left_firstfunction may malfunction.Also, variables acquired through CFG files and environment variables are
fixed asstringtype. To solve this you need to deserialize like this:Installtype-serialize:pipinstalltype-serializeAdd type annotation toNamespaceand then calldeserialize:fromargparseimportNamespacefromgeneralize_configimportread_generalize_configsfromtype_serializeimportdeserializeclassConfig(Namespace):host:strport:int# ...ns=read_generalize_configs(...)config=deserialize(ns,Config)assertisinstance(config,Config)print(config)LicenseSee theLICENSEfile for details. In summary,generalize-configis licensed under theMIT license.