jj2 0.7.0b1__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.0b1/LICENSE +26 -0
- jj2-0.7.0b1/PKG-INFO +257 -0
- jj2-0.7.0b1/README.md +224 -0
- jj2-0.7.0b1/pyproject.toml +115 -0
- jj2-0.7.0b1/pyproject.toml.orig +106 -0
- jj2-0.7.0b1/src/jj2/__init__.py +11 -0
- jj2-0.7.0b1/src/jj2/__main__.py +8 -0
- jj2-0.7.0b1/src/jj2/cli.py +154 -0
- jj2-0.7.0b1/src/jj2/context.py +43 -0
- jj2-0.7.0b1/src/jj2/customize.py +33 -0
- jj2-0.7.0b1/src/jj2/defaults.py +31 -0
- jj2-0.7.0b1/src/jj2/errors.py +56 -0
- jj2-0.7.0b1/src/jj2/filters.py +160 -0
- jj2-0.7.0b1/src/jj2/parsers.py +197 -0
- jj2-0.7.0b1/src/jj2/render.py +108 -0
jj2-0.7.0b1/LICENSE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Copyright (c) 2019-2023, 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.0b1/PKG-INFO
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jj2
|
|
3
|
+
Version: 0.7.0b1
|
|
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
|
+
[](https://github.com/m000/jj2/actions/workflows/ci.yaml)
|
|
35
|
+
[](https://pypi.org/project/jj2/)
|
|
36
|
+
[](https://pypi.org/project/jj2/)
|
|
37
|
+
# jj2 - Juiced Jinja2 command-line tool
|
|
38
|
+
|
|
39
|
+
jj2 (previously `j2cli`) is a command-line tool for templating in
|
|
40
|
+
shell-scripts, leveraging the [Jinja2](http://jinja.pocoo.org/docs/)
|
|
41
|
+
library.
|
|
42
|
+
It supports [several formats](#supported-formats) for loading the context
|
|
43
|
+
used for rendering the Jinja2 templates. Loading environment variables as
|
|
44
|
+
rendering context is also supported.
|
|
45
|
+
|
|
46
|
+
> **Warning**
|
|
47
|
+
> jj2 is in beta, on the way to a full spin-off from the
|
|
48
|
+
> [upstream][j2cli]. The aim is to keep each release usable. However,
|
|
49
|
+
> until the spin-off is complete you should expect:
|
|
50
|
+
> - cli option changes
|
|
51
|
+
> - history rewrites on the development branches
|
|
52
|
+
> - no support for Python <3.12
|
|
53
|
+
>
|
|
54
|
+
> Having said that, you are welcome to use this branch and start an
|
|
55
|
+
> issue if you encounter any problems or have feedback.
|
|
56
|
+
|
|
57
|
+
## jj2 features and roadmap
|
|
58
|
+
|
|
59
|
+
The following planned/implemented features differentiate jj2 from
|
|
60
|
+
its upstreams.
|
|
61
|
+
|
|
62
|
+
- [x] Focus on modern Python (≥3.12). This allows modernizing the codebase.
|
|
63
|
+
- [x] Switch to more modern tooling.
|
|
64
|
+
* [x] [pytest][pytest] (to replace [nose][nose])
|
|
65
|
+
* [x] [ruff][ruff] (to replace [prospector][prospector])
|
|
66
|
+
* [x] [uv][uv] (to replace poetry and tox)
|
|
67
|
+
- [ ] Rendering of multiple templates using the same context in one go.
|
|
68
|
+
Rendering a couple of dozens template one-by-one is fairly slow.
|
|
69
|
+
This should make the tool snappier to use, but also means that
|
|
70
|
+
the command line interface will need to change.
|
|
71
|
+
- [ ] Template dependency analysis to allow better integration with tools
|
|
72
|
+
like [make][make]. Such tools are otherwise oblivious to Jinja2 template
|
|
73
|
+
inheritance/inclusion.
|
|
74
|
+
- [ ] Extended library of Jinja2 filters. This should allow using jj2
|
|
75
|
+
out of the box in a wider range of use cases.
|
|
76
|
+
- [x] Support of *context squashing* (see [below](#context-squashing)),
|
|
77
|
+
to eliminate the need to preprocess context data with external tools.
|
|
78
|
+
|
|
79
|
+
## Getting started
|
|
80
|
+
|
|
81
|
+
### Installation
|
|
82
|
+
```sh
|
|
83
|
+
# as a standalone tool, with uv
|
|
84
|
+
$ uv tool install jj2
|
|
85
|
+
# or with pip
|
|
86
|
+
$ pip install jj2
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
To run jj2 once, without installing it:
|
|
90
|
+
```sh
|
|
91
|
+
$ uvx jj2 config.j2 config.json
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
To try the development branch:
|
|
95
|
+
```sh
|
|
96
|
+
$ uv tool install git+https://github.com/m000/jj2@master-next
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
In a checkout of the repository, run jj2 from the project environment:
|
|
100
|
+
```sh
|
|
101
|
+
$ uv run jj2 config.j2 config.json
|
|
102
|
+
$ uv run python -m jj2 config.j2 config.json
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Basic usage
|
|
106
|
+
```sh
|
|
107
|
+
# render config from template config.j2
|
|
108
|
+
$ jj2 config.j2 config.json -o config
|
|
109
|
+
|
|
110
|
+
# render using yaml data from stdin
|
|
111
|
+
$ wget -O - http://example.com/config.yml | jj2 config.j2 -:yaml
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
For an extensive list of examples, see [docs/examples.md](https://github.com/m000/jj2/blob/master/docs/examples.md).
|
|
115
|
+
|
|
116
|
+
## Context data
|
|
117
|
+
|
|
118
|
+
### Supported formats
|
|
119
|
+
jj2 supports importing context from several different sources:
|
|
120
|
+
|
|
121
|
+
* [JSON][json]: A language-independent data-serialization format, originally derived
|
|
122
|
+
from JavaScript.
|
|
123
|
+
* [YAML][yaml]: A data-serialization language, designed to be human-readable.
|
|
124
|
+
* [INI][ini]: Windows-style configuration files.
|
|
125
|
+
* env: Simple [unix-style][ini] environment variable assignments.
|
|
126
|
+
|
|
127
|
+
For examples with each supported format, see [docs/formats.md](https://github.com/m000/jj2/blob/master/docs/formats.md).
|
|
128
|
+
|
|
129
|
+
### Context squashing
|
|
130
|
+
One of the main strengths of jj2 is that it is not limited to using a single
|
|
131
|
+
data file as context. Several data files—perhaps in different formats—can be
|
|
132
|
+
used to construct the rendering context.
|
|
133
|
+
As the contents of the data files may "overlap", jj2 *recursively squashes*
|
|
134
|
+
their contents to produce the context that will be used for rendering.
|
|
135
|
+
The order of squashing is *from left to right*. I.e. the contents of a data file
|
|
136
|
+
may be overriden by any data files specified *after it* on the command line.
|
|
137
|
+
|
|
138
|
+
Here is a simple example illustrating how context squashing works:
|
|
139
|
+
|
|
140
|
+
* `a.json` contents:
|
|
141
|
+
```json
|
|
142
|
+
{"a": 1, "c": {"x": 2, "y": 3}}
|
|
143
|
+
```
|
|
144
|
+
* `b.json` contents:
|
|
145
|
+
```json
|
|
146
|
+
{"b": 2, "c": {"y": 4}}
|
|
147
|
+
```
|
|
148
|
+
* effective context when rendering with `a.json` and `b.json` (in that order):
|
|
149
|
+
```json
|
|
150
|
+
{"a": 1, "b": 2, "c": {"x": 2, "y": 4}}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Loading data as a context subtree
|
|
154
|
+
By default, the data from each source is squashed into the top level of the
|
|
155
|
+
context. Sources with overlapping keys, or with many keys, such as the
|
|
156
|
+
environment variables, can instead be put under a key. Each data source is
|
|
157
|
+
given as a *data spec*:
|
|
158
|
+
|
|
159
|
+
[dest=]path[:format]
|
|
160
|
+
|
|
161
|
+
* `path`: a data file, or `-` (or nothing) for stdin.
|
|
162
|
+
* `format`: overrides the format given by the file extension, e.g. `settings.cfg:ini`.
|
|
163
|
+
* `dest`: the *context destination*, a key or a dotted key path to put the data
|
|
164
|
+
under, e.g. `web=nginx.json` or `app.host=host.yaml`. The part before the first
|
|
165
|
+
`=` is a destination only if it is a dotted name, so a path like `./a=b.json`
|
|
166
|
+
still works.
|
|
167
|
+
|
|
168
|
+
A source whose top level is a list or a single value, such as a JSON array,
|
|
169
|
+
needs a destination: `items=list.json`. An empty YAML file gives no data.
|
|
170
|
+
|
|
171
|
+
jj2 expands a leading `~` in the path itself, because shells do not always do
|
|
172
|
+
it after `dest=`: zsh never does by default, and bash does not after a dotted
|
|
173
|
+
key path.
|
|
174
|
+
|
|
175
|
+
| Data spec | Where the data goes |
|
|
176
|
+
|---|---|
|
|
177
|
+
| `nginx.json` | the top level |
|
|
178
|
+
| `web=nginx.json` | under `web`: `{{ web.hostname }}` |
|
|
179
|
+
| `app.host=host.yaml` | under `app.host`, squashed with the other data under `app` |
|
|
180
|
+
| `settings.cfg:ini` | the top level, read as INI |
|
|
181
|
+
| `web=-:json` | under `web`, read as JSON from stdin |
|
|
182
|
+
| `env=:ENV` | under `env`, from the environment variables |
|
|
183
|
+
|
|
184
|
+
> [!TIP]
|
|
185
|
+
> In zsh, pressing <kbd>Tab</kbd> after `dest=` does not complete the path by
|
|
186
|
+
> default, because zsh treats `web=ngi` as a single word. Add
|
|
187
|
+
> `setopt magic_equal_subst` to your `~/.zshrc` to complete after `=`, or
|
|
188
|
+
> complete the path first and then add `dest=` in front of it. bash completes
|
|
189
|
+
> after `=` without any setup.
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
## Reference
|
|
193
|
+
`jj2` accepts the following arguments:
|
|
194
|
+
|
|
195
|
+
* `template`: Jinja2 template file to render
|
|
196
|
+
* `data`: one or more data specs, `[dest=]path[:format]`.
|
|
197
|
+
See [Loading data as a context subtree](#loading-data-as-a-context-subtree).
|
|
198
|
+
|
|
199
|
+
Options:
|
|
200
|
+
|
|
201
|
+
* `--format, -f`: format for the data file. The default is `?`: guess from file extension.
|
|
202
|
+
* `--import-env VAR, -e EVAR`: import all environment variables into the template as `VAR`.
|
|
203
|
+
To import environment variables into the global scope, give it an empty string: `--import-env=`.
|
|
204
|
+
(This will overwrite any existing variables!)
|
|
205
|
+
* `-o outfile`: Write rendered template to a file
|
|
206
|
+
* `--undefined={strict, normal, debug}`: Specify the behaviour of jj2 for undefined
|
|
207
|
+
variables. Refer to [Jinja2 docs][jinja2-undefined] for details.
|
|
208
|
+
* `--filters filters.py`: Load custom Jinja2 filters and tests from a Python file.
|
|
209
|
+
Will load all top-level functions and register them as filters.
|
|
210
|
+
This option can be used multiple times to import several files.
|
|
211
|
+
* `--tests tests.py`: Load custom Jinja2 filters and tests from a Python file.
|
|
212
|
+
* `--customize custom.py`: A Python file that implements hooks to fine-tune the jj2 behavior.
|
|
213
|
+
This is fairly advanced stuff, use it only if you really need to customize the way Jinja2 is initialized.
|
|
214
|
+
See [Customization](#customization) for more info.
|
|
215
|
+
|
|
216
|
+
There is some special behavior with environment variables:
|
|
217
|
+
|
|
218
|
+
* When `data` is not provided (data is `-`), `--format` defaults to `env` and thus reads environment variables
|
|
219
|
+
* When `--format=env`, it can read a special "environment variables" file made like this: `env > /tmp/file.env`
|
|
220
|
+
|
|
221
|
+
## Extras
|
|
222
|
+
|
|
223
|
+
### Filters
|
|
224
|
+
For convenience, jj2 offers several additional Jinja2 filters that can be used
|
|
225
|
+
in your templates. These filters should help you avoid having to implement an
|
|
226
|
+
[advanced customization module](#advanced-customization) for many use cases.
|
|
227
|
+
|
|
228
|
+
See [docs/filters.md](https://github.com/m000/jj2/blob/master/docs/filters.md) for details on the available filters.
|
|
229
|
+
|
|
230
|
+
### Advanced customization
|
|
231
|
+
jj2 offers several *hooks* that allow for more advanced customization of its
|
|
232
|
+
operation. This includes:
|
|
233
|
+
|
|
234
|
+
* passing additional keywords to Jinja2 environment
|
|
235
|
+
* modifying the context before it's used for rendering
|
|
236
|
+
* registering custom filters and tests
|
|
237
|
+
|
|
238
|
+
See [docs/advanced.md](https://github.com/m000/jj2/blob/master/docs/advanced.md) for details on advanced customization.
|
|
239
|
+
|
|
240
|
+
## Credits
|
|
241
|
+
jj2 is inspired by and builds on [kolypto/j2cli][j2cli] and
|
|
242
|
+
[mattrobenolt/jinja2-cli][jinja2-cli] tools.
|
|
243
|
+
|
|
244
|
+
[docker]: http://www.docker.com/
|
|
245
|
+
[env]: https://en.wikipedia.org/wiki/Environment_variable#Unix
|
|
246
|
+
[ini]: https://en.wikipedia.org/wiki/INI_file
|
|
247
|
+
[j2cli]: https://github.com/kolypto/j2cli
|
|
248
|
+
[jinja2-cli]: https://github.com/mattrobenolt/jinja2-cli
|
|
249
|
+
[jinja2-undefined]: https://jinja.palletsprojects.com/en/2.10.x/api/#undefined-types
|
|
250
|
+
[json]: https://en.wikipedia.org/wiki/JSON
|
|
251
|
+
[make]: https://www.gnu.org/software/make/
|
|
252
|
+
[nose]: https://nose.readthedocs.io/
|
|
253
|
+
[prospector]: https://prospector.landscape.io/en/master/
|
|
254
|
+
[pytest]: https://docs.pytest.org/
|
|
255
|
+
[ruff]: https://docs.astral.sh/ruff/
|
|
256
|
+
[uv]: https://docs.astral.sh/uv/
|
|
257
|
+
[yaml]: https://en.wikipedia.org/wiki/YAML
|
jj2-0.7.0b1/README.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
[](https://github.com/m000/jj2/actions/workflows/ci.yaml)
|
|
2
|
+
[](https://pypi.org/project/jj2/)
|
|
3
|
+
[](https://pypi.org/project/jj2/)
|
|
4
|
+
# jj2 - Juiced Jinja2 command-line tool
|
|
5
|
+
|
|
6
|
+
jj2 (previously `j2cli`) is a command-line tool for templating in
|
|
7
|
+
shell-scripts, leveraging the [Jinja2](http://jinja.pocoo.org/docs/)
|
|
8
|
+
library.
|
|
9
|
+
It supports [several formats](#supported-formats) for loading the context
|
|
10
|
+
used for rendering the Jinja2 templates. Loading environment variables as
|
|
11
|
+
rendering context is also supported.
|
|
12
|
+
|
|
13
|
+
> **Warning**
|
|
14
|
+
> jj2 is in beta, on the way to a full spin-off from the
|
|
15
|
+
> [upstream][j2cli]. The aim is to keep each release usable. However,
|
|
16
|
+
> until the spin-off is complete you should expect:
|
|
17
|
+
> - cli option changes
|
|
18
|
+
> - history rewrites on the development branches
|
|
19
|
+
> - no support for Python <3.12
|
|
20
|
+
>
|
|
21
|
+
> Having said that, you are welcome to use this branch and start an
|
|
22
|
+
> issue if you encounter any problems or have feedback.
|
|
23
|
+
|
|
24
|
+
## jj2 features and roadmap
|
|
25
|
+
|
|
26
|
+
The following planned/implemented features differentiate jj2 from
|
|
27
|
+
its upstreams.
|
|
28
|
+
|
|
29
|
+
- [x] Focus on modern Python (≥3.12). This allows modernizing the codebase.
|
|
30
|
+
- [x] Switch to more modern tooling.
|
|
31
|
+
* [x] [pytest][pytest] (to replace [nose][nose])
|
|
32
|
+
* [x] [ruff][ruff] (to replace [prospector][prospector])
|
|
33
|
+
* [x] [uv][uv] (to replace poetry and tox)
|
|
34
|
+
- [ ] Rendering of multiple templates using the same context in one go.
|
|
35
|
+
Rendering a couple of dozens template one-by-one is fairly slow.
|
|
36
|
+
This should make the tool snappier to use, but also means that
|
|
37
|
+
the command line interface will need to change.
|
|
38
|
+
- [ ] Template dependency analysis to allow better integration with tools
|
|
39
|
+
like [make][make]. Such tools are otherwise oblivious to Jinja2 template
|
|
40
|
+
inheritance/inclusion.
|
|
41
|
+
- [ ] Extended library of Jinja2 filters. This should allow using jj2
|
|
42
|
+
out of the box in a wider range of use cases.
|
|
43
|
+
- [x] Support of *context squashing* (see [below](#context-squashing)),
|
|
44
|
+
to eliminate the need to preprocess context data with external tools.
|
|
45
|
+
|
|
46
|
+
## Getting started
|
|
47
|
+
|
|
48
|
+
### Installation
|
|
49
|
+
```sh
|
|
50
|
+
# as a standalone tool, with uv
|
|
51
|
+
$ uv tool install jj2
|
|
52
|
+
# or with pip
|
|
53
|
+
$ pip install jj2
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
To run jj2 once, without installing it:
|
|
57
|
+
```sh
|
|
58
|
+
$ uvx jj2 config.j2 config.json
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
To try the development branch:
|
|
62
|
+
```sh
|
|
63
|
+
$ uv tool install git+https://github.com/m000/jj2@master-next
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
In a checkout of the repository, run jj2 from the project environment:
|
|
67
|
+
```sh
|
|
68
|
+
$ uv run jj2 config.j2 config.json
|
|
69
|
+
$ uv run python -m jj2 config.j2 config.json
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Basic usage
|
|
73
|
+
```sh
|
|
74
|
+
# render config from template config.j2
|
|
75
|
+
$ jj2 config.j2 config.json -o config
|
|
76
|
+
|
|
77
|
+
# render using yaml data from stdin
|
|
78
|
+
$ wget -O - http://example.com/config.yml | jj2 config.j2 -:yaml
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
For an extensive list of examples, see [docs/examples.md](https://github.com/m000/jj2/blob/master/docs/examples.md).
|
|
82
|
+
|
|
83
|
+
## Context data
|
|
84
|
+
|
|
85
|
+
### Supported formats
|
|
86
|
+
jj2 supports importing context from several different sources:
|
|
87
|
+
|
|
88
|
+
* [JSON][json]: A language-independent data-serialization format, originally derived
|
|
89
|
+
from JavaScript.
|
|
90
|
+
* [YAML][yaml]: A data-serialization language, designed to be human-readable.
|
|
91
|
+
* [INI][ini]: Windows-style configuration files.
|
|
92
|
+
* env: Simple [unix-style][ini] environment variable assignments.
|
|
93
|
+
|
|
94
|
+
For examples with each supported format, see [docs/formats.md](https://github.com/m000/jj2/blob/master/docs/formats.md).
|
|
95
|
+
|
|
96
|
+
### Context squashing
|
|
97
|
+
One of the main strengths of jj2 is that it is not limited to using a single
|
|
98
|
+
data file as context. Several data files—perhaps in different formats—can be
|
|
99
|
+
used to construct the rendering context.
|
|
100
|
+
As the contents of the data files may "overlap", jj2 *recursively squashes*
|
|
101
|
+
their contents to produce the context that will be used for rendering.
|
|
102
|
+
The order of squashing is *from left to right*. I.e. the contents of a data file
|
|
103
|
+
may be overriden by any data files specified *after it* on the command line.
|
|
104
|
+
|
|
105
|
+
Here is a simple example illustrating how context squashing works:
|
|
106
|
+
|
|
107
|
+
* `a.json` contents:
|
|
108
|
+
```json
|
|
109
|
+
{"a": 1, "c": {"x": 2, "y": 3}}
|
|
110
|
+
```
|
|
111
|
+
* `b.json` contents:
|
|
112
|
+
```json
|
|
113
|
+
{"b": 2, "c": {"y": 4}}
|
|
114
|
+
```
|
|
115
|
+
* effective context when rendering with `a.json` and `b.json` (in that order):
|
|
116
|
+
```json
|
|
117
|
+
{"a": 1, "b": 2, "c": {"x": 2, "y": 4}}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Loading data as a context subtree
|
|
121
|
+
By default, the data from each source is squashed into the top level of the
|
|
122
|
+
context. Sources with overlapping keys, or with many keys, such as the
|
|
123
|
+
environment variables, can instead be put under a key. Each data source is
|
|
124
|
+
given as a *data spec*:
|
|
125
|
+
|
|
126
|
+
[dest=]path[:format]
|
|
127
|
+
|
|
128
|
+
* `path`: a data file, or `-` (or nothing) for stdin.
|
|
129
|
+
* `format`: overrides the format given by the file extension, e.g. `settings.cfg:ini`.
|
|
130
|
+
* `dest`: the *context destination*, a key or a dotted key path to put the data
|
|
131
|
+
under, e.g. `web=nginx.json` or `app.host=host.yaml`. The part before the first
|
|
132
|
+
`=` is a destination only if it is a dotted name, so a path like `./a=b.json`
|
|
133
|
+
still works.
|
|
134
|
+
|
|
135
|
+
A source whose top level is a list or a single value, such as a JSON array,
|
|
136
|
+
needs a destination: `items=list.json`. An empty YAML file gives no data.
|
|
137
|
+
|
|
138
|
+
jj2 expands a leading `~` in the path itself, because shells do not always do
|
|
139
|
+
it after `dest=`: zsh never does by default, and bash does not after a dotted
|
|
140
|
+
key path.
|
|
141
|
+
|
|
142
|
+
| Data spec | Where the data goes |
|
|
143
|
+
|---|---|
|
|
144
|
+
| `nginx.json` | the top level |
|
|
145
|
+
| `web=nginx.json` | under `web`: `{{ web.hostname }}` |
|
|
146
|
+
| `app.host=host.yaml` | under `app.host`, squashed with the other data under `app` |
|
|
147
|
+
| `settings.cfg:ini` | the top level, read as INI |
|
|
148
|
+
| `web=-:json` | under `web`, read as JSON from stdin |
|
|
149
|
+
| `env=:ENV` | under `env`, from the environment variables |
|
|
150
|
+
|
|
151
|
+
> [!TIP]
|
|
152
|
+
> In zsh, pressing <kbd>Tab</kbd> after `dest=` does not complete the path by
|
|
153
|
+
> default, because zsh treats `web=ngi` as a single word. Add
|
|
154
|
+
> `setopt magic_equal_subst` to your `~/.zshrc` to complete after `=`, or
|
|
155
|
+
> complete the path first and then add `dest=` in front of it. bash completes
|
|
156
|
+
> after `=` without any setup.
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
## Reference
|
|
160
|
+
`jj2` accepts the following arguments:
|
|
161
|
+
|
|
162
|
+
* `template`: Jinja2 template file to render
|
|
163
|
+
* `data`: one or more data specs, `[dest=]path[:format]`.
|
|
164
|
+
See [Loading data as a context subtree](#loading-data-as-a-context-subtree).
|
|
165
|
+
|
|
166
|
+
Options:
|
|
167
|
+
|
|
168
|
+
* `--format, -f`: format for the data file. The default is `?`: guess from file extension.
|
|
169
|
+
* `--import-env VAR, -e EVAR`: import all environment variables into the template as `VAR`.
|
|
170
|
+
To import environment variables into the global scope, give it an empty string: `--import-env=`.
|
|
171
|
+
(This will overwrite any existing variables!)
|
|
172
|
+
* `-o outfile`: Write rendered template to a file
|
|
173
|
+
* `--undefined={strict, normal, debug}`: Specify the behaviour of jj2 for undefined
|
|
174
|
+
variables. Refer to [Jinja2 docs][jinja2-undefined] for details.
|
|
175
|
+
* `--filters filters.py`: Load custom Jinja2 filters and tests from a Python file.
|
|
176
|
+
Will load all top-level functions and register them as filters.
|
|
177
|
+
This option can be used multiple times to import several files.
|
|
178
|
+
* `--tests tests.py`: Load custom Jinja2 filters and tests from a Python file.
|
|
179
|
+
* `--customize custom.py`: A Python file that implements hooks to fine-tune the jj2 behavior.
|
|
180
|
+
This is fairly advanced stuff, use it only if you really need to customize the way Jinja2 is initialized.
|
|
181
|
+
See [Customization](#customization) for more info.
|
|
182
|
+
|
|
183
|
+
There is some special behavior with environment variables:
|
|
184
|
+
|
|
185
|
+
* When `data` is not provided (data is `-`), `--format` defaults to `env` and thus reads environment variables
|
|
186
|
+
* When `--format=env`, it can read a special "environment variables" file made like this: `env > /tmp/file.env`
|
|
187
|
+
|
|
188
|
+
## Extras
|
|
189
|
+
|
|
190
|
+
### Filters
|
|
191
|
+
For convenience, jj2 offers several additional Jinja2 filters that can be used
|
|
192
|
+
in your templates. These filters should help you avoid having to implement an
|
|
193
|
+
[advanced customization module](#advanced-customization) for many use cases.
|
|
194
|
+
|
|
195
|
+
See [docs/filters.md](https://github.com/m000/jj2/blob/master/docs/filters.md) for details on the available filters.
|
|
196
|
+
|
|
197
|
+
### Advanced customization
|
|
198
|
+
jj2 offers several *hooks* that allow for more advanced customization of its
|
|
199
|
+
operation. This includes:
|
|
200
|
+
|
|
201
|
+
* passing additional keywords to Jinja2 environment
|
|
202
|
+
* modifying the context before it's used for rendering
|
|
203
|
+
* registering custom filters and tests
|
|
204
|
+
|
|
205
|
+
See [docs/advanced.md](https://github.com/m000/jj2/blob/master/docs/advanced.md) for details on advanced customization.
|
|
206
|
+
|
|
207
|
+
## Credits
|
|
208
|
+
jj2 is inspired by and builds on [kolypto/j2cli][j2cli] and
|
|
209
|
+
[mattrobenolt/jinja2-cli][jinja2-cli] tools.
|
|
210
|
+
|
|
211
|
+
[docker]: http://www.docker.com/
|
|
212
|
+
[env]: https://en.wikipedia.org/wiki/Environment_variable#Unix
|
|
213
|
+
[ini]: https://en.wikipedia.org/wiki/INI_file
|
|
214
|
+
[j2cli]: https://github.com/kolypto/j2cli
|
|
215
|
+
[jinja2-cli]: https://github.com/mattrobenolt/jinja2-cli
|
|
216
|
+
[jinja2-undefined]: https://jinja.palletsprojects.com/en/2.10.x/api/#undefined-types
|
|
217
|
+
[json]: https://en.wikipedia.org/wiki/JSON
|
|
218
|
+
[make]: https://www.gnu.org/software/make/
|
|
219
|
+
[nose]: https://nose.readthedocs.io/
|
|
220
|
+
[prospector]: https://prospector.landscape.io/en/master/
|
|
221
|
+
[pytest]: https://docs.pytest.org/
|
|
222
|
+
[ruff]: https://docs.astral.sh/ruff/
|
|
223
|
+
[uv]: https://docs.astral.sh/uv/
|
|
224
|
+
[yaml]: https://en.wikipedia.org/wiki/YAML
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "jj2"
|
|
3
|
+
version = "0.7.0b1"
|
|
4
|
+
description = "Juiced Jinja2 command-line tool"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "BSD-2-Clause"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
keywords = [
|
|
10
|
+
"Jinja2",
|
|
11
|
+
"templating",
|
|
12
|
+
"command-line",
|
|
13
|
+
"CLI",
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Environment :: Console",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Intended Audience :: System Administrators",
|
|
20
|
+
"Natural Language :: English",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
"Programming Language :: Python :: 3.15",
|
|
28
|
+
"Topic :: Software Development",
|
|
29
|
+
"Topic :: Text Processing :: General",
|
|
30
|
+
]
|
|
31
|
+
dependencies = [
|
|
32
|
+
"jinja2>=3.1",
|
|
33
|
+
"python-dotenv>=1.0",
|
|
34
|
+
"pyyaml>=6.0",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[[project.authors]]
|
|
38
|
+
name = "Manolis Stamatogiannakis"
|
|
39
|
+
email = "mstamat@gmail.com"
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://github.com/m000/jj2"
|
|
43
|
+
Repository = "https://github.com/m000/jj2"
|
|
44
|
+
Issues = "https://github.com/m000/jj2/issues"
|
|
45
|
+
Changelog = "https://github.com/m000/jj2/blob/master/CHANGELOG.md"
|
|
46
|
+
|
|
47
|
+
[project.scripts]
|
|
48
|
+
jj2 = "jj2:render"
|
|
49
|
+
|
|
50
|
+
[dependency-groups]
|
|
51
|
+
dev = [
|
|
52
|
+
"pytest>=9",
|
|
53
|
+
"pytest-cov>=7",
|
|
54
|
+
"pytest-mock>=3.14",
|
|
55
|
+
"ruff>=0.17",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
[build-system]
|
|
59
|
+
requires = ["uv_build>=0.9.8,<0.14"]
|
|
60
|
+
build-backend = "uv_build"
|
|
61
|
+
|
|
62
|
+
[tool.pytest.ini_options]
|
|
63
|
+
testpaths = ["tests"]
|
|
64
|
+
pythonpath = ["tests"]
|
|
65
|
+
strict = true
|
|
66
|
+
addopts = [
|
|
67
|
+
"-ra",
|
|
68
|
+
"--import-mode=importlib",
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
[tool.ruff]
|
|
72
|
+
target-version = "py312"
|
|
73
|
+
line-length = 100
|
|
74
|
+
src = [
|
|
75
|
+
"src",
|
|
76
|
+
"tests",
|
|
77
|
+
]
|
|
78
|
+
extend-exclude = ["#*.py"]
|
|
79
|
+
|
|
80
|
+
[tool.ruff.lint]
|
|
81
|
+
select = [
|
|
82
|
+
"B",
|
|
83
|
+
"C4",
|
|
84
|
+
"C90",
|
|
85
|
+
"E",
|
|
86
|
+
"EXE",
|
|
87
|
+
"F",
|
|
88
|
+
"I",
|
|
89
|
+
"PL",
|
|
90
|
+
"PT",
|
|
91
|
+
"PTH",
|
|
92
|
+
"Q",
|
|
93
|
+
"SIM",
|
|
94
|
+
"UP",
|
|
95
|
+
"W",
|
|
96
|
+
]
|
|
97
|
+
ignore = [
|
|
98
|
+
"E731",
|
|
99
|
+
"Q000",
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
[tool.ruff.lint.per-file-ignores]
|
|
103
|
+
"src/**" = [
|
|
104
|
+
"B",
|
|
105
|
+
"I",
|
|
106
|
+
"PTH",
|
|
107
|
+
"SIM",
|
|
108
|
+
"UP",
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
[tool.ruff.lint.mccabe]
|
|
112
|
+
max-complexity = 15
|
|
113
|
+
|
|
114
|
+
[tool.ruff.format]
|
|
115
|
+
quote-style = "preserve"
|