jj2 0.7.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jj2-0.7.0/LICENSE +26 -0
- jj2-0.7.0/PKG-INFO +345 -0
- jj2-0.7.0/README.md +312 -0
- jj2-0.7.0/pyproject.toml +157 -0
- jj2-0.7.0/pyproject.toml.orig +142 -0
- jj2-0.7.0/src/jj2/__init__.py +12 -0
- jj2-0.7.0/src/jj2/__main__.py +8 -0
- jj2-0.7.0/src/jj2/cli.py +219 -0
- jj2-0.7.0/src/jj2/context.py +43 -0
- jj2-0.7.0/src/jj2/customize.py +31 -0
- jj2-0.7.0/src/jj2/defaults.py +15 -0
- jj2-0.7.0/src/jj2/errors.py +56 -0
- jj2-0.7.0/src/jj2/filters.py +139 -0
- jj2-0.7.0/src/jj2/parsers.py +193 -0
- jj2-0.7.0/src/jj2/render.py +106 -0
jj2-0.7.0/LICENSE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Copyright (c) 2019-2026, Manolis Stamatogiannakis
|
|
2
|
+
Copyright (c) 2014-2019, Mark Vartanyan
|
|
3
|
+
Copyright (c) 2012-2013, Matt Robenolt
|
|
4
|
+
|
|
5
|
+
All rights reserved.
|
|
6
|
+
|
|
7
|
+
Redistribution and use in source and binary forms, with or without modification,
|
|
8
|
+
are permitted provided that the following conditions are met:
|
|
9
|
+
|
|
10
|
+
* Redistributions of source code must retain the above copyright notice,
|
|
11
|
+
this list of conditions and the following disclaimer.
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
19
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
20
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
21
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
22
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
23
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
24
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
25
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
26
|
+
POSSIBILITY OF SUCH DAMAGE.
|
jj2-0.7.0/PKG-INFO
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jj2
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: Juiced Jinja2 command-line tool
|
|
5
|
+
Keywords: Jinja2,templating,command-line,CLI
|
|
6
|
+
Author: Manolis Stamatogiannakis
|
|
7
|
+
Author-email: Manolis Stamatogiannakis <mstamat@gmail.com>
|
|
8
|
+
License-Expression: BSD-2-Clause
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: System Administrators
|
|
14
|
+
Classifier: Natural Language :: English
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
22
|
+
Classifier: Topic :: Software Development
|
|
23
|
+
Classifier: Topic :: Text Processing :: General
|
|
24
|
+
Requires-Dist: jinja2>=3.1
|
|
25
|
+
Requires-Dist: python-dotenv>=1.0
|
|
26
|
+
Requires-Dist: pyyaml>=6.0
|
|
27
|
+
Requires-Python: >=3.12
|
|
28
|
+
Project-URL: Homepage, https://github.com/m000/jj2
|
|
29
|
+
Project-URL: Repository, https://github.com/m000/jj2
|
|
30
|
+
Project-URL: Issues, https://github.com/m000/jj2/issues
|
|
31
|
+
Project-URL: Changelog, https://github.com/m000/jj2/blob/master/CHANGELOG.md
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
[![CI][]][1] [![PyPI][]][2] [![Python]][2]
|
|
35
|
+
|
|
36
|
+
[CI]: https://github.com/m000/jj2/actions/workflows/ci.yaml/badge.svg?branch=master
|
|
37
|
+
[1]: https://github.com/m000/jj2/actions/workflows/ci.yaml
|
|
38
|
+
[PyPI]: https://img.shields.io/pypi/v/jj2.svg
|
|
39
|
+
[2]: https://pypi.org/project/jj2/
|
|
40
|
+
[Python]: https://img.shields.io/badge/python-3.12%2B-blue.svg
|
|
41
|
+
|
|
42
|
+
# jj2 - Juiced Jinja2 command-line tool
|
|
43
|
+
|
|
44
|
+
jj2 (previously `jj2cli`, a fork of [j2cli]) is a command-line tool for
|
|
45
|
+
templating in shell-scripts, leveraging the [Jinja2] library. It supports
|
|
46
|
+
[several formats] for loading the context used for rendering the Jinja2
|
|
47
|
+
templates. Loading environment variables as rendering context is also supported.
|
|
48
|
+
|
|
49
|
+
> [!WARNING]
|
|
50
|
+
> jj2 is in beta. This means that you can expect each release to be usable, but
|
|
51
|
+
> before 1.0 you should expect occasional:
|
|
52
|
+
>
|
|
53
|
+
> - command-line option changes
|
|
54
|
+
> - history rewrites on the development branch (`master-next`)
|
|
55
|
+
|
|
56
|
+
If you find jj2 useful, we would love to hear from you! Please [open an issue]
|
|
57
|
+
for bugs, questions, ideas, or use cases that jj2 does not cover yet. Feedback
|
|
58
|
+
from users shapes what comes next.
|
|
59
|
+
|
|
60
|
+
[j2cli]: https://github.com/kolypto/j2cli
|
|
61
|
+
[Jinja2]: https://jinja.palletsprojects.com/
|
|
62
|
+
[several formats]: #supported-formats
|
|
63
|
+
[open an issue]: https://github.com/m000/jj2/issues
|
|
64
|
+
|
|
65
|
+
## Getting started
|
|
66
|
+
|
|
67
|
+
### Installation
|
|
68
|
+
|
|
69
|
+
``` sh
|
|
70
|
+
# Install jj2 as a standalone tool with uv:
|
|
71
|
+
$ uv tool install jj2
|
|
72
|
+
# Or install jj2 with pip:
|
|
73
|
+
$ pip install jj2
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
To run jj2 one time without installation, use `uvx`:
|
|
77
|
+
|
|
78
|
+
``` sh
|
|
79
|
+
$ uvx jj2 config.j2 config.json
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
To install the development branch, use its Git URL:
|
|
83
|
+
|
|
84
|
+
``` sh
|
|
85
|
+
$ uv tool install git+https://github.com/m000/jj2@master-next
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
In a clone of the repository, run jj2 in the project environment:
|
|
89
|
+
|
|
90
|
+
``` sh
|
|
91
|
+
$ uv run jj2 config.j2 config.json
|
|
92
|
+
$ uv run python -m jj2 config.j2 config.json
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Basic usage
|
|
96
|
+
|
|
97
|
+
``` sh
|
|
98
|
+
# Render the template config.j2 with the data in config.json.
|
|
99
|
+
# Write the result to the file config.
|
|
100
|
+
$ jj2 config.j2 config.json -o config
|
|
101
|
+
|
|
102
|
+
# Render the template with YAML data from stdin.
|
|
103
|
+
$ wget -O - http://example.com/config.yml | jj2 config.j2 :yaml
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
For more examples, see [docs/examples.md]. For recipes for common tasks, see
|
|
107
|
+
[docs/howto.md].
|
|
108
|
+
|
|
109
|
+
[docs/examples.md]: https://github.com/m000/jj2/blob/master/docs/examples.md
|
|
110
|
+
[docs/howto.md]: https://github.com/m000/jj2/blob/master/docs/howto.md
|
|
111
|
+
|
|
112
|
+
## Context data
|
|
113
|
+
|
|
114
|
+
### Supported formats
|
|
115
|
+
|
|
116
|
+
jj2 reads the context in these formats:
|
|
117
|
+
|
|
118
|
+
- [JSON][]: a data format that many programming languages use. It comes from
|
|
119
|
+
JavaScript.
|
|
120
|
+
- [YAML][]: a data format that is easy for persons to read.
|
|
121
|
+
- [INI][]: the configuration file format of Windows.
|
|
122
|
+
- env: a file with [Unix-style] environment variable assignments, as in `.env`
|
|
123
|
+
files.
|
|
124
|
+
- ENV: the environment variables of the jj2 process. This format does not read a
|
|
125
|
+
file.
|
|
126
|
+
|
|
127
|
+
For an example of each format, see [docs/formats.md].
|
|
128
|
+
|
|
129
|
+
[JSON]: https://en.wikipedia.org/wiki/JSON
|
|
130
|
+
[YAML]: https://en.wikipedia.org/wiki/YAML
|
|
131
|
+
[INI]: https://en.wikipedia.org/wiki/INI_file
|
|
132
|
+
[Unix-style]: https://en.wikipedia.org/wiki/Environment_variable#Unix
|
|
133
|
+
[docs/formats.md]: https://github.com/m000/jj2/blob/master/docs/formats.md
|
|
134
|
+
|
|
135
|
+
### Context squashing
|
|
136
|
+
|
|
137
|
+
One of the main strengths of jj2 is that it is not limited to using a single
|
|
138
|
+
data file as context. Several data files—perhaps in different formats—can be
|
|
139
|
+
used to construct the rendering context. As the contents of the data files may
|
|
140
|
+
"overlap", jj2 *recursively squashes* their contents to produce the context that
|
|
141
|
+
will be used for rendering. The order of squashing is *from left to right*. That
|
|
142
|
+
is, the contents of a data file may be overridden by any data files specified
|
|
143
|
+
*after it* on the command line.
|
|
144
|
+
|
|
145
|
+
This example shows how jj2 squashes two files:
|
|
146
|
+
|
|
147
|
+
- The data in `a.json`:
|
|
148
|
+
|
|
149
|
+
``` json
|
|
150
|
+
{"a": 1, "c": {"x": 2, "y": 3}}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
- The data in `b.json`:
|
|
154
|
+
|
|
155
|
+
``` json
|
|
156
|
+
{"b": 2, "c": {"y": 4}}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
- The context for `jj2 template.j2 a.json b.json`:
|
|
160
|
+
|
|
161
|
+
``` json
|
|
162
|
+
{"a": 1, "b": 2, "c": {"x": 2, "y": 4}}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
A key with the value `null` is removed from the context. A YAML key with no
|
|
166
|
+
value is also removed.
|
|
167
|
+
|
|
168
|
+
### Loading data as a context subtree
|
|
169
|
+
|
|
170
|
+
By default, the data from each source is squashed into the top level of the
|
|
171
|
+
context. Sources with overlapping keys, or with many keys, such as the
|
|
172
|
+
environment variables, can instead be put under a key. Each data source is given
|
|
173
|
+
as a *data spec*:
|
|
174
|
+
|
|
175
|
+
[dest=]path[:format]
|
|
176
|
+
|
|
177
|
+
- `path`: the data file. For stdin, use `-` or no path.
|
|
178
|
+
- `format`: the format of the data. It replaces the format of the file
|
|
179
|
+
extension. For example: `settings.cfg:ini`.
|
|
180
|
+
- `dest`: the *context destination*. This is a key or a dotted key path. jj2
|
|
181
|
+
puts the data under it. For example: `web=nginx.json` or `app.host=host.yaml`.
|
|
182
|
+
jj2 uses the part before the first `=` as a destination only if it is a dotted
|
|
183
|
+
name. Thus, a path such as `./a=b.json` also works.
|
|
184
|
+
|
|
185
|
+
If the top level of a source is a list or a single value, the source must have a
|
|
186
|
+
destination. For example: `items=list.json`. An empty YAML file gives no data.
|
|
187
|
+
|
|
188
|
+
jj2 expands a `~` at the start of the path. The shell does not always do this
|
|
189
|
+
after `dest=`: zsh does not do it by default, and bash does not do it after a
|
|
190
|
+
dotted key path.
|
|
191
|
+
|
|
192
|
+
| Data spec | Where jj2 puts the data |
|
|
193
|
+
|----------------------|----------------------------------------------------------|
|
|
194
|
+
| `nginx.json` | at the top level |
|
|
195
|
+
| `web=nginx.json` | under `web`: `{{ web.hostname }}` |
|
|
196
|
+
| `app.host=host.yaml` | under `app.host`, merged with the other data under `app` |
|
|
197
|
+
| `settings.cfg:ini` | at the top level, read as INI |
|
|
198
|
+
| `web=-:json` | under `web`, read as JSON from stdin |
|
|
199
|
+
| `env=:ENV` | under `env`, from the environment variables |
|
|
200
|
+
|
|
201
|
+
> [!NOTE]
|
|
202
|
+
> The argument parser reads a data spec that starts with `-` as an option. For
|
|
203
|
+
> example: `-:json`. Write `:json` in its place, or put `--` before it:
|
|
204
|
+
> `jj2 config.j2 -- -:json`.
|
|
205
|
+
|
|
206
|
+
> [!TIP]
|
|
207
|
+
> By default, zsh does not complete a path after `dest=` when you push
|
|
208
|
+
> <kbd>Tab</kbd>. zsh reads `web=ngi` as one word. To complete after `=`, add
|
|
209
|
+
> `setopt magic_equal_subst` to your `~/.zshrc`. Or complete the path first, and
|
|
210
|
+
> then type `dest=` in front of it. bash completes after `=` without changes.
|
|
211
|
+
|
|
212
|
+
## Reference
|
|
213
|
+
|
|
214
|
+
`jj2` uses these arguments:
|
|
215
|
+
|
|
216
|
+
- `template`: the Jinja2 template file to render.
|
|
217
|
+
- `data`: one or more data specs, `[dest=]path[:format]`. Refer to [Loading data
|
|
218
|
+
as a context subtree].
|
|
219
|
+
|
|
220
|
+
Options:
|
|
221
|
+
|
|
222
|
+
- `-f FORMAT`, `--fallback-format FORMAT`: the format of data that has no format
|
|
223
|
+
in its data spec and no known file extension. The default is `ini`.
|
|
224
|
+
- `-T DIR`, `--template-dir DIR`: look for the templates that a template
|
|
225
|
+
includes, imports or extends in `DIR`. Give the option more times for more
|
|
226
|
+
directories. Refer to [Template search].
|
|
227
|
+
- `-I`, `--ignore-missing`: ignore data files that do not exist. Without this
|
|
228
|
+
option, jj2 stops with an error.
|
|
229
|
+
- `-o outfile`: write the result to a file. Without this option, jj2 writes to
|
|
230
|
+
stdout.
|
|
231
|
+
- `-U MODE`, `--undefined MODE`: what jj2 does with undefined variables:
|
|
232
|
+
`strict` (the default), `normal` or `debug`. Refer to the [Jinja2 docs].
|
|
233
|
+
- `--no-compact`: keep the whitespace around Jinja2 blocks.
|
|
234
|
+
- `--filters a.py,b.py`: load the top-level functions of Python files as Jinja2
|
|
235
|
+
filters. Give a comma-separated list, or give the option more times.
|
|
236
|
+
- `--tests a.py,b.py`: load the top-level functions of Python files as Jinja2
|
|
237
|
+
tests. Give a comma-separated list, or give the option more times.
|
|
238
|
+
- `--customize custom.py`: load hooks from a Python file. The hooks change how
|
|
239
|
+
jj2 sets up Jinja2. Use this option only if the other options are not
|
|
240
|
+
sufficient. Refer to [Advanced customization].
|
|
241
|
+
- `-v`, `--verbose`: show more messages. Give the option more times for more
|
|
242
|
+
messages.
|
|
243
|
+
- `-V`, `--version`: show the versions of jj2 and Jinja2.
|
|
244
|
+
|
|
245
|
+
[Loading data as a context subtree]: #loading-data-as-a-context-subtree
|
|
246
|
+
[Template search]: #template-search
|
|
247
|
+
[Jinja2 docs]: https://jinja.palletsprojects.com/en/stable/api/#undefined-types
|
|
248
|
+
[Advanced customization]: #advanced-customization
|
|
249
|
+
|
|
250
|
+
### Template search
|
|
251
|
+
|
|
252
|
+
jj2 looks for the templates that a template includes, imports or extends in this
|
|
253
|
+
order:
|
|
254
|
+
|
|
255
|
+
1. The `-T` directories, in the order of the options.
|
|
256
|
+
2. The directory of the main template.
|
|
257
|
+
|
|
258
|
+
A file in a `-T` directory thus replaces the file with the same name next to the
|
|
259
|
+
template. To prevent this by accident, put libraries of templates in
|
|
260
|
+
subdirectories, for example `{% include "tools/vim.j2" %}`. jj2 does not look in
|
|
261
|
+
the current directory; to add it, use `-T .`.
|
|
262
|
+
|
|
263
|
+
The main template is always the file on the command line.
|
|
264
|
+
|
|
265
|
+
### Environment variables
|
|
266
|
+
|
|
267
|
+
To use the environment variables as data, use the `ENV` format. `:ENV` puts them
|
|
268
|
+
at the top level, and `env=:ENV` puts them under `env`.
|
|
269
|
+
|
|
270
|
+
To read a file with variable assignments, use the `env` format. For example,
|
|
271
|
+
make the file with `env > /tmp/file.env`.
|
|
272
|
+
|
|
273
|
+
## Extras
|
|
274
|
+
|
|
275
|
+
### Filters
|
|
276
|
+
|
|
277
|
+
For convenience, jj2 offers several additional Jinja2 filters that can be used
|
|
278
|
+
in your templates. These filters should help you avoid having to implement an
|
|
279
|
+
[advanced customization module] for many use cases.
|
|
280
|
+
|
|
281
|
+
For a list of the filters, see [docs/filters.md].
|
|
282
|
+
|
|
283
|
+
[advanced customization module]: #advanced-customization
|
|
284
|
+
[docs/filters.md]: https://github.com/m000/jj2/blob/master/docs/filters.md
|
|
285
|
+
|
|
286
|
+
### Advanced customization
|
|
287
|
+
|
|
288
|
+
jj2 offers several *hooks* that allow for more advanced customization of its
|
|
289
|
+
operation. With these hooks, you can:
|
|
290
|
+
|
|
291
|
+
- give more parameters to the Jinja2 environment.
|
|
292
|
+
- change the context before jj2 renders the template.
|
|
293
|
+
- add filters and tests.
|
|
294
|
+
|
|
295
|
+
For more information, see [docs/advanced.md].
|
|
296
|
+
|
|
297
|
+
[docs/advanced.md]: https://github.com/m000/jj2/blob/master/docs/advanced.md
|
|
298
|
+
|
|
299
|
+
## Features and roadmap
|
|
300
|
+
|
|
301
|
+
How jj2 compares with other Jinja2 command-line tools, as of December 2026:
|
|
302
|
+
|
|
303
|
+
| | jj2 | [jinja2-cli] | [jinjanator] | [j2cli] |
|
|
304
|
+
|----|----|----|----|----|
|
|
305
|
+
| Latest release | [![jj2 on PyPI]][3] | [![jinja2-cli on PyPI]][4] | [![jinjanator on PyPI]][5] | 0.3.10 (2019) |
|
|
306
|
+
| Status | beta, active | stable; no release since 2026-04 | stable, active | archived (2024) |
|
|
307
|
+
| Made for | shell scripts and makefiles: config and dotfile builds | general use, also in Docker images | shell scripts | shell scripts, Docker containers |
|
|
308
|
+
| Python | 3.12 and later | 3.8 and later | 3.11 and later | 2 and 3 |
|
|
309
|
+
| Data files | many, [squashed] | many, deep merge | one | one |
|
|
310
|
+
| Data formats | JSON, YAML, INI, env, ENV | JSON, INI, env, querystring, TOML; YAML, XML, HJSON, JSON5 as extras | JSON, YAML, INI, dotenv; TOML, XML as plugins | JSON, YAML, INI, env |
|
|
311
|
+
| Format override | for each file: `data.cfg:ini` | one `-f` for all files | `-f` | `-f` |
|
|
312
|
+
| Data under a key | `web=nginx.json` | — | — | — |
|
|
313
|
+
| Environment variables | as data: `:ENV`, `env=:ENV`; `env()` | `environ()` | `env()`, `--import-env` | `env()`, `--import-env` |
|
|
314
|
+
| Values on the command line | planned: [\#9] | `-D key=value` | — | — |
|
|
315
|
+
| Template search paths | `-T DIR` | `-I DIR` | — | — |
|
|
316
|
+
| Custom filters and tests | Python files | Python modules (`-F`), extensions (`-e`) | Python files, plugins | Python files |
|
|
317
|
+
| Customization | `--customize` hooks | Jinja2 options as flags | `--customize` hooks, plugins | `--customize` hooks |
|
|
318
|
+
| Dependency file for make | planned: [\#10] | — | — | — |
|
|
319
|
+
| Render a directory | planned: [\#18] | — | — | — |
|
|
320
|
+
|
|
321
|
+
The planned features are GitHub issues, grouped in [milestones]. If jj2 does not
|
|
322
|
+
do something that you need, [open an issue].
|
|
323
|
+
|
|
324
|
+
[jinja2-cli]: https://github.com/mattrobenolt/jinja2-cli
|
|
325
|
+
[jinjanator]: https://github.com/kpfleming/jinjanator
|
|
326
|
+
[j2cli]: https://github.com/kolypto/j2cli
|
|
327
|
+
[jj2 on PyPI]: https://img.shields.io/pypi/v/jj2
|
|
328
|
+
[3]: https://pypi.org/project/jj2/
|
|
329
|
+
[jinja2-cli on PyPI]: https://img.shields.io/pypi/v/jinja2-cli
|
|
330
|
+
[4]: https://pypi.org/project/jinja2-cli/
|
|
331
|
+
[jinjanator on PyPI]: https://img.shields.io/pypi/v/jinjanator
|
|
332
|
+
[5]: https://pypi.org/project/jinjanator/
|
|
333
|
+
[squashed]: #context-squashing
|
|
334
|
+
[\#9]: https://github.com/m000/jj2/issues/9
|
|
335
|
+
[\#10]: https://github.com/m000/jj2/issues/10
|
|
336
|
+
[\#18]: https://github.com/m000/jj2/issues/18
|
|
337
|
+
[milestones]: https://github.com/m000/jj2/milestones
|
|
338
|
+
[open an issue]: https://github.com/m000/jj2/issues
|
|
339
|
+
|
|
340
|
+
## Credits
|
|
341
|
+
|
|
342
|
+
jj2 is inspired by and builds on [kolypto/j2cli] and [mattrobenolt/jinja2-cli].
|
|
343
|
+
|
|
344
|
+
[kolypto/j2cli]: https://github.com/kolypto/j2cli
|
|
345
|
+
[mattrobenolt/jinja2-cli]: https://github.com/mattrobenolt/jinja2-cli
|
jj2-0.7.0/README.md
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
[![CI][]][1] [![PyPI][]][2] [![Python]][2]
|
|
2
|
+
|
|
3
|
+
[CI]: https://github.com/m000/jj2/actions/workflows/ci.yaml/badge.svg?branch=master
|
|
4
|
+
[1]: https://github.com/m000/jj2/actions/workflows/ci.yaml
|
|
5
|
+
[PyPI]: https://img.shields.io/pypi/v/jj2.svg
|
|
6
|
+
[2]: https://pypi.org/project/jj2/
|
|
7
|
+
[Python]: https://img.shields.io/badge/python-3.12%2B-blue.svg
|
|
8
|
+
|
|
9
|
+
# jj2 - Juiced Jinja2 command-line tool
|
|
10
|
+
|
|
11
|
+
jj2 (previously `jj2cli`, a fork of [j2cli]) is a command-line tool for
|
|
12
|
+
templating in shell-scripts, leveraging the [Jinja2] library. It supports
|
|
13
|
+
[several formats] for loading the context used for rendering the Jinja2
|
|
14
|
+
templates. Loading environment variables as rendering context is also supported.
|
|
15
|
+
|
|
16
|
+
> [!WARNING]
|
|
17
|
+
> jj2 is in beta. This means that you can expect each release to be usable, but
|
|
18
|
+
> before 1.0 you should expect occasional:
|
|
19
|
+
>
|
|
20
|
+
> - command-line option changes
|
|
21
|
+
> - history rewrites on the development branch (`master-next`)
|
|
22
|
+
|
|
23
|
+
If you find jj2 useful, we would love to hear from you! Please [open an issue]
|
|
24
|
+
for bugs, questions, ideas, or use cases that jj2 does not cover yet. Feedback
|
|
25
|
+
from users shapes what comes next.
|
|
26
|
+
|
|
27
|
+
[j2cli]: https://github.com/kolypto/j2cli
|
|
28
|
+
[Jinja2]: https://jinja.palletsprojects.com/
|
|
29
|
+
[several formats]: #supported-formats
|
|
30
|
+
[open an issue]: https://github.com/m000/jj2/issues
|
|
31
|
+
|
|
32
|
+
## Getting started
|
|
33
|
+
|
|
34
|
+
### Installation
|
|
35
|
+
|
|
36
|
+
``` sh
|
|
37
|
+
# Install jj2 as a standalone tool with uv:
|
|
38
|
+
$ uv tool install jj2
|
|
39
|
+
# Or install jj2 with pip:
|
|
40
|
+
$ pip install jj2
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
To run jj2 one time without installation, use `uvx`:
|
|
44
|
+
|
|
45
|
+
``` sh
|
|
46
|
+
$ uvx jj2 config.j2 config.json
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
To install the development branch, use its Git URL:
|
|
50
|
+
|
|
51
|
+
``` sh
|
|
52
|
+
$ uv tool install git+https://github.com/m000/jj2@master-next
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
In a clone of the repository, run jj2 in the project environment:
|
|
56
|
+
|
|
57
|
+
``` sh
|
|
58
|
+
$ uv run jj2 config.j2 config.json
|
|
59
|
+
$ uv run python -m jj2 config.j2 config.json
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Basic usage
|
|
63
|
+
|
|
64
|
+
``` sh
|
|
65
|
+
# Render the template config.j2 with the data in config.json.
|
|
66
|
+
# Write the result to the file config.
|
|
67
|
+
$ jj2 config.j2 config.json -o config
|
|
68
|
+
|
|
69
|
+
# Render the template with YAML data from stdin.
|
|
70
|
+
$ wget -O - http://example.com/config.yml | jj2 config.j2 :yaml
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
For more examples, see [docs/examples.md]. For recipes for common tasks, see
|
|
74
|
+
[docs/howto.md].
|
|
75
|
+
|
|
76
|
+
[docs/examples.md]: https://github.com/m000/jj2/blob/master/docs/examples.md
|
|
77
|
+
[docs/howto.md]: https://github.com/m000/jj2/blob/master/docs/howto.md
|
|
78
|
+
|
|
79
|
+
## Context data
|
|
80
|
+
|
|
81
|
+
### Supported formats
|
|
82
|
+
|
|
83
|
+
jj2 reads the context in these formats:
|
|
84
|
+
|
|
85
|
+
- [JSON][]: a data format that many programming languages use. It comes from
|
|
86
|
+
JavaScript.
|
|
87
|
+
- [YAML][]: a data format that is easy for persons to read.
|
|
88
|
+
- [INI][]: the configuration file format of Windows.
|
|
89
|
+
- env: a file with [Unix-style] environment variable assignments, as in `.env`
|
|
90
|
+
files.
|
|
91
|
+
- ENV: the environment variables of the jj2 process. This format does not read a
|
|
92
|
+
file.
|
|
93
|
+
|
|
94
|
+
For an example of each format, see [docs/formats.md].
|
|
95
|
+
|
|
96
|
+
[JSON]: https://en.wikipedia.org/wiki/JSON
|
|
97
|
+
[YAML]: https://en.wikipedia.org/wiki/YAML
|
|
98
|
+
[INI]: https://en.wikipedia.org/wiki/INI_file
|
|
99
|
+
[Unix-style]: https://en.wikipedia.org/wiki/Environment_variable#Unix
|
|
100
|
+
[docs/formats.md]: https://github.com/m000/jj2/blob/master/docs/formats.md
|
|
101
|
+
|
|
102
|
+
### Context squashing
|
|
103
|
+
|
|
104
|
+
One of the main strengths of jj2 is that it is not limited to using a single
|
|
105
|
+
data file as context. Several data files—perhaps in different formats—can be
|
|
106
|
+
used to construct the rendering context. As the contents of the data files may
|
|
107
|
+
"overlap", jj2 *recursively squashes* their contents to produce the context that
|
|
108
|
+
will be used for rendering. The order of squashing is *from left to right*. That
|
|
109
|
+
is, the contents of a data file may be overridden by any data files specified
|
|
110
|
+
*after it* on the command line.
|
|
111
|
+
|
|
112
|
+
This example shows how jj2 squashes two files:
|
|
113
|
+
|
|
114
|
+
- The data in `a.json`:
|
|
115
|
+
|
|
116
|
+
``` json
|
|
117
|
+
{"a": 1, "c": {"x": 2, "y": 3}}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
- The data in `b.json`:
|
|
121
|
+
|
|
122
|
+
``` json
|
|
123
|
+
{"b": 2, "c": {"y": 4}}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
- The context for `jj2 template.j2 a.json b.json`:
|
|
127
|
+
|
|
128
|
+
``` json
|
|
129
|
+
{"a": 1, "b": 2, "c": {"x": 2, "y": 4}}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
A key with the value `null` is removed from the context. A YAML key with no
|
|
133
|
+
value is also removed.
|
|
134
|
+
|
|
135
|
+
### Loading data as a context subtree
|
|
136
|
+
|
|
137
|
+
By default, the data from each source is squashed into the top level of the
|
|
138
|
+
context. Sources with overlapping keys, or with many keys, such as the
|
|
139
|
+
environment variables, can instead be put under a key. Each data source is given
|
|
140
|
+
as a *data spec*:
|
|
141
|
+
|
|
142
|
+
[dest=]path[:format]
|
|
143
|
+
|
|
144
|
+
- `path`: the data file. For stdin, use `-` or no path.
|
|
145
|
+
- `format`: the format of the data. It replaces the format of the file
|
|
146
|
+
extension. For example: `settings.cfg:ini`.
|
|
147
|
+
- `dest`: the *context destination*. This is a key or a dotted key path. jj2
|
|
148
|
+
puts the data under it. For example: `web=nginx.json` or `app.host=host.yaml`.
|
|
149
|
+
jj2 uses the part before the first `=` as a destination only if it is a dotted
|
|
150
|
+
name. Thus, a path such as `./a=b.json` also works.
|
|
151
|
+
|
|
152
|
+
If the top level of a source is a list or a single value, the source must have a
|
|
153
|
+
destination. For example: `items=list.json`. An empty YAML file gives no data.
|
|
154
|
+
|
|
155
|
+
jj2 expands a `~` at the start of the path. The shell does not always do this
|
|
156
|
+
after `dest=`: zsh does not do it by default, and bash does not do it after a
|
|
157
|
+
dotted key path.
|
|
158
|
+
|
|
159
|
+
| Data spec | Where jj2 puts the data |
|
|
160
|
+
|----------------------|----------------------------------------------------------|
|
|
161
|
+
| `nginx.json` | at the top level |
|
|
162
|
+
| `web=nginx.json` | under `web`: `{{ web.hostname }}` |
|
|
163
|
+
| `app.host=host.yaml` | under `app.host`, merged with the other data under `app` |
|
|
164
|
+
| `settings.cfg:ini` | at the top level, read as INI |
|
|
165
|
+
| `web=-:json` | under `web`, read as JSON from stdin |
|
|
166
|
+
| `env=:ENV` | under `env`, from the environment variables |
|
|
167
|
+
|
|
168
|
+
> [!NOTE]
|
|
169
|
+
> The argument parser reads a data spec that starts with `-` as an option. For
|
|
170
|
+
> example: `-:json`. Write `:json` in its place, or put `--` before it:
|
|
171
|
+
> `jj2 config.j2 -- -:json`.
|
|
172
|
+
|
|
173
|
+
> [!TIP]
|
|
174
|
+
> By default, zsh does not complete a path after `dest=` when you push
|
|
175
|
+
> <kbd>Tab</kbd>. zsh reads `web=ngi` as one word. To complete after `=`, add
|
|
176
|
+
> `setopt magic_equal_subst` to your `~/.zshrc`. Or complete the path first, and
|
|
177
|
+
> then type `dest=` in front of it. bash completes after `=` without changes.
|
|
178
|
+
|
|
179
|
+
## Reference
|
|
180
|
+
|
|
181
|
+
`jj2` uses these arguments:
|
|
182
|
+
|
|
183
|
+
- `template`: the Jinja2 template file to render.
|
|
184
|
+
- `data`: one or more data specs, `[dest=]path[:format]`. Refer to [Loading data
|
|
185
|
+
as a context subtree].
|
|
186
|
+
|
|
187
|
+
Options:
|
|
188
|
+
|
|
189
|
+
- `-f FORMAT`, `--fallback-format FORMAT`: the format of data that has no format
|
|
190
|
+
in its data spec and no known file extension. The default is `ini`.
|
|
191
|
+
- `-T DIR`, `--template-dir DIR`: look for the templates that a template
|
|
192
|
+
includes, imports or extends in `DIR`. Give the option more times for more
|
|
193
|
+
directories. Refer to [Template search].
|
|
194
|
+
- `-I`, `--ignore-missing`: ignore data files that do not exist. Without this
|
|
195
|
+
option, jj2 stops with an error.
|
|
196
|
+
- `-o outfile`: write the result to a file. Without this option, jj2 writes to
|
|
197
|
+
stdout.
|
|
198
|
+
- `-U MODE`, `--undefined MODE`: what jj2 does with undefined variables:
|
|
199
|
+
`strict` (the default), `normal` or `debug`. Refer to the [Jinja2 docs].
|
|
200
|
+
- `--no-compact`: keep the whitespace around Jinja2 blocks.
|
|
201
|
+
- `--filters a.py,b.py`: load the top-level functions of Python files as Jinja2
|
|
202
|
+
filters. Give a comma-separated list, or give the option more times.
|
|
203
|
+
- `--tests a.py,b.py`: load the top-level functions of Python files as Jinja2
|
|
204
|
+
tests. Give a comma-separated list, or give the option more times.
|
|
205
|
+
- `--customize custom.py`: load hooks from a Python file. The hooks change how
|
|
206
|
+
jj2 sets up Jinja2. Use this option only if the other options are not
|
|
207
|
+
sufficient. Refer to [Advanced customization].
|
|
208
|
+
- `-v`, `--verbose`: show more messages. Give the option more times for more
|
|
209
|
+
messages.
|
|
210
|
+
- `-V`, `--version`: show the versions of jj2 and Jinja2.
|
|
211
|
+
|
|
212
|
+
[Loading data as a context subtree]: #loading-data-as-a-context-subtree
|
|
213
|
+
[Template search]: #template-search
|
|
214
|
+
[Jinja2 docs]: https://jinja.palletsprojects.com/en/stable/api/#undefined-types
|
|
215
|
+
[Advanced customization]: #advanced-customization
|
|
216
|
+
|
|
217
|
+
### Template search
|
|
218
|
+
|
|
219
|
+
jj2 looks for the templates that a template includes, imports or extends in this
|
|
220
|
+
order:
|
|
221
|
+
|
|
222
|
+
1. The `-T` directories, in the order of the options.
|
|
223
|
+
2. The directory of the main template.
|
|
224
|
+
|
|
225
|
+
A file in a `-T` directory thus replaces the file with the same name next to the
|
|
226
|
+
template. To prevent this by accident, put libraries of templates in
|
|
227
|
+
subdirectories, for example `{% include "tools/vim.j2" %}`. jj2 does not look in
|
|
228
|
+
the current directory; to add it, use `-T .`.
|
|
229
|
+
|
|
230
|
+
The main template is always the file on the command line.
|
|
231
|
+
|
|
232
|
+
### Environment variables
|
|
233
|
+
|
|
234
|
+
To use the environment variables as data, use the `ENV` format. `:ENV` puts them
|
|
235
|
+
at the top level, and `env=:ENV` puts them under `env`.
|
|
236
|
+
|
|
237
|
+
To read a file with variable assignments, use the `env` format. For example,
|
|
238
|
+
make the file with `env > /tmp/file.env`.
|
|
239
|
+
|
|
240
|
+
## Extras
|
|
241
|
+
|
|
242
|
+
### Filters
|
|
243
|
+
|
|
244
|
+
For convenience, jj2 offers several additional Jinja2 filters that can be used
|
|
245
|
+
in your templates. These filters should help you avoid having to implement an
|
|
246
|
+
[advanced customization module] for many use cases.
|
|
247
|
+
|
|
248
|
+
For a list of the filters, see [docs/filters.md].
|
|
249
|
+
|
|
250
|
+
[advanced customization module]: #advanced-customization
|
|
251
|
+
[docs/filters.md]: https://github.com/m000/jj2/blob/master/docs/filters.md
|
|
252
|
+
|
|
253
|
+
### Advanced customization
|
|
254
|
+
|
|
255
|
+
jj2 offers several *hooks* that allow for more advanced customization of its
|
|
256
|
+
operation. With these hooks, you can:
|
|
257
|
+
|
|
258
|
+
- give more parameters to the Jinja2 environment.
|
|
259
|
+
- change the context before jj2 renders the template.
|
|
260
|
+
- add filters and tests.
|
|
261
|
+
|
|
262
|
+
For more information, see [docs/advanced.md].
|
|
263
|
+
|
|
264
|
+
[docs/advanced.md]: https://github.com/m000/jj2/blob/master/docs/advanced.md
|
|
265
|
+
|
|
266
|
+
## Features and roadmap
|
|
267
|
+
|
|
268
|
+
How jj2 compares with other Jinja2 command-line tools, as of December 2026:
|
|
269
|
+
|
|
270
|
+
| | jj2 | [jinja2-cli] | [jinjanator] | [j2cli] |
|
|
271
|
+
|----|----|----|----|----|
|
|
272
|
+
| Latest release | [![jj2 on PyPI]][3] | [![jinja2-cli on PyPI]][4] | [![jinjanator on PyPI]][5] | 0.3.10 (2019) |
|
|
273
|
+
| Status | beta, active | stable; no release since 2026-04 | stable, active | archived (2024) |
|
|
274
|
+
| Made for | shell scripts and makefiles: config and dotfile builds | general use, also in Docker images | shell scripts | shell scripts, Docker containers |
|
|
275
|
+
| Python | 3.12 and later | 3.8 and later | 3.11 and later | 2 and 3 |
|
|
276
|
+
| Data files | many, [squashed] | many, deep merge | one | one |
|
|
277
|
+
| Data formats | JSON, YAML, INI, env, ENV | JSON, INI, env, querystring, TOML; YAML, XML, HJSON, JSON5 as extras | JSON, YAML, INI, dotenv; TOML, XML as plugins | JSON, YAML, INI, env |
|
|
278
|
+
| Format override | for each file: `data.cfg:ini` | one `-f` for all files | `-f` | `-f` |
|
|
279
|
+
| Data under a key | `web=nginx.json` | — | — | — |
|
|
280
|
+
| Environment variables | as data: `:ENV`, `env=:ENV`; `env()` | `environ()` | `env()`, `--import-env` | `env()`, `--import-env` |
|
|
281
|
+
| Values on the command line | planned: [\#9] | `-D key=value` | — | — |
|
|
282
|
+
| Template search paths | `-T DIR` | `-I DIR` | — | — |
|
|
283
|
+
| Custom filters and tests | Python files | Python modules (`-F`), extensions (`-e`) | Python files, plugins | Python files |
|
|
284
|
+
| Customization | `--customize` hooks | Jinja2 options as flags | `--customize` hooks, plugins | `--customize` hooks |
|
|
285
|
+
| Dependency file for make | planned: [\#10] | — | — | — |
|
|
286
|
+
| Render a directory | planned: [\#18] | — | — | — |
|
|
287
|
+
|
|
288
|
+
The planned features are GitHub issues, grouped in [milestones]. If jj2 does not
|
|
289
|
+
do something that you need, [open an issue].
|
|
290
|
+
|
|
291
|
+
[jinja2-cli]: https://github.com/mattrobenolt/jinja2-cli
|
|
292
|
+
[jinjanator]: https://github.com/kpfleming/jinjanator
|
|
293
|
+
[j2cli]: https://github.com/kolypto/j2cli
|
|
294
|
+
[jj2 on PyPI]: https://img.shields.io/pypi/v/jj2
|
|
295
|
+
[3]: https://pypi.org/project/jj2/
|
|
296
|
+
[jinja2-cli on PyPI]: https://img.shields.io/pypi/v/jinja2-cli
|
|
297
|
+
[4]: https://pypi.org/project/jinja2-cli/
|
|
298
|
+
[jinjanator on PyPI]: https://img.shields.io/pypi/v/jinjanator
|
|
299
|
+
[5]: https://pypi.org/project/jinjanator/
|
|
300
|
+
[squashed]: #context-squashing
|
|
301
|
+
[\#9]: https://github.com/m000/jj2/issues/9
|
|
302
|
+
[\#10]: https://github.com/m000/jj2/issues/10
|
|
303
|
+
[\#18]: https://github.com/m000/jj2/issues/18
|
|
304
|
+
[milestones]: https://github.com/m000/jj2/milestones
|
|
305
|
+
[open an issue]: https://github.com/m000/jj2/issues
|
|
306
|
+
|
|
307
|
+
## Credits
|
|
308
|
+
|
|
309
|
+
jj2 is inspired by and builds on [kolypto/j2cli] and [mattrobenolt/jinja2-cli].
|
|
310
|
+
|
|
311
|
+
[kolypto/j2cli]: https://github.com/kolypto/j2cli
|
|
312
|
+
[mattrobenolt/jinja2-cli]: https://github.com/mattrobenolt/jinja2-cli
|