Skip to main content

Wrapper that combines argparse with yaml config files.

Project description

Travis Pypi

A wrapper for combining yaml configuration files and command line argument parsing. It builds upon PyYAML for parsing yaml config files and argparse for handling command line arguments and adds the clue produce one unified program configuration.

Requirements

  • Python 2.7+ or 3.2+

  • PyYAML >= 3.1

Configuration Environments

Because configuration often differs depending on the environment, e.g. production and testing settings, yconf allows you to define multiple environments in a single configuration. The current supported environemts are production, staging and development. For convenience, yconf is able to merge environment configuration to reduce redundancy, meaning that if the environment is set to staging, the configuration will inherit all settings available in a production environment and override them with staging values where neseccary.

Example

config.yml

production:
  rundir: /var/run/example
  database: /var/lib/example/db.sqlite
  loglevel: warning

staging:
  database: :memory:

development:
  loglevel: debug

Configuration

# production environment
config.rundir: /var/run/example
config.database: /var/lib/example/db.sqlite
config.loglevel: warning

# staging environment
config.rundir: /var/run/example
config.database: :memory:
config.loglevel: warning

# development environment
config.rundir: /var/run/example
config.database: :memory:
config.loglevel: debug

Both staging and development environments are optional.

If a directory is specified instead of a configuration file, yconf will look for configuration files by convention of <environment>.yml in that directory and merge them accordingly if possible.

Command Line Arguments

Extending the argument list supported by a configuration will require subclassing yconf.BaseConfiguration and overriding the makeParser method.

Example

import sys
from yconf import BaseConfiguration

class MyConfig(BaseConfiguration):

  def makeParser(self):
    parser = super(MyConfig, self).makeParser()
    parser.add_argument("-f", "--foo", dest="foo")
    return parser


config = MyConfig()
config.parse(sys.argv[1:])

The following configuration arguments are already preset:

  • -c Path to configuration file or directory

  • -e The environment used for configuration (default: production)

When present, command line argument always take precedent over configuration settings. To override nested yaml values, a dotted destination path can be set for the argument.

config.yml

logging:
  loglevel: debug

Argument

parser.add_argument("--log-level", dest="logging.loglevel")

Accessing Configuration

Configuration can be accessed either by attribute or by key.

config = MyConfig()
config.parse(args)

config.foo == config['foo']
config.foo.bar == config['foo']['bar']

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yconf-0.6.2.tar.gz (10.1 kB view details)

Uploaded Source

File details

Details for the file yconf-0.6.2.tar.gz.

File metadata

  • Download URL: yconf-0.6.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200906 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for yconf-0.6.2.tar.gz
Algorithm Hash digest
SHA256 e60844f3c46a65f645ce88d8b7db9467f86d13f6a661ae8a6b9c7062c24a648d
MD5 838f907349e29d2a39bcbe45f9546b2c
BLAKE2b-256 35fecc3188a2d88501049412dce77af81f7659d4163191d1952051e0b0325f28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page