Passed
Push — main ( 7699bf...3c69d9 )
by Sat CFDI
01:46
created

FacturacionEnvironment.filter()   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 filter(self):
14
        def sub(f):
15
            self.filters[f.__name__] = f
16
            return f
17
18
        return sub
19
20
    @property
21
    def glob(self):
22
        def sub(f):
23
            self.globals[f.__name__] = f
24
            return f
25
26
        return sub
27
28
    def __init__(self):
29
        super().__init__(
30
            loader=jinja2.FileSystemLoader(searchpath=['templates']),
31
            autoescape=True,
32
            trim_blocks=True,
33
            lstrip_blocks=True,
34
            undefined=jinja2.StrictUndefined,
35
        )
36
37
        @self.glob
38
        def iterate(v):
39
            if isinstance(v, Undefined):
40
                return v
41
            return h_iterate(v)
42
43
        @self.filter
44
        def bold(k):
45
            return do_mark_safe(
46
                tag(html_escape(k), "b")
47
            )
48
49
50
facturacion_environment = FacturacionEnvironment()
51
52
53
def tag(text, tag):
54
    return '<' + tag + '>' + text + '</' + tag + '>'
55