Ejemplo n.º 1
0
def test_metamodel_for_file(language_registered):
    """
    Test finding a meta-model for the given file or file pattern.
    """
    mm = metamodel_for_file('mymodel.test')
    assert isinstance(mm, TextXMetaModel)

    mm = metamodel_for_file('*.test')
    assert isinstance(mm, TextXMetaModel)

    mm = metamodel_for_file('somefile*.test')
    assert isinstance(mm, TextXMetaModel)
Ejemplo n.º 2
0
def metamodel_for_file_or_default_metamodel(filename, the_metamodel):
    from textx import metamodel_for_file
    from textx.exceptions import TextXRegistrationError
    try:
        return metamodel_for_file(filename)
    except TextXRegistrationError:
        return the_metamodel
Ejemplo n.º 3
0
    def check(ctx,
              model_files,
              language=None,
              grammar=None,
              ignore_case=False):
        """
        Check/validate model given its file path. If grammar is given use it to
        construct the meta-model. If language is given use it to retrieve the
        registered meta-model.

        Examples:

        \b
        # textX language is built-in, so always registered:
        textx check entity.tx

        \b
        # If the language is not registered you must provide the grammar:
        textx check person.ent --grammar entity.tx

        \b
        # or if we have language registered (see: text list-languages) it's just:
        textx check person.ent

        \b
        # Use "--language" if meta-model can't be deduced by file extension:
        textx check person.txt --language entity

        \b
        # Or to check multiple model files and deduce meta-model by extension
        textx check *

        """  # noqa

        debug = ctx.obj['debug']

        try:
            per_file_metamodel = False
            if grammar:
                metamodel = metamodel_from_file(grammar,
                                                debug=debug,
                                                ignore_case=ignore_case)
            elif language:
                metamodel = metamodel_for_language(language)
            else:
                per_file_metamodel = True

            for model_file in model_files:
                if per_file_metamodel:
                    metamodel = metamodel_for_file(model_file)

                metamodel.model_from_file(model_file, debug=debug)
                click.echo("{}: OK.".format(os.path.abspath(model_file)))

        except TextXRegistrationError as e:
            raise click.ClickException(e.message)

        except TextXError as e:
            raise click.ClickException(str(e))
Ejemplo n.º 4
0
def test_metamodel_for_file_with_params(language_registered):
    """
    Test that passing in kwargs to `metamodel_for_file` call will create a
    brand new meta-model while the old instance will be returned if no `kwargs`
    are given.
    """
    class MyModel:
        pass

    mm = metamodel_for_file('*.test', ignore_case=True, classes=[MyModel])
    assert mm.ignore_case
    assert 'MyModel' in mm.user_classes

    # Now we can call without kwargs and we still get the same instance
    mm2 = metamodel_for_file('*.test')
    assert mm is mm2

    # But, passing kwargs again creates a new meta-model
    mm3 = metamodel_for_file('*.test', ignore_case=False, classes=[MyModel])
    assert not mm3.ignore_case
    assert 'MyModel' in mm.user_classes
