pytemplator package

Submodules

pytemplator.cli module

Console script for pytemplator.

pytemplator.cli.main()[source]

Console script for pytemplator.

pytemplator.constants module

Various constants used throughout the project.

pytemplator.pytemplator module

Main module.

class pytemplator.pytemplator.Templator(base_dir: Optional[str] = None, template_location: Optional[str] = None, checkout_branch: str = 'main', destination_dir: Optional[str] = None, no_input: bool = False)[source]

Bases: object

Main class creating a project from a template.

add_pytemplator_yaml()[source]

Add a .pytemplator.yml config file.

This allows for future automated boilerplate update to follow the latest version of the template.

finalize()[source]

Run the finalize part of the template.

generate_context()[source]

Generate the context for the initialize part of the template.

get_git_template(url)[source]

Get the template project from a Git repository.

property last_commit_hash

Return the hash of the latest commit.

This is recorded in the .pytemplator.yml file to allow for further re-templating in the future.

This is kept separate from the prepare_template_dir as unlike the checkout logic, storing the commit for a git repo on the filesystem is something useful which doesn’t lead to unexpected side-effects.

prepare_template_dir()[source]

Make sure the template directory is in the expected state.

render()[source]

Copy the folder/files with their names properly templated, then render them.

pytemplator.utils module

Utility functions for the Templator.

class pytemplator.utils.Context(questions=None)[source]

Bases: object

Utility class handling the context passed to the Jinja2 engine.

as_dict()[source]

Return the context as dictionary.

resolve(no_input: bool)[source]

Resolve the questions and populate the context dict.

class pytemplator.utils.Question(key, ask=None, default=None, no_input_default=None, validators=None)[source]

Bases: object

Class handling the user input and validation for a context key.

is_valid()[source]

Make sure the user input passes validation.

resolve(no_input)[source]

Fill the answer attribute, prompting the user if required.

pytemplator.utils.cd(new_dir)[source]

Cd into the directory.

Used as context manager to avoid side-effects if something goes wrong. See SO #24176022

pytemplator.utils.check_if_new_dirs_can_be_created(directories, context, destination_dir, no_input: bool)[source]

Check if any of the templated directories already exist.

If so, offer the user to overwrite them.

pytemplator.utils.generate_context_from_json(json_file, context, no_input)[source]

Generate the context from a json file.

Same behavior as cookiecutter.

pytemplator.utils.import_module_from_path(path)[source]

Import and execute a module in a safer way.

Recipe taken from importlib doc at https://docs.python.org/3/library/importlib.html

pytemplator.utils.is_yes(reply)[source]

Handle human-readable replies.

pytemplator.utils.render_templates(templates, root_directories, context, destination_dir, no_input)[source]

Render the templated directories/files into the current location.

Module contents

Top-level package for pytemplator.