Package: alphaconf
AlphaConfA small library to ease writing parameterized scripts.
The goal is to execute a single script and be able to overwrite the parameters
easily.
The configuration is based onOmegaConf.
Optionally, loading from toml or usingpydanticis possible.To run multiple related tasks, there is an integration withinvoke.
If you need something more complex, like running multiple instances of the
script, take a look athydra-coreor use another script
to launch multiple instances.Demo and applicationTo run an application, you need...# myapp.pyimportalphaconfimportlogging# define the default values and helpersalphaconf.setup_configuration({"server.url":"http://default",},{"server.url":"The URL to show here",})defmain():log=logging.getLogger()log.info('server.url:',alphaconf.get('server.url'))log.info('has server.user:',alphaconf.get('server.user',bool,default=False))if__name__=='__main__':alphaconf.cli.run(main)Invoking:pythonmyapp.pyserver.url=http://github.comDuring aninteractive session, you can set the application in the current
context.# import other modulesimportalphaconf.interactivealphaconf.interactive.mount()alphaconf.interactive.load_configuration_file('path')Check theDEMOfor more examples.How the configuration is loadedWhen running a program, first dotenv is used to load environment variables
from a.envfile - this is optional.Then configuration is built from:default configurations defined using (alphaconf.setup_configuration)applicationkey is generatedPYTHON_ALPHACONFenvironment variable may contain a path to loadconfiguration files from configuration directories (using application name)environment variables based on key prefixes,
except "BASE" and "PYTHON";if you have a configuration key "abc", all environment variables starting
with "ABC_" will be loaded, for example "ABC_HELLO=a" would set "abc.hello=a"key-values from the program argumentsFinally, the configuration is fully resolved and logging is configured.Configuration templates and resolversConfiguration values are resolved byOmegaConf.
Some of the resolvers (standard and custom):${oc.env:USER,me}: resolve the environment variable USER
with a default value "me"${oc.select:config_path}: resolve to another configuration value${read_text:file_path}: read text contents of a file asstr${read_bytes:file_path}: read contents of a file asbytes${read_strip:file_path}: read text contents of a file as strip spacesTheoc.selectis used to build multiple templates for configurations
by providing base configurations.
An argument--select key=templateis a shortcut forkey=${oc.select:base.key.template}.
So,logging: ${oc.select:base.logging.default}resolves to the configuration
dict defined in base.logging.default and you can select it using--select logging=default.Configuration values and integrationsTyped-configurationYou can useOmegaConfwithpydantictogettyped values.classMyConf(pydantic.BaseModel):value:int=0defbuild(self):# use as a factory pattern to create more complex objects# for example, a connection to the databasereturnself.value*2# setup the configurationalphaconf.setup_configuration(MyConf,prefix='a')# read the valuealphaconf.get('a',MyConf)v=alphaconf.get(MyConf)# because it's registered as a typeSecretsWhen showing the configuration, by default configuration keys which are
secrets, keys or passwords will be masked.
You can read values or passwords from files, by using the template${read_strip:/path_to_file}or, more securely, read the file in the codealphaconf.get('secret_file', Path).read_text().strip().Inject parametersWe can inject default values to functions from the configuration.
Either one by one, where we can map a factory function or a configuration key.
Or inject all automatically base on the parameter name.fromalphaconf.injectimportinject,inject_auto@inject('name','application.name')@inject_auto(ignore={'name'})defmain(name:str,example=None):pass# similar todefmain(name:str=None,example=None):ifnameisNone:name=alphaconf.get('application.name',str)ifexampleisNone:example=alphaconf.get('example',default=example)...Invoke integrationJust add the lines below to parameterize invoke.
Note that the argument parsing to overwrite configuration will work only
when the script is directly called.importalphaconf.invokens=alphaconf.invoke.collection(globals())alphaconf.setup_configuration({'backup':'all'})alphaconf.invoke.run(__name__,ns)Way to 1.0Run a specific functionalphaconf my.module.main:
find functions and inject argsInstall completions for bashalphaconf --install-autocompletion