command line jinja2 template (yet another j2cli)
Project description
kamidana is yet another jinja2’s cli wrapper.
features
using jinja2 file as template file (basic feature)
various input formats support (json, yaml, toml, …)
batch execution for speed-up (via kamidana-batch)
rendering with individual filters (via –additionals option)
(useful additionals modules (e.g. kamidana.additionals.naming …)
usage
usage: kamidana [-h] [--driver DRIVER] [--loader LOADER] [-d DATA]
[--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}]
[-a ADDITIONALS] [-e EXTENSION]
[-i {yaml,json,toml,csv,tsv,raw,env,md,markdown,spreadsheet}]
[-o OUTPUT_FORMAT] [--dump-context] [--debug] [--dst DST]
[template]
positional arguments:
template
optional arguments:
-h, --help show this help message and exit
--driver DRIVER default: kamidana.driver:Driver
--loader LOADER default: kamidana.loader:TemplateLoader
-d DATA, --data DATA support yaml, json, toml
--logging {CRITICAL,FATAL,ERROR,WARN,WARNING,INFO,DEBUG,NOTSET}
-a ADDITIONALS, --additionals ADDITIONALS
-e EXTENSION, --extension EXTENSION
-i {yaml,json,toml,csv,tsv,raw,env,md,markdown,spreadsheet}, --input-format {yaml,json,toml,csv,tsv,raw,env,md,markdown,spreadsheet}
-o OUTPUT_FORMAT, --output-format OUTPUT_FORMAT
--dump-context
--debug
--dst DST
examples
example (basic)
$ kamidana examples/readme/src/00/nginx.jinja2 --data examples/readme/src/00/data.json
server {
listen 80;
server_name localhost;
root /var/www/project;
index index.htm;
access_log /var/log/nginx/http.access.log combined;
error_log /var/log/nginx/http.error.log;
}
examples/readme/src/00/nginx.jinja2
server {
listen 80;
server_name {{ nginx.hostname }};
root {{ nginx.webroot }};
index index.htm;
access_log {{ nginx.logdir }}/http.access.log combined;
error_log {{ nginx.logdir }}/http.error.log;
}
examples/readme/src/00/data.json
{
"nginx": {
"hostname": "localhost",
"webroot": "/var/www/project",
"logdir": "/var/log/nginx"
}
}
More over, passing data with stdin. (please doen’t forget to add –input-format option)
$ echo '{"nginx": {"logdir": "/tmp/logs/nginx"}}' | kamidana --input-format json examples/readme/src/00/nginx.jinja2 --data examples/readme/src/00/data.json
server {
listen 80;
server_name localhost;
root /var/www/project;
index index.htm;
access_log /tmp/logs/nginx/http.access.log combined;
error_log /tmp/logs/nginx/http.error.log;
}
example2 (–additionals)
builtin addtional modules
$ kamidana --additionals=kamidana.additionals.naming examples/readme/src/01/use-naming.jinja2
singular, plurals
- days|singularize -> day
- day|pluralize -> days
- people|singularize -> person
- person|pluralize -> people
to {snake_case, kebab-case, camelCase}
- fooBarBoo|snakecase -> foo_bar_boo
- fooBarBoo|kebabcase -> foo-bar-boo
- foo_bar_boo|camelcase -> fooBarBoo
more information: see kamidana.additionals.naming module
examples/readme/src/01/use-naming.jinja2
singular, plurals
- days|singularize -> {{"days"|singularize}}
- day|pluralize -> {{"day"|pluralize}}
- people|singularize -> {{"people"|singularize}}
- person|pluralize -> {{"person"|pluralize}}
to {snake_case, kebab-case, camelCase}
- fooBarBoo|snakecase -> {{"fooBarBoo"|snakecase}}
- fooBarBoo|kebabcase -> {{"fooBarBoo"|kebabcase}}
- foo_bar_boo|camelcase -> {{"foo_bar_boo"|camelcase}}
more information: see kamidana.additionals.naming module
or kamidana -a naming is also OK (shortcut).
individual additional modules
$ kamidana --additionals=examples/readme/src/01/additionals.py --data=examples/readme/src/01/data.yaml examples/readme/src/01/hello.jinja2
bye, world!!
examples/readme/src/01/hello.jinja2
{% if 19 is night %}
{{night}}, {{name|surprised}}
{% else %}
{{daytime}}, {{name|surprised}}
{% endif %}
examples/readme/src/01/additionals.py
from kamidana import (
as_filter,
as_globals_generator,
as_test,
)
@as_filter
def surprised(v):
return "{}!!".format(v)
@as_globals_generator
def generate_globals():
return {"daytime": "hello", "night": "bye"}
@as_test
def night(hour):
return 19 <= hour or hour < 3
examples/readme/src/01/data.yaml
name: world
example3 (using jinja2 extensions)
$ kamidana -e with_ -e do -e loopcontrols examples/readme/src/02/use-extension.jinja2
hello
world
hello
## counting
- 1
- 2
- 4
## do
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
examples/readme/src/02/use-extension.jinja2
{# with with. with_ extension is used. #}
{% with msg = "hello"%}
{{msg}}
{% with msg = "world"%}
{{msg}}
{% endwith %}
{{msg}}
{% endwith %}
## counting
{# with break and continue. loopcontrolls extension is used. #}
{% for i in range(10) %}
{% if i % 3 == 0 %}{% continue %} {% endif %}
{% if i == 5 %}{% break %} {% endif %}
- {{i}}
{% endfor %}
## do
{% set xs = [] %}
{% for i in range(10) %}
{% do xs.append(i) %}
{% endfor %}
{{xs}}
example4 (batch execution)
TODO. see this
debugging
–dump-context
–debug
dump context
$ kamidana --dump-context --data=examples/readme/src/10/data.yaml
{
"name": "foo",
"age": 20,
"friends": [
"bar",
"boo"
],
"template_filename": null
}
and be able to merge two files.
$ kamidana --dump-context --data=examples/readme/src/10/data.yaml --data=examples/readme/src/10/data2.yaml
{
"name": "foo",
"age": 21,
"friends": [
"bar",
"baz"
],
"template_filename": null
}
then
examples/readme/src/10/data.yaml
name: foo
age: 20
friends:
- bar
- boo
examples/readme/src/10/data2.yaml
age: 21
friends:
- bar
- baz
0.6.2
template is not passed, act as –dump-context (#21)
same behaviour -a kamidana.additonals.reader and -a reader (shortcuts) (#18)
update readme.rst
0.6.1
kamidana, fix: –dst option is ignored
kamidana-batch, fix: kamidana-batch ignore –logging option
0.6.0
kamidana.additionals.naming
0.5.0
new kamidana-batch command
changing addtionals.reader’s current working directory path
0.4.1
fix bug (multi -a option)
0.4.0
add kamidana.additionals.reader
0.3.0
add –extension option
add –dump-context option
0.2.1
rename Loader to TemplateLoader
0.2.0
–loader option is added
interfaces are defined
0.1.0
first release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for kamidana-0.6.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1c3506b595ca8f1bd23c5b694ed08e137bc00a86d1d0fa9ab1b67f08eb7119c |
|
MD5 | e2f2eac8a9f98693df872c6e548b8c98 |
|
BLAKE2b-256 | e8f5b52e84fe10b6eefe249ffbecfdba412f57d885ca0401880c08ea224a7054 |