Skip to content

String template

StringTemplateExtract

Bases: ExtractionMethod

Accepts a template and output_key. Existing terms are added to the template.

Method name: string_template

Example Configuration

.. code-block:: yaml

- method: string_template
  inputs:
    template: {hello}/{goodbye}/{hello}/bonjour.html
    output_key: manifest_url
Source code in extraction_methods/plugins/string_template.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class StringTemplateExtract(ExtractionMethod):
    """
    Accepts a template and output_key. Existing terms are added to the template.

    **Method name:** ``string_template``

    Example Configuration:
        .. code-block:: yaml

            - method: string_template
              inputs:
                template: {hello}/{goodbye}/{hello}/bonjour.html
                output_key: manifest_url
    """

    input_class = StringTemplateInput

    @update_input
    def run(self, body: dict[str, Any]) -> dict[str, Any]:

        terms = re.findall("{(.*?)}", self.input.template)

        if self.input.descructive:
            format_terms = {term: body.pop(term, "") for term in terms}
        else:
            format_terms = {term: body.get(term, "") for term in terms}

        body[self.input.output_key] = self.input.template.format(**format_terms)

        return body

StringTemplateInput

Bases: Input

Model for String Template Input.

Parameters:

Name Type Description Default
template str

Template to follow.

required
descructive bool

True if terms should be removed after templating.

False
output_key str

key to output to.

required
Source code in extraction_methods/plugins/string_template.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class StringTemplateInput(Input):
    """
    Model for String Template Input.
    """

    template: str = Field(
        description="Template to follow.",
    )
    descructive: bool = Field(
        default=False,
        description="True if terms should be removed after templating.",
    )
    output_key: str = Field(
        description="key to output to.",
    )