Loads, validates and normalizes configuration.
Project description
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 uses glorpen.config.loaders to allow loading data from different sources.
Loaders should accept:
path, filepath constructor argument
file-like object, fileobj constructor argument
Additionally you can just pass dict data to config with 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.simple.String fields.
Normalization and validation
Each field type has own normalization rules, eg. for glorpen.config.fields.log.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:
import logging
import glorpen.config.fields.simple as f
from glorpen.config.fields.log import LogLevel
project_path = "/tmp/project"
spec = f.Dict({
"project_path": f.Path(default=project_path),
"project_cache_path": f.Path(default="{{ project_path }}/cache"),
"logging": LogLevel(default=logging.INFO),
"database": f.String(),
"sources": f.Dict({
"some_param": f.String(),
"some_path": f.Path(),
}),
"maybe_string": f.Variant([
f.String(),
f.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:
from glorpen.config import Config
import glorpen.config.loaders as loaders
loader = loaders.YamlLoader(filepath=config_path)
cfg = Config(loader=loader, 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.base.Field or glorpen.config.fields.base.FieldWithDefault.
glorpen.config.fields.base.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.base.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.base.ResolvableObject. You can get real value by using glorpen.config.fields.base.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
Built Distribution
File details
Details for the file glorpen-config-2.2.0.tar.gz
.
File metadata
- Download URL: glorpen-config-2.2.0.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bdeaaf0ba582397f2fb7231273139da61f30d1ca729ce4a4a44575b3d1e83f6 |
|
MD5 | 7931b6f9987f45cfd834874b73f69890 |
|
BLAKE2b-256 | af14162f6791c3fc5a3e833222bf03f86203135c379f059f5e3f727a2ec963d8 |
File details
Details for the file glorpen_config-2.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: glorpen_config-2.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbdbedf0370af840a06fb3268debf63ad6cc3811cadda55eaba5a22246109b18 |
|
MD5 | 141a999aafa177bbd821103abf2cce6a |
|
BLAKE2b-256 | b210f9a0b81864e0162c127e12c39936ff1b6ab7a9e8cc62f336412a2b962888 |