Passed
Push — main ( 4a0f00...43c999 )
by Sat CFDI
01:49
created

FacturacionEnvironment.glob()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
from html import escape as html_escape
2
3
import jinja2
4
from jinja2 import Environment, Undefined
5
from jinja2.filters import do_mark_safe
6
# noinspection PyUnresolvedReferences
7
from satcfdi.transform.catalog import CATALOGS
8
from satcfdi.transform.helpers import iterate as h_iterate
9
10
11
class FacturacionEnvironment(Environment):
12
    @property
13
    def glob(self):
14
        def sub(f):
15
            self.globals[f.__name__] = f
16
            return f
17
18
        return sub
19
20
    def __init__(self):
21
        super().__init__(
22
            loader=jinja2.FileSystemLoader(searchpath=['templates']),
23
            autoescape=True,
24
            trim_blocks=True,
25
            lstrip_blocks=True,
26
            undefined=jinja2.StrictUndefined,
27
        )
28
29
        @self.glob
30
        def iterate(v):
31
            if isinstance(v, Undefined):
32
                return v
33
            return h_iterate(v)
34
35
36
environment_default = FacturacionEnvironment()
37
38
39
def tag(text, tag):
40
    return '<' + tag + '>' + text + '</' + tag + '>'
41
42
43
def finalize_html(val):
44
    return do_mark_safe(
45
        tag(html_escape(val), "b")
46
    )
47
48
49
environment_bold_escaped = FacturacionEnvironment()
50
environment_bold_escaped.finalize = finalize_html
51