Skip to content

Commit 79d99df

Browse files
authored
Feature/custom loader (#2)
* [#1] add possibility to override env Also provides an example of how to implement a custom loader Fixes #1 * update docs to match new endpoint filename * add doc about custom loader * fix link
1 parent 2ac1a00 commit 79d99df

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ Every now and then, it's pretty useful to just have a cli tool that does the job
44

55
## Usage: ##
66
```
7-
python j2-lint.py my-template.j2
7+
python j2lint.py my-template.j2
88
```
99

1010
It accepts multiple arguments so shell expansion and/or combining with find is no issue:
1111

1212
```
13-
python j2-lint.py *.j2
14-
find src -type f -name "*.j2" -exec python j2-lint.py '{}' +
13+
python j2lint.py *.j2
14+
find src -type f -name "*.j2" -exec python j2lint.py '{}' +
1515
```
1616

17+
## Usage with custom filters, tests, etc ##
18+
19+
If you want to use this linter with custom filters, tests, etc, you can easily
20+
extend the main cli endpoint by passing in a `env` keyword argument.
21+
22+
The file [custom_check_example.py](custom_check_example.py) provides a working example for the filter
23+
'to_nice_json'.
24+
25+
Note that for linting it is not necessary to refer to the actual implementation
26+
of the filters, jinja2 only needs to know they exist.

custom_check_example.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
This is an example of how to extend the default environment
3+
and/or loader to add your own filter logic.
4+
"""
5+
import jinja2
6+
from j2lint import main, AbsolutePathLoader
7+
8+
filters = ['to_nice_json']
9+
10+
env = jinja2.Environment(loader=AbsolutePathLoader())
11+
env.filters.update({name: lambda: None for name in filters})
12+
13+
main(env=env)

j2-lint.py renamed to j2lint.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
@author Gerard van Helden <gerard@zicht.nl>
2+
@author Gerard van Helden <drm@melp.nl>
33
@license DBAD, see <http://www.dbad-license.org/>
44
55
Simple j2 linter, useful for checking jinja2 template syntax
@@ -19,9 +19,7 @@ def get_source(self, environment, path):
1919
return source, path, lambda: mtime == os.path.getmtime(path)
2020

2121

22-
def check(template, out, err):
23-
env = jinja2.Environment(loader=AbsolutePathLoader())
24-
22+
def check(template, out, err, env=jinja2.Environment(loader=AbsolutePathLoader())):
2523
try:
2624
env.get_template(template)
2725
out.write("%s: Syntax OK\n" % template)
@@ -33,11 +31,13 @@ def check(template, out, err):
3331
err.write("%s: Syntax check failed: %s in %s at %d\n" % (template, e.message, e.filename, e.lineno))
3432
return 1
3533

36-
37-
if __name__ == "__main__":
34+
def main(**kwargs):
3835
import sys
39-
4036
try:
41-
sys.exit(reduce(lambda r, fn: r + check(fn, sys.stdout, sys.stderr), sys.argv[1:], 0))
37+
sys.exit(reduce(lambda r, fn: r + check(fn, sys.stdout, sys.stderr, **kwargs), sys.argv[1:], 0))
4238
except IndexError:
43-
sys.stdout.write("Usage: j2-lint.py filename [filename ...]\n")
39+
sys.stdout.write("Usage: j2lint.py filename [filename ...]\n")
40+
41+
if __name__ == "__main__":
42+
main()
43+

0 commit comments

Comments
 (0)