Package: pytemplator
Pytemplator aims to streamline the creation of dynamic templates.
It supports the format fromCookieCutter packagebut also offers the option
to generate the context using Python, which in practice provides a better user
experience and more flexibility.Free software: Apache Software License 2.0Documentation:https://arnaudblois.github.io/pytemplator/.How to useInstall the packagepytemplatorusing 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 --helpFor template developersExampleSee thisproject exampleto get an idea of an actual pytemplator template.General ideaA typical Pytemplator template project can live either as a local directory or as a Git repo.
It relies on three elements:
- atemplatesfolder where all folders and files to be templated should be placed.
Under the hood, pytemplator relies on jinja2.
- aninitialize.pyat the root level with a function “generate_context”. More details below.
- afinalize.pywhosefinalizefunction is run after the templating.Thegenerate_contextfunctionThegenerate_contextfunction 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_contextmust acceptno_inputas 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,ContextandQuestion.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()AQuestiontakes several arguments:
- the key that will be put in the context, required.
-askis 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.
-defaultis 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_defaultis the value used whenno_inputis True. If None,defaultis used.ContributingAll help is much appreciated and credit is always given.
Please consult CONTRIBUTING.rst for details on how to assist me.CreditsThis package is inspired from the excellentCookieCutter packageandaudreyr/cookiecutter-pypackageproject template.