Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jenisys/simplegen
A code generator with a simple data model based on Jinja2.
https://github.com/jenisys/simplegen
Last synced: about 1 month ago
JSON representation
A code generator with a simple data model based on Jinja2.
- Host: GitHub
- URL: https://github.com/jenisys/simplegen
- Owner: jenisys
- Created: 2013-07-06T07:58:04.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-07-06T08:12:42.000Z (over 11 years ago)
- Last Synced: 2023-04-09T14:50:07.355Z (over 1 year ago)
- Size: 102 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
simplegen
================================================================================:License: BSD
:Requires: Python >= 2.5 (not: Python >= 3.0)
:Requires: Jinja2 >= 2.5 (license: BSD)This project provides a simple code generator that uses a simple data model.
The data model can be defined via:* command-line define statements
* JSON files
* ...`Jinja2`_ is used as powerful template engine for the templates.
This allows to use it for simple and more complex examples.SEE ALSO:
* http://jinja.pocoo.org/
* http://www.json.org/.. _Jinja2: http://jinja.pocoo.org/
EXAMPLE
-------------------------------------------------------------------------------First you define the data model for this example.
It basically consists of a person database in the "persons.json" file.
The persons are just enumerated in an array/list... code-block:: json
{
"persons": [
{ "name": "Alice", "age": 25, "gender": "female" },
{ "name": "Bob", "age": 31, "gender": "male" },
{ "name": "Camille", "age": 16, "gender": "female" }
]
}Then you provide a textual template file in Jinja2 syntax for your report.
.. code-block:: jinja
# -- TEMPLATE-FILE: template.txt.in
{%- set person = persons[person_id|int] %}person.id: ${person_id}
person.name: ${person.name}
person.age: ${person.age}Make me 10 years older: ${person.age + 10}
__END-OF-FILE__
.. note::
1. Placeholders use the ${placeholder} instead of {{placeholder}} syntax.
2. The set-operation in the second line selects the person from the
persons database by using the person_id as index into the array.
3. To ensure that "person_id" is a number, its value is converted into
an integer by using the "|int" Jinja filter expression.Next you generate the report for Bob with "person_id=1" as index.
Simplegen writes to stdout because no --output option is used... code-block:: bash
$ simplegen --data=persons.json -D person_id=1 template.txt.in
SIMPLEGEN: Loading persons.json ...
SIMPLEGEN: Processing template.txt.in ...
TEMPLATE-FILE: template.txt.inperson.id: 1
person.name: Bob
person.age: 31Make me 10 years older: 41
__END-OF-FILE__