Skip to content

Commit c01b9f3

Browse files
committed
[REF] render edoc method
1 parent f14d32e commit c01b9f3

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/erpbrasil/edoc/edoc.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
# License MIT
44

55
import abc
6+
import warnings
7+
from dataclasses import is_dataclass
68
from datetime import datetime
79
from datetime import timedelta
810
from datetime import timezone
9-
10-
from erpbrasil.assinatura.assinatura import Assinatura
1111
from lxml import etree
1212
from lxml.etree import _Element
1313

14+
from xsdata.formats.dataclass.serializers import XmlSerializer
15+
from xsdata.formats.dataclass.serializers.config import SerializerConfig
16+
1417
from .resposta import analisar_retorno_raw
1518

1619
# Fix Python 2.x.
@@ -39,28 +42,52 @@ def __init__(self, transmissao):
3942
self._transmissao = transmissao
4043

4144
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.
4976

5077
output = StringIO()
5178
namespace = False
5279
if self._namespace:
5380
namespace = 'xmlns="' + self._namespace + '"'
5481

5582
if namespace:
56-
ds.export(
83+
edoc.export(
5784
output,
5885
0,
5986
pretty_print=pretty_print,
6087
namespacedef_=namespace
6188
)
6289
else:
63-
ds.export(
90+
edoc.export(
6491
output,
6592
0,
6693
pretty_print=pretty_print,
@@ -220,7 +247,7 @@ def _data_hoje(self):
220247
return datetime.strftime(datetime.now(), "%Y-%m-%d")
221248

222249
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)
224251
xml_assinado = Assinatura(self._transmissao.certificado).assina_xml2(
225252
xml_etree, id, getchildren
226253
)

0 commit comments

Comments
 (0)