Package: resconfig
resconfigresconfigis a minimalistic application configuration library for
Python. It is a thin wrapper around nesteddictobjects with added
features that make it easy to deal with the data structure as a
centralized storage of application configuration.ResConfigsupportsmultiple configuration file formats: INI, JSON, TOML, and YAML;environment variables: Configuration can be easily overridden with
environment variables;command-line arguments: Configuration can be easily overridden with
ArgumentParser command-line arguments.“.”-delimited nested keys:config["foo.bar"]is equivalent toconfig["foo"]["bar"].The advanced usage ofResConfigallows:Dynamic reloading of configuration at run time: Watch functions can
be attached to any keys within the configuration to trigger actions
to manage resources.For the full documentation, visitdocumentation.Installation$pipinstallresconfigQuickstartLet us first create anResConfigobject with a simple default
configuration for your application,myapp.py:fromresconfigResConfigconfig=ResConfig({"db":{"host":"localhost","port":5432}})By default,ResConfigloads configuration immediately after its
initialization. To control the timing of load, use theload_on_initflag:config=ResConfig({"db":{"host":"localhost","port":5432}},load_on_init=False)config.load()The following sections introduce you to the basic usage ofResConfigobject.The “.”-Style Key NotationResConfigexposesdict-like interface for value access but
additionally allows the “.”-style notation for nested keys. The
following methods all return the same value,localhost:host=config["db"]["host"]host=config["db.host"]host=config.get("db.host")# similar to dict.getThe “.”-style can be used elsewhere, e.g.,config=ResConfig({"db.host":"localhost","db.port":5432})This will be the same default configuration shown
earlier.ResConfigtakes care of nesting thedictfor you.Use with Configuration FilesTo read configuration from (multiple) files, supply a list of paths on
object initialization:config=ResConfig({"db.host":"localhost","db.port":5432},config_files=["myconf.yml","~/.myconf.yml,"/etc/myconf.yml"])If any of the files exists, they are read in the reverse order, i.e.,/etc/myconf.yml,~/.myconf.yml, and thenmyconf.yml, and the
configuration read from them get merged in that order, overriding the
default. This allows layered configuration based on specificity by
filesystem location.Use with Environment VariablesProperly named environment variables can override default
configuration. When you run yourmyapp.pyapp with theDB_HOSTand/orDB_PORTenvironment variables set, their values override
the default:$DB_HOST=remotehostDB_PORT=3306pythonmyapp.pyThat is,config["db.host"]andconfig["db.port"]will returnremotehostand3306, respectively. As a rule of thumb, a
configuration key maps to an uppercased, “_”-delimited (when nested)
environment variable name.Use with ArgumentParserAResConfigobject can dynamically generateargparse.ArgumentParserarguments from default configuration:parser=argparse.ArgumentParser()parser.add_argument(...)# Define other argumentsconfig.add_arguments_to_argparse(parser)# --pg-host and --pg-port arguments are now availableAfter actually parsing the (command-line) arguments, pass the parse
result toResConfigand then load the configuration:args=parser.parse_args()config.prepare_from_argparse(args)config.load()Adding Actions on ChangesAResConfigobject is aware of changes to its
configuration.Watch functionswatch changes happening at any nested
key to act on them:fromresconfigimportAction@config.watch("db.host")defact_on_nested_key(action,old,new):ifaction==Action.ADDED:# db.host addedelifaction==Action.MODIFIED:# db.host modifiedelifaction==Action.RELOADED:# db.host reloadedelifaction==Action.REMOVED:# db.host removedHere, theact_on_nested_keyfunction is called whenever
configuration changes occur atdb.hostand can decide what to do
with theoldand/ornewvalues.Development$pipinstall-e.[dev]$pre-commitinstallRunning Tests$pythonsetup.pytestsLicenseApache License, Version 2.0