Coder Social home page Coder Social logo

avielyo10 / jinja2-tools Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 2.0 52 KB

Use Jinja2 templates via cli

Home Page: https://pypi.org/project/jinja2-tools/

License: GNU General Public License v3.0

Python 100.00%
jinja2 jinja2-cli jinja2-tools jinja2-templates ansible-template ansible cli

jinja2-tools's Introduction

Jinja2 tools

Use Jinja2 templates via cli

Install

$ pip install jinja2-tools

Usage

Usage: jinja render [OPTIONS]
Options:
  -d, --data TEXT          PATH to YAML or JSON data file, URL or '-' for
                           stdin.

  -t, --template TEXT      PATH to directory or to any file that uses Jinja,
                           URL or '-' for stdin.

  -v, --verbose
  -tb, --no-trim-blocks    Disable trim blocks.
  -lb, --no-lstrip-blocks  Disable lstrip blocks.
  -o, --output PATH        PATH for output, stdout by default.
  -e, --extra-var TEXT     key value pair separated by '='. 'value' will be
                           treated as JSON or as a string in case of JSON
                           decoding error. This will take precedence over
                           'data'.

  --help                   Show this message and exit.

Whitespace Control

trim_blocks and lstrip_blocks are used by default, to disable them use -tb or -lb respectively.

Examples

  • Use path from the filesystem for data & template:

    ➜ jinja render -d examples/data/data.yaml -t examples/templates/template.yaml
    (1)
    ip access-list extended al-hq-in
    (2)
        (3)    remark Allow traffic from hq to local office
        (4)(2)
        (3)    permit 10.0.0.0/22 10.100.0.0/24
        (5)(6)
    (7)
    
    # All ACLs have been generated
    
  • Use stdin for data & URL for template, also disable trim blocks & lstrip blocks:

    ➜ jinja render -d - -t https://raw.githubusercontent.com/Avielyo10/jinja2-tools/master/examples/templates/template.yaml -lb -tb < examples/data/data.yaml
    (1)
    ip access-list extended al-hq-in
      (2)
        (3)
        remark Allow traffic from hq to local office
        (4)
      (2)
        (3)
        permit 10.0.0.0/22 10.100.0.0/24
        (5)
      (6)
    (7)
    
    # All ACLs have been generated
    
  • Verbose:

    ➜ jinja render -d examples/data/data.yaml -t examples/templates/template.yaml -v     
    ---------- [Data] ----------
    {
      "access_lists": {
        "al-hq-in": [
          {
            "action": "remark",
            "text": "Allow traffic from hq to local office"
          },
          {
            "action": "permit",
            "src": "10.0.0.0/22",
            "dst": "10.100.0.0/24"
          }
        ]
      }
    }
    
    ---------- [Template] ----------
    {% for acl, acl_lines in access_lists.items() %}(1)
    ip access-list extended {{ acl }}
      {% for line in acl_lines %}(2)
        (3){% if line.action == "remark" %}
        remark {{ line.text }}
        (4){% elif line.action == "permit" %}
        permit {{ line.src }} {{ line.dst }}
        (5){% endif %}
      {% endfor %}(6)
    {% endfor %}(7)
    
    # All ACLs have been generated
    
    (1)
    ip access-list extended al-hq-in
    (2)
        (3)    remark Allow traffic from hq to local office
        (4)(2)
        (3)    permit 10.0.0.0/22 10.100.0.0/24
        (5)(6)
    (7)
    
    # All ACLs have been generated
    
  • Pass the data using multiple extra vars:

    ➜ jinja render -t examples/templates/template.yaml \
    -e access_lists='{"al-hq-in": [{"action": "remark", "text": "Allow traffic from hq to local office"}, {"action": "permit", "src": "10.0.0.0/22", "dst": "10.100.0.0/24"}]}' \
    -e message=world \
    -v
    ---------- [ExtraVars] ----------
    {
      "access_lists": {
        "al-hq-in": [
          {
            "action": "remark",
            "text": "Allow traffic from hq to local office"
          },
          {
            "action": "permit",
            "src": "10.0.0.0/22",
            "dst": "10.100.0.0/24"
          }
        ]
      },
      "message": "world"
    } 
    
    ---------- [Template] ----------
    {% for acl, acl_lines in access_lists.items() %}(1)
    ip access-list extended {{ acl }}
      {% for line in acl_lines %}(2)
        (3){% if line.action == "remark" %}
        remark {{ line.text }}
        (4){% elif line.action == "permit" %}
        permit {{ line.src }} {{ line.dst }}
        (5){% endif %}
      {% endfor %}(6)
    {% endfor %}(7)
    hello {{ message }}!
    # All ACLs have been generated 
    
    (1)
    ip access-list extended al-hq-in
    (2)
        (3)    remark Allow traffic from hq to local office
        (4)(2)
        (3)    permit 10.0.0.0/22 10.100.0.0/24
        (5)(6)
    (7)
    hello world!
    # All ACLs have been generated
    
  • Use directory option

    ➜ jinja render -d examples/data/data.yaml -t examples/ -o test/
    ➜ tree test/
    test/
    ├── config
    │   ├── example.ini
    │   └── example.properties
    ├── data
    │   ├── data.json
    │   └── data.yaml
    └── templates
        ├── lookup.yaml
        └── template.yaml
    
    3 directories, 6 files
    ➜ cat test/templates/template.yaml
    (1)
    ip access-list extended al-hq-in
    (2)
        (3)    remark Allow traffic from hq to local office
        (4)(2)
        (3)    permit 10.0.0.0/22 10.100.0.0/24
        (5)(6)
    (7)
    hello jinja!
    # All ACLs have been generated
    

jinja2-tools's People

Contributors

avielyo10 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lmelwyn epdhowwd

jinja2-tools's Issues

loading data as yaml with date or datetime fails with json exception

some data file:

example:
    - someDate: 2022-02-02

Using the cli interface:

$ jinja --version
jinja, version 1.0.7

Error is:

$ jinja render -t foobar -d data.yaml
Traceback (most recent call last):
  File "/usr/local/bin/jinja", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2_tools/cli.py", line 53, in render
    data = Data(data, verbose).get_data()
  File "/usr/local/lib/python3.10/site-packages/jinja2_tools/objects.py", line 69, in get_data
    print_verbose({'title': '[Data]', 'content': json.dumps(
  File "/usr/local/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/usr/local/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/usr/local/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/local/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/local/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/encoder.py", line 325, in _iterencode_list
    yield from chunks
  File "/usr/local/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/local/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File "/usr/local/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type date is not JSON serializable

Looking into objects.py there is a json.dumps invocation on the loaded yaml which seems not be able to cope with date and timestamps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.