Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Método de renderização do objeto edoc. #67

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 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,7 +247,7 @@ 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
)
Expand Down