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.86.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: scrippy-template-1.1.86.tar.gz
  • Upload date:
  • Size: 6.1 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.86.tar.gz
Algorithm Hash digest
SHA256 15b2fe0dca2733262142ba88e1317547abfbbc93c4649b16389be84b0e0e107a
MD5 6f40cf8e32e0e9bfca788e84ba3b24e7
BLAKE2b-256 8336b8c0a71e08dc32f2591a5539db128280ea2c2619355ce74bbaf3f69fc3bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for scrippy_template-1.1.86-py3-none-any.whl
Algorithm Hash digest
SHA256 e9234e8399dd65de1b2626ccd41a94b7431882452b6bc798b70a97bd6b868248
MD5 ffbfb45371396530b0a17908c08a15da
BLAKE2b-256 7087a9abb240ecf2974303b0e161bb45756b6e2fc9ce3754d6d60beef8af363c

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