Skip to content

Template

blackline.query.template

logger = logging.getLogger(__name__) module-attribute

Template

Bases: object

Source code in BAR /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/query/template.py
class Template(object):
    def __init__(
        self,
        params: TemplateParams,
        trim_blocks=True,
        lstrip_blocks=True,
        where: Optional[str] = None,
        *args,
        **kwrgs,
    ) -> None:
        if where is not None:
            params.where_template += " " + where
        self.params = params
        self.where = where
        self.env = Environment(
            *args, trim_blocks=trim_blocks, lstrip_blocks=lstrip_blocks, **kwrgs
        )
        self.layout = self.env.from_string(layout)
        self.deidentifier_marco_base = self.env.from_string(deidentifier_marco_base)
        self._load_marcos()

    @property
    def template(self) -> JinjaTemplate:
        return self.env.from_string(self.template_str())

    def _load_marcos(self) -> Environment:
        return self.env.globals.update(
            {
                "redact": self.redact_macro(),
                "replace": self.replace_macro(),
                "mask": self.mask_macro(),
            }
        )

    def _deidentifer_macro(self, name, method) -> str:
        sql_str = self.deidentifier_marco_base.module.deidentifier_marco_base(
            macro_name=name, assignment=getattr(self.params, method)
        )
        logger.debug(f"{name} macro: \n{sql_str}")

        return getattr(self.env.from_string(sql_str).module, name.lower())

    def redact_macro(self) -> str:
        return self._deidentifer_macro(name="Redact", method="redact_template")

    def replace_macro(self) -> str:
        return self._deidentifer_macro(name="Replace", method="replace_template")

    def mask_macro(self) -> str:
        return self._deidentifer_macro(name="Mask", method="mask_template")

    def template_str(self) -> str:
        # if self.where is not None:
        #     self.params.where_template += " " + self.where

        return self.env.from_string(query).render(
            layout=self.layout,
            update_statement=self.params.update_template,
            set_statement=self.params.set_template,
            where_statement=self.params.where_template,
        )

    def render_template(self, *args, **kwargs) -> str:
        template = self.template()
        return self.env.from_string(template).render(*args, **kwargs)

deidentifier_marco_base = self.env.from_string(deidentifier_marco_base) instance-attribute

env = Environment(*args, trim_blocks=trim_blocks, lstrip_blocks=lstrip_blocks, **kwrgs) instance-attribute

layout = self.env.from_string(layout) instance-attribute

params = params instance-attribute

template: JinjaTemplate property

where = where instance-attribute

__init__(params, trim_blocks=True, lstrip_blocks=True, where=None, *args, **kwrgs)

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/query/template.py
def __init__(
    self,
    params: TemplateParams,
    trim_blocks=True,
    lstrip_blocks=True,
    where: Optional[str] = None,
    *args,
    **kwrgs,
) -> None:
    if where is not None:
        params.where_template += " " + where
    self.params = params
    self.where = where
    self.env = Environment(
        *args, trim_blocks=trim_blocks, lstrip_blocks=lstrip_blocks, **kwrgs
    )
    self.layout = self.env.from_string(layout)
    self.deidentifier_marco_base = self.env.from_string(deidentifier_marco_base)
    self._load_marcos()

mask_macro()

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/query/template.py
def mask_macro(self) -> str:
    return self._deidentifer_macro(name="Mask", method="mask_template")

redact_macro()

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/query/template.py
def redact_macro(self) -> str:
    return self._deidentifer_macro(name="Redact", method="redact_template")

render_template(*args, **kwargs)

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/query/template.py
def render_template(self, *args, **kwargs) -> str:
    template = self.template()
    return self.env.from_string(template).render(*args, **kwargs)

replace_macro()

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/query/template.py
def replace_macro(self) -> str:
    return self._deidentifer_macro(name="Replace", method="replace_template")

template_str()

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/query/template.py
def template_str(self) -> str:
    # if self.where is not None:
    #     self.params.where_template += " " + self.where

    return self.env.from_string(query).render(
        layout=self.layout,
        update_statement=self.params.update_template,
        set_statement=self.params.set_template,
        where_statement=self.params.where_template,
    )