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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sonotoria-0.1.9-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file sonotoria-0.1.9.tar.gz.

File metadata

  • Download URL: sonotoria-0.1.9.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1

File hashes

Hashes for sonotoria-0.1.9.tar.gz
Algorithm Hash digest
SHA256 29b121ddc1c865321b7a8f0ec732051ba9d9eeceac04ba75eddc00cfdd63d7e4
MD5 cd17798dca21963c545edb83211cb2ae
BLAKE2b-256 b5eeb55ab16fcc48371581ea883e1614f50fbee9d26c5f7b33fd88687cfa6e11

See more details on using hashes here.

File details

Details for the file sonotoria-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: sonotoria-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1

File hashes

Hashes for sonotoria-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 8d3ad373168bbf32540eeb23c7d09223d8a15ff7249cf270ca76b82e2d1b1789
MD5 5c5e44f260dab04671e056c8aa8a08c5
BLAKE2b-256 6021fd21afea5e42626ffac422ca98b3b4b12012579b5e5e6b8c5d88f7980130

See more details on using hashes here.

Supported by

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