elasticai.creator.template#

Module Contents#

Classes#

TemplateParameter

AnalysingTemplateParameter

A template parameter type that needs an analysis phase before the replacement.

TemplateBuilder

Builds a template based on a given prototype.

API#

class elasticai.creator.template.TemplateParameter[source]#

Bases: typing.Protocol

regex: str#

None

A regular expression to match what should be turned into a template parameter.

replace(match: dict[str, str]) str[source]#

Define how to replace the matched pattern by the template parameter.

The match is a dictionary of named capture groups. In the most simple cases you can just return f"${self.name}" to replace the full match by the template parameter. But you can also use the match to create a more complex replacement.

The full match is available as match[""].

class elasticai.creator.template.AnalysingTemplateParameter[source]#

Bases: elasticai.creator.template.TemplateParameter, typing.Protocol

A template parameter type that needs an analysis phase before the replacement.

You could e.g., use this to find the first class definition in a python file and use that class name to build the regex parameter and the replacement to replace all occurences of that class name by a template parameter.

Example:

class ClassNameParameter:
    name = "class_name"
    analyse_regex = r"class (?P<name>[a-zA-Z_][a-zA-Z0-9_]*)"

    def __init__(self):
        self._class_name = None

    def analyse(self, match: dict[str, str]) -> None:
        # Store first found class name
        if self._class_name is None:
            self._class_name = match["name"]

    @property
    def regex(self) -> str:
        # After analysis, look for all occurrences of the class name
        return self._class_name

    def replace(self, match: dict[str, str]) -> str:
        # Replace with template parameter
        return f"${self.name}"

# Usage:
builder = TemplateBuilder()
builder.set_prototype('''
    class MyClass:
        def method(self):
            return MyClass()
''')
builder.add_parameter(ClassNameParameter())
template = builder.build()
print(template.render(dict(class_name="MyNewClass"))) # Will replace all occurrences of "MyClass" with "MyNewClass"
analyse_regex: str#

None

Will be used during the analysis phase.

See analyse for more information.

analyse(match: dict[str, str]) None[source]#

Called for each match of analyse_regex during the analysis phase.

Parameters:

match – A dictionary of the named capture groups.

class elasticai.creator.template.TemplateBuilder[source]#

Builds a template based on a given prototype.

The builder takes a prototype as a string and creates a template from it, based on specified template parameter types. A template parameter type specifies how a pattern inside the prototype shall be converted into a template parameter.

Two different kinds of template parameter types are supported:

TemplateParameterType

will use a regular expression .regex to search for occurences of a pattern and replace them by the result of calling .replace on the found match. Usually the new value is a template parameter, e.g., "$my_template_param".

Note

in case of multiple overlapping parameter types the first one added to the builder will take precedence.

AnalysingTemplateParameterType

works like TemplateParameterType but will first go through an analysing phase. This is useful to let the regular expression depend on the content of the prototype. E.g., we could find the first defined class in a python file and replace all occurences of that class name with a template parameter.

Once build, the template is cached. The cache is invalidated as soon as new parameter types are added or the underlying prototype is changed.

Initialization

set_prototype(prototype: str | tuple[str, ...] | list[str]) elasticai.creator.template.TemplateBuilder[source]#
add_parameter(_type: elasticai.creator.template.TemplateParameter | elasticai.creator.template.AnalysingTemplateParameter) elasticai.creator.template.TemplateBuilder[source]#
build() str[source]#