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

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

Details for the file scrippy_template-1.1.88.tar.gz.

File metadata

  • Download URL: scrippy_template-1.1.88.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for scrippy_template-1.1.88.tar.gz
Algorithm Hash digest
SHA256 20cac57c3e154dd6291168e619e051cd1d85bf646865296fca8f6af5400f1a63
MD5 862aa933d4b1658fd143a07019ff46f7
BLAKE2b-256 d975017f5becd6880c33d8f822dbe7de49232e32a59582f70d244e578238e6b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for scrippy_template-1.1.88-py3-none-any.whl
Algorithm Hash digest
SHA256 c294dd30a0f33cf57a3cf5f51803edf29bafeb87ff8ee307f82deaf4bc5c53e1
MD5 d4dc1ceb1b9d7735559bafce03c5a36e
BLAKE2b-256 ad274d5d2e368419fd652fd92a54929be62566399ceeac9b4b842ab9a2b07e0f

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