Skip to main content

Meta-language for DSL implementation inspired by Xtext

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textx_jinja-0.4.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

textx_jinja-0.4.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file textx_jinja-0.4.0.tar.gz.

File metadata

  • Download URL: textx_jinja-0.4.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for textx_jinja-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ce206224a355abd6dd9c4307c520ce5bbb82acb638c49c2ae2b90f4bff32e5b3
MD5 90b33105ccb53b6be63e9536b39567e5
BLAKE2b-256 4bb669f47a8a793e65b4b70e8072e4ee00750563caf5bf162b3d7f8d8c8afb3d

See more details on using hashes here.

File details

Details for the file textx_jinja-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: textx_jinja-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for textx_jinja-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 411e927af193d11ea559e4c6e849e1e87f403dd44d5114c6b4944b867fddb2a2
MD5 42edc8adf0d915f2622f3109c8101347
BLAKE2b-256 48a805cad647ed579623a8b168a17da8850cb42d4307d47a2bfe0912c60065e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page