Passed
Push — main ( 445c1a...2d946b )
by Sat CFDI
05:21
created

satcfdi.xelement   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 84.91%

Importance

Changes 0
Metric Value
wmc 21
eloc 62
dl 0
loc 83
ccs 45
cts 53
cp 0.8491
rs 10
c 0
b 0
f 0

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, parser
7 1
from .transform.objectify import cfdi_objectify
8 1
from .transform.xmlify import cfdi_xmlify
9
10
11 1
class XElement(ScalarMap, Representable):
12 1
    tag = None
13
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
    def copy(self) -> 'XElement':
31 1
        el = XElement(super().copy())
32 1
        el.tag = self.tag
33 1
        return el
34
35 1
    @classmethod
36 1
    def from_xml(cls, xml_root) -> 'XElement':
37 1
        obj = cfdi_objectify[xml_root.tag](cls, xml_root)
38 1
        if not isinstance(obj, XElement):
39 1
            obj = XElement(obj)
40 1
            obj.tag = xml_root.tag
41 1
        return obj
42
43 1
    @classmethod
44 1
    def from_file(cls, filename) -> 'XElement':
45 1
        return cls.from_xml(etree.parse(filename, parser=parser).getroot())
46
47 1
    @classmethod
48 1
    def from_string(cls, string) -> 'XElement':
49 1
        return cls.from_xml(etree.fromstring(string, parser=parser))
50
51 1
    def xml_write(self, target, pretty_print=False, xml_declaration=True):
52
        xml = self.to_xml()
53
        et = etree.ElementTree(xml)
54
        et.write(
55
            target,
56
            xml_declaration=xml_declaration,
57
            encoding="UTF-8",
58
            pretty_print=pretty_print
59
        )
60
61 1
    def xml_bytes(self, pretty_print=False, xml_declaration=True, validate=False, include_schema_location=False) -> bytes:
62 1
        xml = self.to_xml(validate=validate, include_schema_location=include_schema_location)
63 1
        return etree.tostring(xml, xml_declaration=xml_declaration, encoding="UTF-8", pretty_print=pretty_print)
64
65 1
    def json_write(self, target, pretty_print=False):
66
        if isinstance(target, str):
67
            with open(target, 'w') as f:
68
                json.dump(self, f, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
69
                return
70
71
        json.dump(self, target, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
72
73 1
    def json_str(self, pretty_print=False) -> str:
74 1
        return json.dumps(self, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
75
76
    def __repr__(self):
77
        # return '%s.%s(%s)' % (self.__class__.__module__,
78
        #                       self.__class__.__qualname__,
79
        #                       f'{repr(self.tag)}, {super().__repr__()}')
80
        return '%s(%s)' % (
81
            self.__class__.__qualname__,
82
            f'{dict.__repr__(self)}'
83
        )
84