Skip to main content

"Gestion simplifiée des fichiers modèles"

Project description

Build Status License Language

Scrippy, my scrangourou friend

scrippy_template

Simplified management of template files for the Scrippy framework.

Prerequisites

Python modules

List of required modules

The modules listed below will be automatically installed.

  • jinja2

Installation

Manual

git clone https://codeberg.org/scrippy/scrippy-template.git
cd scrippy-template
python -m pip install -r requirements.txt
make install

With pip

pip install scrippy-template

Usage

This module allows document generation from template files using the jinja2 rendering engine.

To be usable, template files must be located in the directory defined by the base_path parameter passed to the template.Renderer object.

To handle variable interpolation, the template file MUST accept a dictionary named params as a parameter.

This dictionary must contain all the variables necessary for the complete rendering of the template file.

The rendering of the template file is obtained from the template.Renderer object, whose instantiation requires the name of a template file and the base path in which to search for the template file.

Environment-specific parameters for Jinja2 can be modified through the env attribute of the template.Renderer object.

By convention, the template files used by the Scrippy framework are stored in the directory defined by the env::templatedirdir configuration parameter (see the configuration of the Scrippy framework in the relevant documentation).

A template is a simple text file with certain duly marked passages interpolated by the variables passed as parameters.

The Renderer.render() method returns the rendering of the template file.

The variables must be supplied to the template file as a dictionary named params.

The dictionary will be passed to the template file which will be responsible for interpolating the variables it contains.

Template files can include:

  • control structures
  • loops

Simple template file

With the following template file named template_test.mod located in the directory /var/scrippy/templates:

Hello {{params.user}}

You received this email because you are a member of the functional administrators of {{params.app}}.

The {{params.script}} script execution on {{params.date}} ended with the following error code:
- {{params.error.code}}: {{params.error.msg}}

--
Regards.
{{params.sender}}

The template file can be used as follows:

import datetime
from scrippy_template import template

params = {"user": "Harry Fink",
          "app": "Flying Circus",
          "script": "dead_parrot.py",
          "date": datetime.datetime.now().strftime("%Y/%m/%d"),
          "error": {"code": 42,
                    "msg": "It’s not pinin’! It’s passed on! This parrot is no more!"},
          "sender": "Luiggi Vercotti", }

base_path = '/var/lib/scrippy/templates'
template_file = 'template.j2'
renderer = template.Renderer(base_path, template_file)

print(renderer.render(params))

With the default values, the message displayed at the end of the script will contain:

Hello Harry Fink

You received this email because you are a member of the functional administrators of Flying Circus.

The  dead_parrot.py script execution on 2019/09/15 ended with the following error code:
- 42: It’s not pinin’! It’s passed on! This parrot is no more!

--
Regards.
Luigi Vercotti

Template file with control structures

params = {"user": "Harry Fink",
          "app": "Flying Circus",
          "script": "dead_parrot.py",
          "date": datetime.datetime.now().strftime("%Y/%m/%d"),
          "num_error": 42,
          "sender": "Luiggi Vercotti", }
Hello {{params.user}}

You received this email because you are a member of the functional administrators of {{params.app}}.

The {{params.script}} script execution on {{params.date}} ended
{% if params.num_errors == 0 %}
- without error
{% else %}
 with {{params.num_errors}} error(s)
{% endif %}

--
Regards.
{{params.sender}}

Template file with loop

params = {"user": "Harry Fink",
          "app": "Flying Circus",
          "script": "dead_parrot.py",
          "date": datetime.datetime.now().strftime("%Y/%m/%d"),
          'errors': [{'code': 2, 'msg': "It's not pinin’! It's passed on! This parrot is no more!"},
                     {'code': 3, 'msg': "Ohh! The cat's eaten it."}],
          "sender": "Luiggi Vercotti", }
Hello {{params.user}}

You received this email because you are a member of the functional administrators of {{params.app}}.

The {{params.script}} script execution on {{params.date}} ended with the following errors:
{% for error in params.errors %}
- {{ error.code }}: {{ error.msg}}
{% endfor %}

--
Regards.
{{params.sender}}

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

scrippy-template-1.1.87.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

scrippy_template-1.1.87-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file scrippy-template-1.1.87.tar.gz.

File metadata

  • Download URL: scrippy-template-1.1.87.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for scrippy-template-1.1.87.tar.gz
Algorithm Hash digest
SHA256 6246390dafa005c14503d8059c8bccdd6a0d213367c2a96257706557233daad6
MD5 3ce74778f356ed313dbafcb8e3cc751f
BLAKE2b-256 907249d6b0dbd2b94ea30ab7c4f9396e420904ddb8dcaf1525351cc61cd1fc87

See more details on using hashes here.

File details

Details for the file scrippy_template-1.1.87-py3-none-any.whl.

File metadata

File hashes

Hashes for scrippy_template-1.1.87-py3-none-any.whl
Algorithm Hash digest
SHA256 a42994f50ef01cf3882976189bd675111eb4677886dd5e29ce7b2d0670275243
MD5 b331866d345c249a51a0943e2f5a964b
BLAKE2b-256 19d8c96cba1ea9e00fd28f39d47a7109ee1a50898ba582cdedecbbb53ab1e551

See more details on using hashes here.

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