Skip to content

Commit

Permalink
refactor: drop jinja2-ansible-filters and reimplement Jinja2 filters
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp committed Oct 10, 2024
1 parent a4d38b1 commit b831a07
Show file tree
Hide file tree
Showing 6 changed files with 773 additions and 40 deletions.
5 changes: 2 additions & 3 deletions copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
UnsafeTemplateError,
UserMessageError,
)
from .jinja_ext import CopierExtension
from .subproject import Subproject
from .template import Task, Template
from .tools import (
Expand Down Expand Up @@ -547,9 +548,7 @@ def jinja_env(self) -> SandboxedEnvironment:
"""
paths = [str(self.template.local_abspath)]
loader = FileSystemLoader(paths)
default_extensions = [
"jinja2_ansible_filters.AnsibleCoreFiltersExtension",
]
default_extensions = [CopierExtension]
extensions = default_extensions + list(self.template.jinja_extensions)
# We want to minimize the risk of hidden malware in the templates
# so we use the SandboxedEnvironment instead of the regular one.
Expand Down
55 changes: 46 additions & 9 deletions docs/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -950,15 +950,52 @@ Overwrite files that already exist, without asking.
Additional Jinja2 extensions to load in the Jinja2 environment. Extensions can add
filters, global variables and functions, or tags to the environment.

The following extensions are _always_ loaded:

- [`jinja2_ansible_filters.AnsibleCoreFiltersExtension`](https://gitlab.com/dreamer-labs/libraries/jinja2-ansible-filters/):
this extension adds most of the
[Ansible filters](https://docs.ansible.com/ansible/2.3/playbooks_filters.html) to
the environment.

You don't need to tell your template users to install these extensions: Copier depends
on them, so they are always installed when Copier is installed.
By default, most of the
[Ansible filters](https://docs.ansible.com/ansible/2.3/playbooks_filters.html) are
_always_ loaded:

- `ans_groupby`
- `ans_random`
- `b64decode`
- `b64encode`
- `basename`
- `bool`
- `checksum`
- `dirname`
- `expanduser`
- `expandvars`
- `extract`
- `fileglob`
- `flatten`
- `from_json`
- `from_yaml`
- `from_yaml_all`
- `hash`
- `mandatory`
- `md5`
- `quote`
- `random_mac`
- `realpath`
- `regex_escape`
- `regex_findall`
- `regex_replace`
- `regex_search`
- `relpath`
- `sha1`
- `shuffle`
- `splitext`
- `strftime`
- `ternary`
- `to_datetime`
- `to_json`
- `to_nice_json`
- `to_nice_yaml`
- `to_uuid`
- `to_yaml`
- `type_debug`
- `win_basename`
- `win_dirname`
- `win_splitdrive`

!!! warning

Expand Down
10 changes: 4 additions & 6 deletions docs/creating.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ In addition to
[all the features Jinja supports](https://jinja.palletsprojects.com/en/3.1.x/templates/),
Copier includes:
- All functions and filters from
[jinja2-ansible-filters](https://gitlab.com/dreamer-labs/libraries/jinja2-ansible-filters/).
- This includes the `to_nice_yaml` filter, which is used extensively in our
context.

- Most of the
[Ansible filters](https://docs.ansible.com/ansible/2.3/playbooks_filters.html)
including the `to_nice_yaml` filter, which is used extensively in our context. See
the [`jinja_extensions`][] setting for more details.
- `_copier_answers` includes the current answers dict, but slightly modified to make
it suitable to [autoupdate your project safely][the-copier-answersyml-file]:
- It doesn't contain secret answers.
Expand Down
116 changes: 97 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ colorama = ">=0.4.6"
dunamai = ">=1.7.0"
funcy = ">=1.17"
jinja2 = ">=3.1.4"
jinja2-ansible-filters = ">=1.3.1"
packaging = ">=23.0"
pathspec = ">=0.9.0"
plumbum = ">=1.6.9"
pydantic = ">=2.4.2"
pygments = ">=2.7.1"
pyyaml = ">=5.3.1"
questionary = ">=1.8.1"
typing-extensions = { version = ">=3.7.4,<5.0.0", python = "<3.9" }
typing-extensions = { version = ">=3.10.0.0,<5.0.0", python = "<3.9" }
eval-type-backport = { version = ">=0.1.3,<0.3.0", python = "<3.10" }

[tool.poetry.group.dev]
optional = true

[tool.poetry.group.dev.dependencies]
backports-zoneinfo = { version = ">=0.2.1", python = "<3.9" }
mypy = ">=0.931"
pexpect = ">=4.8.0"
poethepoet = ">=0.12.3"
Expand All @@ -56,6 +56,7 @@ pytest = ">=7.2.0"
pytest-cov = ">=3.0.0"
pytest-gitconfig = ">=0.6.0"
pytest-xdist = ">=2.5.0"
time-machine = ">=2.14.2"
types-backports = ">=0.1.3"
types-colorama = ">=0.4"
types-psutil = "*"
Expand Down
Loading

0 comments on commit b831a07

Please sign in to comment.