Templating utilities
Project description
Sonotoria
Sonotoria is a library designed to provide features helping with templating and handling yaml files.
Loading Yaml
Sonotoria lets you load a yaml with variables using jinja2 syntax:
Examples
From a file
Given the file:
# test.yml
---
param: value
param2: {{ param }}
You can load the data:
>>> from sonotoria import yaml
>>> yaml.load('test.yml')
{'param': 'value', 'param2': 'value'}
From a string
You can also load a string directly:
>>> from sonotoria import yaml
>>> yaml.loads('---\nparam: value\nparam2: {{ param }}')
{'param': 'value', 'param2': 'value'}
Using context
Given the file:
# test.yml
---
param2: {{ param }}
You can load the data:
>>> from sonotoria import yaml
>>> yaml.load('test.yml', context={'param': 12})
{'param2': 12}
Using filters
Given the file:
# test.yml
---
param: value
param2: {{ param | doubled }}
You can load the data:
>>> from sonotoria import yaml
>>> yaml.load('test.yml', filters={'doubled': lambda s: s*2})
{'param': 'value', 'param2': 'valuevalue'}
Using tests
Given the file:
# test.yml
---
param: value
param2: {{ param is number }}
You can load the data:
>>> from sonotoria import yaml
>>> yaml.load('test.yml', tests={'number': lambda s: s.isdigit()})
{'param': 'value', 'param2': False}
Using objects
Given the file:
# test.yml
--- !stuff
param: value
param2: {{ param }}
You can load the data:
>>> from sonotoria import yaml
>>> class Stuff:
.... pass
>>> my_stuff = yaml.load('test.yml', types={'stuff': Stuff})
>>> my_stuff.param
value
>>> my_stuff.param2
value
You can add tests, filters and types:
Extractor
Sonotoria lets you extract data from a file using a jinja2 template.
Example
Given this input file:
That is a description
:param test: Looks like a test variable, huh
:param lol: This might be a fun variable
:param plop: Plop might just be the next best variable name
:return: Pretty much nothing, sadly
And this template file:
{{ description }}
{% for param, desc in params.items() %}
:param {{ param }}: {{ desc }}
{% endfor %}{% if return_given %}
:return: {{ return }}{% endif %}{% if rtype_given %}
:rtype: {{ rtype }}{% endif %}
You can extract data this way:
>>> import sonotoria
>>> sonotoria.extract('template.file', 'input.file')
{
'description': 'That is a description',
'params': {
'test': 'Looks like a test variable, huh',
'lol': 'This might be a fun variable',
'plop': 'Plop might just be the next best variable name'
},
'return': 'Pretty much nothing, sadly',
'rtype': None,
'return_given': True,
'rtype_given': False
}
Contributors
- Emmanuel Pluot (aka. Neomyte)
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
[Unreleased]
[0.1.8] - 2021-11-30
Changed
- fixed dump
[0.1.7] - 2021-11-30
Added
- possibility to give a context to the yaml loader
[0.1.6] - 2021-10-24
Changed
- removed print statements
[0.1.5] - 2021-10-24
Added
- Add dumpers for yaml
Changed
- fixed readme
[0.1.4] - 2021-10-24
Changed
- fixed readme
[0.1.3] - 2021-10-24
Added
- ordered
- yaml.load
- yaml.loads
- constructed
- extractor
Project details
Release history Release notifications | RSS feed
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
Hashes for sonotoria-0.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5a612496c4ae7061db9e0966bacb05ba5e60c26350f72435435960488353d6e |
|
MD5 | ab3faebfa1fc0c5f8e7cc3325ead45cf |
|
BLAKE2b-256 | 23dc323d5ada3400dbe664a675e8bfdf1dad06e53db498c62314a0aa2e78c7bb |