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 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.9] - 2021-11-30

Changed

  • fixed dump (again :p)

[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


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.9.tar.gz (10.4 kB view hashes)

Uploaded Source

Built Distribution

sonotoria-0.1.9-py3-none-any.whl (12.7 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