Skip to main content

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 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)

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

sonotoria-0.1.5.tar.gz (9.5 kB view hashes)

Uploaded Source

Built Distribution

sonotoria-0.1.5-py3-none-any.whl (12.2 kB view hashes)

Uploaded Python 3

Supported by

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