Passed
Push — main ( 4d8c5b...036071 )
by Sat CFDI
15:59
created

satcfdi.xelement   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 85.19%

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 85
ccs 46
cts 54
cp 0.8519
rs 10
c 0
b 0
f 0
wmc 21

11 Methods

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