Ejemplo n.º 5
0
    def generate(ctx,
                 model_files,
                 output_path,
                 language,
                 target,
                 overwrite,
                 grammar=None,
                 ignore_case=False):
        """
        Run code generator on a provided model(s).

        For example::

        \b
        # Generate PlantUML output from .flow models
        textx generate mymodel.flow --target PlantUML

        \b
        # or with defined output folder
        textx generate mymodel.flow -o rendered_model/ --target PlantUML

        \b
        # To chose language by name and not by file pattern use --language
        textx generate *.flow --language flow --target PlantUML

        \b
        # Use --overwrite to overwrite target files
        textx generate mymodel.flow --target PlantUML --overwrite

        \b
        # In all above cases PlantUML generator must be registered, i.e.:
        $ textx list-generators
        flow-dsl -> PlantUML  Generating PlantUML visualization from flow-dsl

        \b
        # If the source language is not registered you can use the .tx grammar
        # file for parsing but the language name used will be `any`.
        textx generate --grammar Flow.tx --target dot mymodel.flow


        """

        debug = ctx.obj['debug']
        click.echo('Generating {} target from models:'.format(target))

        try:
            per_file_metamodel = False
            if grammar:
                metamodel = metamodel_from_file(grammar,
                                                debug=debug,
                                                ignore_case=ignore_case)
                language = 'any'
            elif language:
                metamodel = metamodel_for_language(language)
            else:
                per_file_metamodel = True

            # Find all custom arguments
            model_files = list(model_files)
            model_files_without_args = []
            custom_args = {}
            while model_files:
                m = model_files.pop(0)
                if m.startswith('--'):
                    arg_name = m[2:]
                    if not model_files or model_files[0].startswith('--'):
                        # Boolean argument
                        custom_args[arg_name] = True
                    else:
                        custom_args[arg_name] = model_files.pop(0).strip('"\'')
                else:
                    model_files_without_args.append(m)

            # Call generator for each model file
            for model_file in model_files_without_args:

                click.echo(os.path.abspath(model_file))

                if per_file_metamodel:
                    language = language_for_file(model_file).name
                    metamodel = metamodel_for_file(model_file)

                # Get custom args that match defined model parameters and pass
                # them in to be available to model processors.
                model_params = {
                    k: v
                    for k, v in custom_args.items()
                    if k in metamodel.model_param_defs
                }

                model = metamodel.model_from_file(model_file, **model_params)
                generator = generator_for_language_target(
                    language, target, any_permitted=per_file_metamodel)

                generator(metamodel, model, output_path, overwrite, debug,
                          **custom_args)

        except TextXRegistrationError as e:
            raise click.ClickException(e.message)

        except TextXError as e:
            raise click.ClickException(str(e))
Ejemplo n.º 6
0
    def generate(ctx, model_files, output_path, language, target, overwrite,
                 grammar=None, ignore_case=False):
        """
        Run code generator on a provided model(s).

        For example::

        \b
        # Generate PlantUML output from .flow models
        textx generate mymodel.flow --target PlantUML

        \b
        # or with defined output folder
        textx generate mymodel.flow -o rendered_model/ --target PlantUML

        \b
        # To chose language by name and not by file pattern use --language
        textx generate *.flow --language flow --target PlantUML

        \b
        # Use --overwrite to overwrite target files
        textx generate mymodel.flow --target PlantUML --overwrite

        \b
        # In all above cases PlantUML generator must be registered, i.e.:
        $ textx list-generators
        flow-dsl -> PlantUML  Generating PlantUML visualization from flow-dsl

        \b
        # If the source language is not registered you can use the .tx grammar
        # file for parsing but the language name used will be `any`.
        textx generate --grammar Flow.tx --target dot mymodel.flow


        """

        debug = ctx.obj['debug']
        click.echo('Generating {} target from models:'.format(target))

        try:
            per_file_metamodel = False
            if grammar:
                metamodel = metamodel_from_file(grammar, debug=debug,
                                                ignore_case=ignore_case)
                language = 'any'
            elif language:
                metamodel = metamodel_for_language(language)
            else:
                per_file_metamodel = True

            # Find all custom arguments
            model_files = list(model_files)
            model_files_without_args = []
            custom_args = {}
            while model_files:
                m = model_files.pop(0)
                if m.startswith('--'):
                    custom_args[m[2:]] = model_files.pop(0).strip('"\'')
                else:
                    model_files_without_args.append(m)

            # Call generator for each model file
            for model_file in model_files_without_args:

                click.echo(os.path.abspath(model_file))

                if per_file_metamodel:
                    language = language_for_file(model_file).name
                    metamodel = metamodel_for_file(model_file)
                model = metamodel.model_from_file(model_file)
                generator = generator_for_language_target(
                    language, target, any_permitted=per_file_metamodel)

                generator(metamodel, model, output_path, overwrite, debug,
                          **custom_args)

        except TextXRegistrationError as e:
            click.echo(e.message)

        except TextXError as e:
            click.echo(e)