[PYTHON] Use Jinja2 for PasteScript template engine

PasteScript

PasteScript is a command line tool that provides the following two functions. This time, we will talk about paster create.

--Create a project template based on the specified template (you can create your own) (paster create) -Launch the WSGI application (including WSGI server and related middleware settings) set by PasteDeploy (paster serve)

The bottleneck was that it didn't support Python 3 for a long time, but PasteScript 2.0 released last month finally supports Python 3.

paster create command

The following is an example of specifying the included basic_package template. The basic_package template creates a Python package project that includes setup.py.

$ paster create -t basic_package test
Selected and implied templates:
  PasteScript#basic_package  A basic setuptools-enabled package

Variables:
  egg:      test
  package:  test
  project:  test
Enter version (Version (like 0.1)) ['']: 0.1
Enter description (One-line description of the package) ['']: test
Enter long_description (Multi-line description (in reST)) ['']: test
Enter keywords (Space-separated keywords/tags) ['']: python
Enter author (Author name) ['']: FGtatsuro
Enter author_email (Author email) ['']:
Enter url (URL of homepage) ['']:
Enter license_name (License name) ['']:
Enter zip_safe (True/False: if the package can be distributed as a .zip file) [False]:
Creating template basic_package
Creating directory ./test
  Recursing into +package+
    Creating ./test/test/
    Copying __init__.py to ./test/test/__init__.py
    Recursing into __pycache__
      Creating ./test/test/__pycache__/
  Copying setup.cfg to ./test/setup.cfg
  Copying setup.py_tmpl to ./test/setup.py
Running /Users/tatsuro/.homesick/repos/dotfiles/home/.virtualenvs/flask-boilerplate/bin/python setup.py egg_info
# setup.Python package project containing py
$ ls test
setup.cfg     setup.py      test          test.egg-info

You can create your own template. I will not explain the details (see Documentation), but the general flow is as follows.

  1. Create a Python package project that defines the template.
  2. Create a class that inherits the paste.script.templates.Template class (hereinafter referred to as a custom template). 3.1 Specify the class defined in 2 from the entry point of setup.py of the project of 1.

Template engine provided by default

The file name and contents of the specified template can be customized by the arguments given when the template is generated.

#The arguments given interactively are reflected(ex. version, description)
$ cat test/setup.py
from setuptools import setup, find_packages
import sys, os

version = '0.1'

setup(name='test',
      version=version,
      description="test",
      long_description="""\
test""",
      classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
      keywords='python',
      author='FGtatsuro',
      ...
      )

At this time, the template engine is used to rewrite the contents. The template engine to use is as follows when looking at the document.

  1. If the ʻuse_cheetah` attribute of the custom template is True, then Cheetah is used.
  2. Otherwise, string.Template is used.

Use another template engine

Either of the two is fine, but you may also want to create a custom template using your familiar template engine. Personally, I wanted to use Jinja2, which I'm used to.

The above request can be realized by associating the template_renderer attribute of the custom template with a static method that satisfies certain conditions. The following is an example of using Jinja2 as the template engine.

python


# https://github.com/FGtatsuro/flask-boilerplate/blob/master/flask_boilerplate/templates.py
class Boilerplate(Template):
    _template_dir = 'templates'
    summary = 'A boilerplate for Flask project'
    required_templates = []
    vars = [
        var('app_name', 'Flask application name', default=NoDefault),
        var('description', 'One-line description of the package'),
        var('author', 'Author name'),
        var('author_email', 'Author email'),
    ]
    @staticmethod
    def template_renderer(body, context, filename=None):
        return Jinja2Template(body).render(context)
    ...

The conditions that the static method must meet are as follows.

--Take the character string to be rewritten as the first argument, the dictionary of variables used for rewriting as the second argument, and the file name to be replaced as the third argument. If you don't need the file name, the default file name can be None. --Return the rewritten character string. In most cases, you can just rewrite the string using the template engine and return.

This example is Jinja2, but it can be used in the same way as long as it is a template engine that can generate the rewritten character string from the given three arguments so that the above conditions can be satisfied. I can't say for sure because the other engines are unconfirmed, but I think most template engines are okay.

Recommended Posts

Use Jinja2 for PasteScript template engine
Jinja2 | Python template engine
How to use Template Engine for Network Engineer
Use jinja2 template in excel file
How to use template engine in pyramid 1 file application
parse for format, for Jinja2 ...?
Python template engine empy
Web application with Python3.3.1 + Bottle (1) --Change template engine to jinja2
Use PySide for HDA UI
Pre-processing pipeline template for DataLiner
Python template for Codeforces-manual test-