|
3 | 3 | # License MIT
|
4 | 4 |
|
5 | 5 | import abc
|
| 6 | +import warnings |
| 7 | +from dataclasses import is_dataclass |
6 | 8 | from datetime import datetime
|
7 | 9 | from datetime import timedelta
|
8 | 10 | from datetime import timezone
|
9 |
| - |
10 |
| -from erpbrasil.assinatura.assinatura import Assinatura |
11 | 11 | from lxml import etree
|
12 | 12 | from lxml.etree import _Element
|
13 | 13 |
|
| 14 | +from xsdata.formats.dataclass.serializers import XmlSerializer |
| 15 | +from xsdata.formats.dataclass.serializers.config import SerializerConfig |
| 16 | + |
14 | 17 | from .resposta import analisar_retorno_raw
|
15 | 18 |
|
16 | 19 | # Fix Python 2.x.
|
@@ -39,28 +42,52 @@ def __init__(self, transmissao):
|
39 | 42 | self._transmissao = transmissao
|
40 | 43 |
|
41 | 44 | def _generateds_to_string_etree(self, ds, pretty_print=False):
|
42 |
| - |
43 |
| - if type(ds) == _Element: |
44 |
| - return etree.tostring(ds), ds |
45 |
| - if isinstance(ds, str): |
46 |
| - return ds, etree.fromstring(ds) |
47 |
| - # if isinstance(ds, unicode): |
48 |
| - # return ds, etree.fromstring(ds) |
| 45 | + warnings.warn( |
| 46 | + "A função `_generateds_to_string_etree` está obsoleta e " |
| 47 | + "será removida em versões futuras. " |
| 48 | + "Por favor, substitua o uso desta função por `_render_edoc()`. ", |
| 49 | + DeprecationWarning |
| 50 | + ) |
| 51 | + return self._render_edoc(ds, pretty_print) |
| 52 | + |
| 53 | + def _render_edoc(self, edoc, pretty_print=False): |
| 54 | + |
| 55 | + if type(edoc) == _Element: |
| 56 | + return etree.tostring(edoc), edoc |
| 57 | + if isinstance(edoc, str): |
| 58 | + return edoc, etree.fromstring(edoc) |
| 59 | + # if isinstance(edoc, unicode): |
| 60 | + # return edoc, etree.fromstring(edoc) |
| 61 | + |
| 62 | + # XSDATA |
| 63 | + if is_dataclass(edoc): |
| 64 | + serializer = XmlSerializer(config=SerializerConfig(pretty_print=pretty_print)) |
| 65 | + if self._namespace: |
| 66 | + ns_map = {None: self._namespace} |
| 67 | + else: |
| 68 | + ns_map = None |
| 69 | + xml_string = serializer.render(obj=edoc, ns_map=ns_map) |
| 70 | + return xml_string, etree.fromstring(xml_string.encode('utf-8')) |
| 71 | + |
| 72 | + # GenereteDS |
| 73 | + # ======= Aviso de Obsolescência ======= |
| 74 | + # Este bloco de código será removido em uma versão futura. |
| 75 | + # Certifique-se de atualizar para as alternativas recomendadas. |
49 | 76 |
|
50 | 77 | output = StringIO()
|
51 | 78 | namespace = False
|
52 | 79 | if self._namespace:
|
53 | 80 | namespace = 'xmlns="' + self._namespace + '"'
|
54 | 81 |
|
55 | 82 | if namespace:
|
56 |
| - ds.export( |
| 83 | + edoc.export( |
57 | 84 | output,
|
58 | 85 | 0,
|
59 | 86 | pretty_print=pretty_print,
|
60 | 87 | namespacedef_=namespace
|
61 | 88 | )
|
62 | 89 | else:
|
63 |
| - ds.export( |
| 90 | + edoc.export( |
64 | 91 | output,
|
65 | 92 | 0,
|
66 | 93 | pretty_print=pretty_print,
|
@@ -220,7 +247,7 @@ def _data_hoje(self):
|
220 | 247 | return datetime.strftime(datetime.now(), "%Y-%m-%d")
|
221 | 248 |
|
222 | 249 | def assina_raiz(self, raiz, id, getchildren=False):
|
223 |
| - xml_string, xml_etree = self._generateds_to_string_etree(raiz) |
| 250 | + xml_string, xml_etree = self._render_edoc(raiz) |
224 | 251 | xml_assinado = Assinatura(self._transmissao.certificado).assina_xml2(
|
225 | 252 | xml_etree, id, getchildren
|
226 | 253 | )
|
|
0 commit comments