Skip to main content

Loads, validates and normalizes configuration.

Project description

https://travis-ci.org/glorpen/glorpen-config.svg?branch=master https://readthedocs.org/projects/glorpen-config/badge/?version=latest

Config framework for Your projects - with validation, interpolation and value normalization!

Full documentation: https://glorpen-config.readthedocs.io/

Official repositories

GitHub: https://github.com/glorpen/glorpen-config

BitBucket: https://bitbucket.org/glorpen/glorpen-config

Features

You can:

  • create custom fields for custom data

  • define configuration schema inside Python app

  • convert configuration values to Python objects

  • validate configuration

  • use interpolation to fill config values

  • set default values

Loading data

glorpen.config.Config class allows loading data from three sources:

  • path, filepath constructor argument

  • file-like object, fileobj constructor argument

  • dict object passed to glorpen.config.Config.load_data or glorpen.config.Config.finalize.

Interpolation

You can reuse values from config with {{ path.to.value }} notation, eg:

project:
   path: "/tmp"
   cache_path: "{{ project.path }}/cache"

String interpolation currently can be used only with glorpen.config.fields.String fields.

Normalization and validation

Each field type has own normalization rules, eg. for glorpen.config.fields.LogLevel:

logging: DEBUG

config.get("logging") would yield value 10 as is logging.DEBUG.

Additionally it will raise glorpen.config.exceptions.ValidationError if invalid level name is given.

Default values

Each field can have default value. If no value is given in config but default one is set, it will be used instead.

Default values adhere to same interpolation and normalization rules - each default value is denormalized and then passed to normalizers. That way complex object can still profit from config interpolation. There should not be any real impact on performance as it is done only once.

Example usage

Your first step should be defining configuration schema:

from glorpen.config import Config
from glorpen.config.fields import Dict, String, Path, LogLevel

project_path = "/tmp/project"

spec = Dict({
   "project_path": Path(default=project_path),
   "project_cache_path": Path(default="{{ project_path }}/cache"),
   "logging": Dict({
       "level": LogLevel(default=logging.INFO)
   }),
   "database": String(),
   "sources": Dict(
       "some_param": String(),
       "some_path": Path(),
   ),
   "maybe_string": Variant([
       String(),
       Number()
   ])
})

Example yaml config:

logging: "DEBUG"
database: "mysql://...."
sources:
   some_param: "some param"
   some_path: "/tmp"
maybe_string: 12

Then you can create glorpen.config.Config instance:

cfg = Config(filepath=config_path, spec=spec).finalize()

cfg.get("sources.some_param") #=> "some param"
cfg.get("project_path") #=> "/tmp/project"
cfg.get("project_cache_path") #=> "/tmp/project/cache"
cfg.get("logging") #=> 10
cfg.get("maybe_string") #=> 12

Creating custom fields

Custom field class should extend glorpen.config.fields.Field or glorpen.config.fields.FieldWithDefault.

glorpen.config.fields.Field.make_resolvable method should register normalizer functions which later will be called in registration order. Each value returned by normalizer is passed to next one. After chain end value is returned as config value.

Returned glorpen.config.fields.ResolvableObject instance is resolved before passing it to next normalizer.

If value passed to normalizator is invalid it should raise glorpen.config.exceptions.ValidationError. Sometimes value can be lazy loaded - it is represented as glorpen.config.fields.ResolvableObject. You can get real value by using glorpen.config.fields.resolve(value, config).

class MyValue(object):
   def __init__(self, value):
      super(MyValue, self).__init__()
      self.value = value

class MyField(Field):

    def to_my_value(self, value, config):
        return MyValue(value)

    def is_value_supported(self, value):
        return True

    def make_resolvable(self, r):
        r.on_resolve(self.to_my_value)

The last thing is to use prepared custom field in configuration spec.

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

glorpen-config-2.0.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

glorpen_config-2.0.0-py2.py3-none-any.whl (10.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file glorpen-config-2.0.0.tar.gz.

File metadata

  • Download URL: glorpen-config-2.0.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for glorpen-config-2.0.0.tar.gz
Algorithm Hash digest
SHA256 a74be8d39f3bc012acbd25f7ed54e19ce7a70e0008691169df0f669895f3e5ef
MD5 4af6ed8025aacc222bd2adbd8e672125
BLAKE2b-256 5c02bd2699b6775d9d56f5ef08830cbeb70efeb0f22a66c7fbb78eb3e35d2b53

See more details on using hashes here.

File details

Details for the file glorpen_config-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: glorpen_config-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for glorpen_config-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 317db8e43239c811c1bdbf9ef09a9e9176f6fc9ef44e71f63005c80fc06d2114
MD5 daab2a5245d67946e9f886682c494311
BLAKE2b-256 7d97fd90426c24fdf56610f0e57b6f73fe79374d0bc88e5c9790ba2d6a8524e6

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