File Config
Easily define configs by creating a config()
decorated class…
from typing import List, Dict
import file_config
@file_config.config
class ProjectConfig(object):
@file_config.config
class Dependency(object):
name = file_config.var(str, min=1)
version = file_config.var(file_config.Regex(r"^v\d+$"))
name = file_config.var(str, min=1)
type_ = file_config.var(str, name="type", required=False)
keywords = file_config.var(List[str], min=0, max=10)
dependencies = file_config.var(Dict[str, Dependency])
config = ProjectConfig(
name="My Project",
type_="personal-project",
keywords=["example", "test"],
dependencies={
"a-dependency": ProjectConfig.Dependency(name="A Dependency", version="v12")
},
)
Configs can be dumped and loaded to various config formats…
>>> json_content = config.dumps_json()
>>> print(json_content)
{"name":"My Project","type":"personal-project","keywords":["example","test"],"dependencies":{"a-dependency":{"name":"A Dependency","version":"v12"}}}
>>> new_config = ProjectConfig.loads_json(json_content)
ProjectConfig(name='My Project', type_='personal-project', keywords=['example', 'test'], dependencies={'a-dependency': ProjectConfig.Dependency(name='A Dependency', version='v12')})
>>> assert new_config == config
and validated…
>>> config.dependencies["a-dependency"].version = "12"
>>> file_config.validate(config)
Traceback (most recent call last):
File "main.py", line 28, in <module>
file_config.validate(config)
File "/home/stephen-bunn/Git/file-config/file_config/_file_config.py", line 373, in validate
to_dict(instance, dict_type=dict), build_schema(instance.__class__)
File "/home/stephen-bunn/.local/share/virtualenvs/file-config-zZO-gwXq/lib/python3.6/site-packages/jsonschema/validators.py", line 861, in validate
cls(schema, *args, **kwargs).validate(instance)
File "/home/stephen-bunn/.local/share/virtualenvs/file-config-zZO-gwXq/lib/python3.6/site-packages/jsonschema/validators.py", line 305, in validate
raise error
jsonschema.exceptions.ValidationError: '12' does not match '^v\\d+$'
Failed validating 'pattern' in schema['properties']['dependencies']['patternProperties']['^(.*)$']['properties']['version']:
{'$id': '#/properties/dependencies/properties/version',
'pattern': '^v\\d+$',
'type': 'string'}
On instance['dependencies']['a-dependency']['version']:
'12'
To get started using this package, please see the Getting Started page!
User Documentation¶
- Getting Started
- Handlers
- Contributing
- Changelog
- 1.0.0 (2019-10-07)
- 0.3.10 (2019-10-04)
- 0.3.9 (2019-06-15)
- 0.3.8 (2019-04-01)
- 0.3.7 (2019-03-22)
- 0.3.6 (2019-03-15)
- 0.3.5 (2019-02-15)
- 0.3.4 (2019-01-11)
- 0.3.3 (2019-01-10)
- 0.3.2 (2019-01-09)
- 0.3.1 (2018-12-18)
- 0.3.0 (2018-12-16)
- 0.2.0 (2018-11-07)
- 0.1.0 (2018-10-26)
- 0.0.8 (2018-10-16)
- 0.0.7 (2018-10-12)
- 0.0.6 (2018-10-08)
- 0.0.5 (2018-10-05)
- 0.0.4 (2018-10-04)