Skip to content

Commit

Permalink
[REF] render edoc method
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniospneto committed Nov 8, 2023
1 parent 1f01b65 commit 8401bf6
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions src/erpbrasil/edoc/edoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
# License MIT

import abc
import warnings
from dataclasses import is_dataclass
from datetime import datetime
from datetime import timedelta
from datetime import timezone

from erpbrasil.assinatura.assinatura import Assinatura
from lxml import etree
from lxml.etree import _Element

from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig

from .resposta import analisar_retorno_raw

# Fix Python 2.x.
Expand Down Expand Up @@ -39,28 +42,52 @@ def __init__(self, transmissao):
self._transmissao = transmissao

def _generateds_to_string_etree(self, ds, pretty_print=False):

if type(ds) == _Element:
return etree.tostring(ds), ds
if isinstance(ds, str):
return ds, etree.fromstring(ds)
# if isinstance(ds, unicode):
# return ds, etree.fromstring(ds)
warnings.warn(
"A função `_generateds_to_string_etree` está obsoleta e "
"será removida em versões futuras. "
"Por favor, substitua o uso desta função por `_render_edoc()`. ",
DeprecationWarning
)
return self._render_edoc(ds, pretty_print)

def _render_edoc(self, edoc, pretty_print=False):

if type(edoc) == _Element:
return etree.tostring(edoc), edoc
if isinstance(edoc, str):
return edoc, etree.fromstring(edoc)
# if isinstance(edoc, unicode):
# return edoc, etree.fromstring(edoc)

# XSDATA
if is_dataclass(edoc):
serializer = XmlSerializer(config=SerializerConfig(pretty_print=pretty_print))
if self._namespace:
ns_map = {None: self._namespace}
else:
ns_map = None
xml_string = serializer.render(obj=edoc, ns_map=ns_map)
return xml_string, etree.fromstring(xml_string.encode('utf-8'))

# GenereteDS
# ======= Aviso de Obsolescência =======
# Este bloco de código será removido em uma versão futura.
# Certifique-se de atualizar para as alternativas recomendadas.

output = StringIO()
namespace = False
if self._namespace:
namespace = 'xmlns="' + self._namespace + '"'

if namespace:
ds.export(
edoc.export(
output,
0,
pretty_print=pretty_print,
namespacedef_=namespace
)
else:
ds.export(
edoc.export(
output,
0,
pretty_print=pretty_print,
Expand Down Expand Up @@ -220,12 +247,11 @@ def _data_hoje(self):
return datetime.strftime(datetime.now(), "%Y-%m-%d")

def assina_raiz(self, raiz, id, getchildren=False):
xml_string, xml_etree = self._generateds_to_string_etree(raiz)
xml_string, xml_etree = self._render_edoc(raiz)
xml_assinado = Assinatura(self._transmissao.certificado).assina_xml2(
xml_etree, id, getchildren
)
return xml_assinado
return xml_assinado.replace('\n', '').replace('\r', '')

def _verifica_servico_em_operacao(self, proc_servico):
return True
Expand Down

0 comments on commit 8401bf6

Please sign in to comment.