Package: vyper-config
VyperPython configuration with (more) fangs! Python port of the very awesomeViperfor Go.PyPI name changeThe package changed name on pypi fromvypertovyper-configon August 20th 2018.
Thevypername is now used by the followingproject.
Thevyper-configpackage is available onPyPI.What is Vyper?Vyper is a complete configuration solution for Python applications including 12 factor apps. It is designed
to work within an application, and can handle all types of configuration needs and formats. It supports:setting defaultsreading from JSON, TOML, and YAML config fileslive watching and re-reading of config files (optional)reading from environment variablesreading from remote config systems (etcd, Consul or ZooKeeper)live watching and re-reading of remote config files (optional)reading from command line argumentsreading from buffersetting explicit valuesVyper can be thought of as a registry for all of your applications
configuration needs.Why Vyper?When building a modern application, you don’t want to worry about
configuration file formats; you want to focus on building awesome software.
Vyper is here to help with that.Vyper does the following for you:Find, load, and unmarshall a configuration file in JSON, TOML, or YAML format.Provide a mechanism to set default values for your different
configuration options.Provide a mechanism to set override values for options specified through
command line arguments.Provide an alias system to easily rename parameters without breaking existing
code.Make it easy to tell the difference between when a user has provided a
command line or config file which is the same as the default.Vyper uses the following precedence order. Each item takes precedence over the
item below it:explicit call to setargumentenvironment variableconfigkey/value storedefaultVyper configuration keys are case insensitive.Putting Values into VyperEstablishing DefaultsA good configuration system will support default values. A default value is not
required for a key, but it's useful in the event that a key hasn't been set via
config file, environment variable, remote configuration or argument.Examples:v.set_default('ContentDir','content')v.set_default('LayoutDir','layouts')v.set_default('Taxonomies',{'tag':'tags','category':'categories'})Reading Config FilesVyper requires minimal configuration so it knows where to look for config files.
Vyper supports JSON, TOML and YAML files. Vyper can search multiple paths, but
currently a single Vyper instance only supports a single configuration file.
Vyper does not default to any configuration search paths leaving defaults decision
to an application.Here is an example of how to use Vyper to search for and read a configuration file.
None of the specific paths are required, but at least one path should be provided
where a configuration file is expected.v.set_config_name('config')# name of config file (without extension)v.add_config_path('/etc/appname/')# path to look for the config file inv.add_config_path('$HOME/.appname')# call multiple times to add many search pathsv.add_config_path('.')# optionally look for config in the working directoryv.read_in_config()# Find and read the config fileWatching and re-reading config filesVyper supports the ability to have your application live read a config file while running.Gone are the days of needing to restart a server to have a config take effect,
vyper powered applications can read an update to a config file while running and
not miss a beat.Simply tell the Vyper instance to watch_config().
Optionally you can provide a function for Vyper to run each time a change occurs.Make sure you add all of the config_paths prior to callingwatch_config()v.watch_config()deff():print('Config file changed')v.on_config_change(f)Reading Config from bufferVyper pre-defines many configuration sources such as files, environment
variables, arguments, and remote K/V store, but you are not bound to them. You can
also implement your own required configuration source and feed it to Vyper.v.set_config_type('yaml')# or v.set_config_type('YAML')# any approach to require this configuration into your program.yaml_example='''Hacker: truename: stevehobbies:- skateboarding- snowboarding- goclothing:jacket: leathertrousers: denimage: 35eyes : brownbeard: true'''v.read_config(yaml_example)v.get('name')# this would be 'steve'Setting OverridesThese could be from a command line argument, or from your own application logic.v.set('Verbose',True)v.set('LogFile',log_file)Registering and Using AliasesAliases permit a single value to be referenced by multiple keysv.register_alias('loud','Verbose')v.set('verbose',True)# same result as next linev.set('loud',True)# same result as prior linev.get_bool('loud')# Truev.get_bool('verbose')# TrueWorking with Environment VariablesVyper has full support for environment variables. This enables 12 factor
applications out of the box. There are four methods that exist to aid working
with ENV:automatic_env()bind_env(string)set_env_prefix(string)set_env_replacer(string)When working with ENV variables, it’s important to recognize that Vyper
treats ENV variables as case sensitive.Vyper provides a mechanism to try to ensure that ENV variables are unique. By
usingset_env_prefix(), you can tell Vyper to use add a prefix while reading from
the environment variables. Bothbind_env()andautomatic_env()will use this
prefix.bind_env()takes one or two parameters. The first parameter is the key name, the
second is the name of the environment variable. The name of the environment
variable is case sensitive. If the ENV variable name is not provided, then
Vyper will automatically assume that the key name matches the ENV variable name,
but the ENV variable is IN ALL CAPS. When you explicitly provide the ENV
variable name, itdoes notautomatically add the prefix.One important thing to recognize when working with ENV variables is that the
value will be read each time it is accessed. Vyper does not fix the value when
thebind_env()is called.automatic_env()is a powerful helper especially when combined withset_env_prefix(). When called, Vyper will check for an environment variable any
time av.get()request is made. It will apply the following rules. It will
check for a environment variable with a name matching the key uppercased and
prefixed with theenv_prefix()if set.set_env_replacer()allows you to use astrobject to rewrite Env
keys to an extent. This is useful if you want to use-or something in yourget()calls, but want your environmental variables to use_delimiters. An
example of using it can be found intests/test_vyper.py.Env examplev.set_env_prefix('spf')# will be uppercased automaticallyv.bind_env('id')os.environ['SPF_ID']='13'# typically done outside of the appid=v.get('id')# 13Working with command line argumentsVyper has the ability to bind to command line arguments.
Specifically, Vyper supportsargparse.
Seedocfor more details.The values are set when the binding method is called.As it deals with command line arguments, thebind_args()method needs to be called passing
an instance of argparse.ArgumentParser(). The method also sets defaults based on what you pass
viaadd_argument()defaultparameter.Note: If you don't specify a default, the values will be set toNone.p=argparse.ArgumentParser(description="Application settings")p.add_argument('--app-name',type=str,help='Application and process name')p.add_argument('--env',type=str,choices=['dev','pre-prod','prod'],help='Application env')p.add_argument('--port',type=int,default=5000,help='Application port')p.add_argument('--password',type=str,help='Application password')v.bind_args(p)# "your_app.py", "--app-name=cmd-app", "--env=prod"app_name=v.get('app_name')# 'cmd-app'env=v.get('env')# 'prod'port=v.get('port')# 5000password=v.get('password')# `None`Getting Values From VyperIn Vyper, there are a few ways to get a value depending on the value's type.
The following functions and methods exist:get(key)get_bool(key) : boolget_float(key) : floatget_int(key) : intget_string(key) : stris_set(key) : boolOne important thing to recognize is that each get function will return a zero
value if it’s not found. To check if a given key exists, theis_set()method
has been provided.Example:v.get_string('logfile')# case-insensitive Setting & Gettingifv.get_bool('verbose'):print('verbose enabled')Accessing nested keysThe accessor methods also accept formatted paths to deeply nested keys. For
example, if the following JSON file is loaded:{"host":{"address":"localhost","port":5799},"datastore":{"metric":{"host":"127.0.0.1","port":3099},"warehouse":{"host":"198.0.0.1","port":2112}}}Vyper can access a nested field by passing a.delimited path of keys:v.get_string('datastore.metric.host')# returns '127.0.0.1'This obeys the precedence rules established above; the search for the root key
(in this example,datastore) will cascade through the remaining configuration
registries until found. The search for the sub-keys (metricandhost),
however, will not.For example, if themetrickey was not defined in the configuration loaded
from file, but was defined in the defaults, Vyper would return the zero value.On the other hand, if the primary key was not defined, Vyper would go through
the remaining registries looking for it.Lastly, if there exists a key that matches the delimited key path, its value
will be returned instead. E.g.{"datastore.metric.host":"0.0.0.0","host":{"address":"localhost","port":5799},"datastore":{"metric":{"host":"127.0.0.1","port":3099},"warehouse":{"host":"198.0.0.1","port":2112}}}v.get_string('datastore.metric.host')#returns'0.0.0.0'Vyper or Vypers?Vyper comes ready to use out of the box. There is no configuration or
initialization needed to begin using Vyper. Since most applications will want
to use a single central repository for their configuration, the vyper package
provides this. It is similar to a singleton.In all of the examples above, they demonstrate using vyper in it's singleton
style approach.