1
|
1 |
|
import json |
2
|
1 |
|
from lxml import etree |
3
|
|
|
|
4
|
1 |
|
from .transform import SchemaCollector, cfdi_schemas, validate_xsd |
5
|
1 |
|
from .utils import ScalarMap, parser |
6
|
1 |
|
from .transform.objectify import cfdi_objectify |
7
|
1 |
|
from .transform.xmlify import cfdi_xmlify |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
class XElement(ScalarMap): |
11
|
1 |
|
tag = None |
12
|
|
|
|
13
|
1 |
|
def to_xml(self, validate=False, include_schema_location=False) -> etree.Element: |
14
|
1 |
|
xml = cfdi_xmlify[self.tag](self) |
15
|
|
|
|
16
|
1 |
|
if validate or include_schema_location: |
17
|
1 |
|
col = SchemaCollector() |
18
|
1 |
|
cfdi_schemas[self.tag](col, self) |
19
|
1 |
|
if validate: |
20
|
1 |
|
validate_xsd(xml, col.base) |
21
|
1 |
|
if include_schema_location: |
22
|
1 |
|
xml.attrib['{http://www.w3.org/2001/XMLSchema-instance}schemaLocation'] = " ".join(col.schemas) |
23
|
|
|
|
24
|
1 |
|
return xml |
25
|
|
|
|
26
|
1 |
|
def process(self, validate=False) -> 'XElement': |
27
|
1 |
|
return XElement.from_xml(self.to_xml(validate=validate)) |
28
|
|
|
|
29
|
1 |
|
def copy(self) -> 'XElement': |
30
|
1 |
|
el = XElement(super().copy()) |
31
|
1 |
|
el.tag = self.tag |
32
|
1 |
|
return el |
33
|
|
|
|
34
|
1 |
|
@classmethod |
35
|
1 |
|
def from_xml(cls, xml_root) -> 'XElement': |
36
|
1 |
|
obj = cfdi_objectify[xml_root.tag](cls, xml_root) |
37
|
1 |
|
if not isinstance(obj, XElement): |
38
|
1 |
|
obj = XElement(obj) |
39
|
1 |
|
obj.tag = xml_root.tag |
40
|
1 |
|
return obj |
41
|
|
|
|
42
|
1 |
|
@classmethod |
43
|
1 |
|
def from_file(cls, filename) -> 'XElement': |
44
|
1 |
|
return cls.from_xml(etree.parse(filename, parser=parser).getroot()) |
45
|
|
|
|
46
|
1 |
|
@classmethod |
47
|
1 |
|
def from_string(cls, string) -> 'XElement': |
48
|
1 |
|
return cls.from_xml(etree.fromstring(string, parser=parser)) |
49
|
|
|
|
50
|
1 |
|
def xml_write(self, target, pretty_print=False, xml_declaration=True): |
51
|
|
|
xml = self.to_xml() |
52
|
|
|
et = etree.ElementTree(xml) |
53
|
|
|
et.write( |
54
|
|
|
target, |
55
|
|
|
xml_declaration=xml_declaration, |
56
|
|
|
encoding="UTF-8", |
57
|
|
|
pretty_print=pretty_print |
58
|
|
|
) |
59
|
|
|
|
60
|
1 |
|
def xml_bytes(self, pretty_print=False, xml_declaration=True, validate=False, include_schema_location=False) -> bytes: |
61
|
1 |
|
xml = self.to_xml(validate=validate, include_schema_location=include_schema_location) |
62
|
1 |
|
return etree.tostring(xml, xml_declaration=xml_declaration, encoding="UTF-8", pretty_print=pretty_print) |
63
|
|
|
|
64
|
1 |
|
def json_write(self, target, pretty_print=False): |
65
|
|
|
if isinstance(target, str): |
66
|
|
|
with open(target, 'w') as f: |
67
|
|
|
json.dump(self, f, ensure_ascii=False, default=str, indent=2 if pretty_print else None) |
68
|
|
|
return |
69
|
|
|
|
70
|
|
|
json.dump(self, target, ensure_ascii=False, default=str, indent=2 if pretty_print else None) |
71
|
|
|
|
72
|
1 |
|
def json_str(self, pretty_print=False) -> str: |
73
|
1 |
|
return json.dumps(self, ensure_ascii=False, default=str, indent=2 if pretty_print else None) |
74
|
|
|
|
75
|
|
|
def __repr__(self): |
76
|
|
|
# return '%s.%s(%s)' % (self.__class__.__module__, |
77
|
|
|
# self.__class__.__qualname__, |
78
|
|
|
# f'{repr(self.tag)}, {super().__repr__()}') |
79
|
|
|
return '%s(%s)' % ( |
80
|
|
|
self.__class__.__qualname__, |
81
|
|
|
f'{dict.__repr__(self)}' |
82
|
|
|
) |
83
|
|
|
|