Skip to main content

A library for rendering projects templates

Project description

Copier

A library for rendering projects templates.

  • Works with local paths and git URLs.
  • Your project can include any file and Copier can dynamically replace values in any kind of text files.
  • It generates a beautiful output and take care of not overwrite existing files, unless instructed to do so.

Sample output

How to use

  • Use it in your Python code:
from copier import copy

# Create a project from a local path
copy('path/to/project/template', 'path/to/destination')

# Or from a git URL.
copy('https://github.com/jpscaletti/copier.git', 'path/to/destination')

# You can also use "gh:" as a shortcut of "https://github.com/"
copy('gh:jpscaletti/copier.git', 'path/to/destination')

# Or "gl:" as a shortcut of "https://gitlab.com/"
copy('gl:jpscaletti/copier.git', 'path/to/destination')
  • Or as a command-line tool:
copier path/to/project/template path/to/destination

How it works

The content of the files inside the project template are copied to the destination without changes, unless are suffixed with the extension '.tmpl'. In that case, the templating engine will be used to render them.

A slightly customized Jinja2 templating is used. The main difference is that variables are referenced with [[ name ]] instead of {{ name }} and blocks are [% if name %] instead of {% if name %}. To read more about templating see the Jinja2 documentation.

If a YAML file named copier.yml is found in the root of the project (alternatively, a TOML file named copier.toml, or a JSON file named copier.json), the user will be prompted to fill in or confirm the default values.

Use the data argument to pass whatever extra context you want to be available in the templates. The arguments can be any valid Python value, even a function.

The copier.yml file

If a copier.yml, copier.toml, or copier.json file is found in the root of the project, it will be read and used for two purposes:

Prompt the user for information

For each key found, Copier will prompt the user to fill or confirm the values before they become avaliable to the project template. So a content like this:

name_of_the_project: My awesome project
number_of_eels: 1234
your_email: ""

will result in this series of questions:

   name_of_the_project? [My awesome project]
   your_email? [] myemail@gmail.com
   number_of_eels? [1234] 42

NOTE: All values are required. If you want some value to be optional, do not use an empty string as the default value or copier will not allow you to continue without answering with a value. Use null instead, so you can press ENTER to accept the "empty" default value.

# DO NOT do this for optionals
optional_value: ""

# DO THIS instead
optional_value: null

Arguments defaults

The keys _exclude, _include, _skip_if_exists, _tasks, and _extra_paths in the copier.yml file, will be treated as the default values for the exclude, include, tasks, and , extra_paths arguments to copier.copy().

Note that they become just the defaults, so any explicitely-passed argument will overwrite them.

# Shell-style patterns files/folders that must not be copied.
_exclude:
  - "*.bar"
  - ".git"
  - ".git/*"

# Shell-style patterns files/folders that *must be* copied, even if
# they are in the exclude list
_include:
  - "foo.bar"

# Shell-style patterns files to skip, without asking, if they already exists
# in the destination folder
_skip_if_exists:

# Commands to be executed after the copy
_tasks:
  - "git init"
  - "rm [[ name_of_the_project ]]/README.md"

# Additional paths, from where to search for templates
_extra_paths:
  - ~/Projects/templates

Warning: Use only trusted project templates as these tasks run with the same level of access as your user.


API

copier.copy()

copier.copy(
    src_path,
    dst_path,

    data=DEFAULT_DATA,
    *,
    exclude=DEFAULT_FILTER,
    include=DEFAULT_INCLUDE,
    skip_if_exists=[],
    tasks=[],

    envops={},
    extra_paths=[],

    pretend=False,
    force=False,
    skip=False,
    quiet=False,
    cleanup_on_error=True
)

Uses the template in src_path to generate a new project at dst_path.

Arguments:

  • src_path (str):
    Absolute path to the project skeleton. May be a version control system URL.

  • dst_path (str):
    Absolute path to where to render the skeleton.

  • data (dict):
    Data to be passed to the templates in addtion to the user data from a copier.yml.

  • exclude (list):
    A list of names or shell-style patterns matching files or folders that must not be copied.

    To exclude a folder you should use two entries, one for the folder and the other for its content: [".git", ".git/*"].

  • include (list):
    A list of names or shell-style patterns matching files or folders that must be included, even if its name are a match for the exclude list. Eg: ['.gitignore']. The default is an empty list.

  • skip_if_exists (list):
    Skip any of these files, without asking, if another with the same name already exists in the destination folder. (it only makes sense if you are copying to a folder that already exists).

  • tasks (list):
    Optional lists of commands to run in order after finishing the copy. Like in the templates files, you can use variables on the commands that will be replaced by the real values before running the command. If one of the commands fail, the rest of them will not run.

  • envops (dict):
    Extra options for the Jinja template environment.

  • extra_paths (list):
    Additional paths, from where to search for templates. This is intended to be used with shared parent templates, files with macros, etc. outside the copied project skeleton.

  • pretend (bool):
    Run but do not make any changes.

  • force (bool):
    Overwrite files that already exist, without asking.

  • skip (bool):
    Skip files that already exist, without asking.

  • quiet (bool):
    Suppress the status output.

  • cleanup_on_error (bool):
    Remove the destination folder if the copy process or one of the tasks fail. True by default.

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

copier-2.5.1.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

copier-2.5.1-py2.py3-none-any.whl (13.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file copier-2.5.1.tar.gz.

File metadata

  • Download URL: copier-2.5.1.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for copier-2.5.1.tar.gz
Algorithm Hash digest
SHA256 c12b255a8d57d08ec36b47e7b3fccbaaf7f57cfdfad56e94720ab148482f482c
MD5 99aaffabe4e7928ee87799ae8f4b5dc2
BLAKE2b-256 6a1530568af55a8da45cb690cbf767b7a9a0b65dce96f3bcc5d3df1d8d43e58b

See more details on using hashes here.

File details

Details for the file copier-2.5.1-py2.py3-none-any.whl.

File metadata

  • Download URL: copier-2.5.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for copier-2.5.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6feccafe52131c10aba1dc320ad5edf7c1daee0341c7b9bca7d83b23e527cad2
MD5 af1f3fcc29676e48257f47c5f450b746
BLAKE2b-256 cdda2fbe0bdc9483070c5f00679c218859d2c3c4fe3f86f6d5eedca7156e06ee

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