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