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.0a2.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

pytemplator-0.1.0a2-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytemplator-0.1.0a2.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for pytemplator-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 5811f2fbf6ed63c81da2dc2f210de7df9948b0f4a668cf915477e2b7b3cde122
MD5 b9adf1aba5d33d6fea2f285d1a380438
BLAKE2b-256 c9f46a80d1a071d7250a47639b0ce908ae77f0199d9caeaaf602d279f2b77f44

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pytemplator-0.1.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 1108805e9622fe383464cb557c81ab12000a5359c7392eea3ac5bd293416e58e
MD5 a55673dcee9ad7c532871f38debff8ef
BLAKE2b-256 f15426dbb8fa0e000db58a7dfd70142d02d6ceae9a5836bc78693af889828439

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