Skip to main content

Pytemplator aims to streamline the creation of dynamic templates. It is inspired from the excellent CookieCutter package but offers more flexibility.

Project description

https://img.shields.io/pypi/v/pytemplator.svg Updates

Pytemplator aims to streamline the creation of dynamic templates. It supports the format from CookieCutter package but also offers the option to generate the context using Python, which in practice provides a better user experience and more flexibility.

How to use

  • Install the package pytemplator using pip or poetry.

  • In a shell:

    $ pytemplate <target>

Where <target> can be either a local path to the directory of a Pytemplator template or the url to a git repo.

There are options to specify which branch should be used for templating, the output directory and the config directory. More details can be obtained with:

$ pytemplate --help

For template developers

Example

See this project example to get an idea of an actual pytemplator template.

General idea

A typical Pytemplator template project can live either as a local directory or as a Git repo. It relies on three elements: - a templates folder where all folders and files to be templated should be placed. Under the hood, pytemplator relies on jinja2. - an initialize.py at the root level with a function “generate_context”. More details below. - a finalize.py whose finalize function is run after the templating.

The generate_context function

The generate_context function should return a dictionary mapping the variables in the template to their values. The idea is to use the extra flexibility to offer sensible default values to make the user experience smoother.

generate_context must accept no_input as argument. This tells what should happen in purely programmatic environment. It is up to you how you’d like to address this, you can provide default values if this makes sense and choose not to handle it, in which case a NoInputOptionNotHandledByTemplateError will be raised.

There are several utility classes to help, Context and Question.

The following code illustrates how they can be used:

import datetime

from pytemplator.utils import Question as Q, Context

def generate_context(no_input, *args, **kwargs):
    """Generate context."""

    context = Context()

    context.questions = [
        Q("pypi_name", ask="Name of the package on Pypi"),
        Q("module_name", ask=False, default=lambda: context["pypi_name"].replace("-","_").lower()),
        Q("year", default=date.today().year, ask=False),
    ]
    context.resolve(no_input)
    return context.as_dict()

A Question takes several arguments: - the key that will be put in the context, required. - ask is the prompt displayed to the user, a default inferred from the key is displayed if this is left to None. Set this to False to take the default without asking the user. - default is the value by default. This can be either a value or a callable. The latter allows for lazy evaluation, especially useful to look into the context to use answers from previous questions. - no_input_default is the value used when no_input is True. If None, default is used.

Contributing

All help is much appreciated and credit is always given. Please consult CONTRIBUTING.rst for details on how to assist me.

Credits

This package is inspired from the excellent CookieCutter package and audreyr/cookiecutter-pypackage project template.

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

pytemplator-0.1.0a1.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

pytemplator-0.1.0a1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file pytemplator-0.1.0a1.tar.gz.

File metadata

  • Download URL: pytemplator-0.1.0a1.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.2 Linux/5.11.0-1027-azure

File hashes

Hashes for pytemplator-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 d711e6b7dc0aefb73862f692b3f7387eb29d557cf6074e339526d9c4f378f0f7
MD5 916f727105912b84b2a816b1f168a485
BLAKE2b-256 eccab8774f9a7e4300f6c7eb47e93347c67e9c6212eba2798b0365acfef853d9

See more details on using hashes here.

File details

Details for the file pytemplator-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: pytemplator-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.2 Linux/5.11.0-1027-azure

File hashes

Hashes for pytemplator-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 b8454d0aac4df89e9cde4d2c58e4941561586868446a73220f892c5d9422c978
MD5 592dd39cebfa4d69a77ac315c854393d
BLAKE2b-256 546840c44f42fff04c3c594562da19e3103268a1d9f244a3aeafeb855ee0d6fe

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