1
|
|
|
import logging |
2
|
|
|
from decimal import Decimal |
3
|
|
|
from html import escape as html_escape |
4
|
|
|
|
5
|
|
|
import jinja2 |
6
|
|
|
import yaml |
7
|
|
|
from jinja2 import Environment |
8
|
|
|
from jinja2.filters import do_mark_safe |
9
|
|
|
from satcfdi import Code |
10
|
|
|
from satcfdi.pacs import sat |
11
|
|
|
from satcfdi.transform.helpers import Xint |
12
|
|
|
|
13
|
|
|
logger = logging.getLogger() |
14
|
|
|
sat_manager = sat.SAT() |
15
|
|
|
|
16
|
|
|
environment_default = Environment( |
17
|
|
|
loader=jinja2.FileSystemLoader(searchpath=['templates']), |
18
|
|
|
autoescape=False, |
19
|
|
|
trim_blocks=True, |
20
|
|
|
lstrip_blocks=True, |
21
|
|
|
undefined=jinja2.StrictUndefined, |
22
|
|
|
) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def finalize_html(val): |
26
|
|
|
return do_mark_safe( |
27
|
|
|
tag(html_escape(val), "b") |
28
|
|
|
) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
environment_bold_escaped = Environment( |
32
|
|
|
loader=jinja2.FileSystemLoader(searchpath=['templates']), |
33
|
|
|
autoescape=True, |
34
|
|
|
trim_blocks=True, |
35
|
|
|
lstrip_blocks=True, |
36
|
|
|
undefined=jinja2.StrictUndefined, |
37
|
|
|
finalize=finalize_html |
38
|
|
|
) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class LocalData(dict): |
42
|
|
|
file_source = None |
43
|
|
|
|
44
|
|
|
def __init__(self): |
45
|
|
|
with open(self.file_source, "r", encoding="utf-8") as fs: |
46
|
|
|
super().__init__(yaml.safe_load(fs)) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
class ConfigManager(LocalData): |
50
|
|
|
file_source = "config.yaml" |
51
|
|
|
|
52
|
|
|
def folio(self): |
53
|
|
|
return self["Folio"] |
54
|
|
|
|
55
|
|
|
def serie(self): |
56
|
|
|
return self["Serie"] |
57
|
|
|
|
58
|
|
|
def inc_folio(self): |
59
|
|
|
self["Folio"] += 1 |
60
|
|
|
self.save() |
61
|
|
|
|
62
|
|
|
def save(self): |
63
|
|
|
with open(self.file_source, "w", encoding="utf-8") as fs: |
64
|
|
|
yaml.dump_all([self], fs, Dumper=yaml.SafeDumper, encoding="utf-8", allow_unicode=True, sort_keys=False) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
class ClientsManager(LocalData): |
68
|
|
|
file_source = "clients.yaml" |
69
|
|
|
|
70
|
|
|
def __init__(self): |
71
|
|
|
super().__init__() |
72
|
|
|
for k in self: |
73
|
|
|
self[k]["Rfc"] = k |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
class FacturasManager(LocalData): |
77
|
|
|
file_source = "facturas.yaml" |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def tag(text, tag): |
81
|
|
|
return '<' + tag + '>' + text + '</' + tag + '>' |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
yaml.SafeDumper.add_multi_representer(dict, lambda dumper, data: dumper.represent_dict(data)) |
85
|
|
|
yaml.SafeLoader.add_constructor("!decimal", lambda loader, node: Decimal(loader.construct_scalar(node))) |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
def represent_decimal(dumper, data): |
89
|
|
|
return dumper.represent_scalar('tag:yaml.org,2002:str', str(data)) |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def represent_str(dumper, data): |
93
|
|
|
return dumper.represent_scalar('tag:yaml.org,2002:str', str(data)) |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
yaml.SafeDumper.add_representer(Decimal, represent_decimal) |
97
|
|
|
yaml.SafeDumper.add_representer(Code, represent_str) |
98
|
|
|
yaml.SafeDumper.add_representer(Xint, represent_str) |
99
|
|
|
|