Skip to main content

Build status Code test coverage Documentation Status

textX-jinja

Jinja based framework for textX generators. Use if you need to generate a set of template-based files from textX models.

How to use?

  1. Create a folder containing files and folder which resemble the structure you want to generate. Each file may be a Jinja template (should end with .jinja extension).

  2. File names may contain variable parts in the form __<varible name>__ (double underscores around the variable name). These parts of file names will be replaced by the value of the variable from the generator context. If the variable is iterable, a file will be created for each object. In that case, the value for substitution in the file name will be created by function map_names(still WIP!), if given, or str of the object itself. The object will be available in the template context under the name obj. If the variable is of bool type the file will be skipped if the variable value is False.

  3. In your textX project register a generator (see registration).

    from textx import generator
    from textxjinja import textx_jinja_generator
    
    @generator('mylang', 'mytarget')
    def mygenerator(metamodel, model, output_path, overwrite, debug):
        # template directory
        template_folder = os.path.join(os.path.dirname(__file__), 'templates')
    
        # create context dict with all variables that should be accessible
        # by templates
        context = {'some_variable': 'some value'}
        
        # Optionally provide Jinja filters
        def striptabs(s):
            return re.sub(r'^[ \t]+', '', s, flags=re.M)
        filters = {
            'striptabs': striptabs
        }
    
        # call the generator
        textx_jinja_generator(template_folder, output_path, context, overwrite, filters)
    
  4. Install your project (recommended is the usage of Python virtual environment):

    pip install -e <path to your project>
    
  5. Run your textX generator as usual:

    $ textx generate ...
    

The generator will use the template folder for rendering files using Jinja template engine. All files from the template folder which are not Jinja templates (don't end with .jinja' extensions) will be copied over to the target folder unchanged (variable substitutions in file names still apply).

As a full example of its usage see startproject generator in textX-dev project. Templates for the startproject command are here.

Metadata

Release files for textX-jinja 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for textX-jinja 0.4.0
File Size Uploaded
textx_jinja-0.4.0.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for textX-jinja 0.4.0
File Interpreter ABI Platform
textx_jinja-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / textx_jinja-0.4.0.tar.gz

Download URL textx_jinja-0.4.0.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ce206224a355abd6dd9c4307c520ce5bbb82acb638c49c2ae2b90f4bff32e5b3
BLAKE2b-256 checksum
How to use checksums
4bb669f47a8a793e65b4b70e8072e4ee00750563caf5bf162b3d7f8d8c8afb3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.3

Release files / textx_jinja-0.4.0-py3-none-any.whl

Download URL textx_jinja-0.4.0-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
411e927af193d11ea559e4c6e849e1e87f403dd44d5114c6b4944b867fddb2a2
BLAKE2b-256 checksum
How to use checksums
48a805cad647ed579623a8b168a17da8850cb42d4307d47a2bfe0912c60065e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.3

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page