From c0acf767daeceaeb4a5630b9fd0e6423af692c4b Mon Sep 17 00:00:00 2001 From: Danimar Ribeiro Date: Sun, 21 Jun 2015 01:26:12 -0300 Subject: [PATCH] =?UTF-8?q?Criado=20classes=20de=20para=20serializa=C3=A7?= =?UTF-8?q?=C3=A3o=20de=20xm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pytrustnfe/HttpClient.py | 2 +- pytrustnfe/Strings.py | 40 +++++++++++++++++++ pytrustnfe/servicos/Comunicacao.py | 10 +++++ pytrustnfe/test/test_comunicacao.py | 13 +++++++ pytrustnfe/test/test_xml_serializacao.py | 29 ++++++++++++++ pytrustnfe/xml/DynamicXml.py | 67 ++++++++++++++++++++++++++++++++ pytrustnfe/xml/__init__.py | 0 7 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 pytrustnfe/Strings.py create mode 100644 pytrustnfe/test/test_xml_serializacao.py create mode 100644 pytrustnfe/xml/DynamicXml.py create mode 100644 pytrustnfe/xml/__init__.py diff --git a/pytrustnfe/HttpClient.py b/pytrustnfe/HttpClient.py index bbb4910..61f8742 100644 --- a/pytrustnfe/HttpClient.py +++ b/pytrustnfe/HttpClient.py @@ -27,7 +27,7 @@ class HttpClient(object): try: conexao.request(u'POST', post, xml, self._headers()) response = conexao.getresponse() - if response.status == '200': + if response.status == 200: return response.read() return response.read() except Exception as e: diff --git a/pytrustnfe/Strings.py b/pytrustnfe/Strings.py new file mode 100644 index 0000000..9fcf6a9 --- /dev/null +++ b/pytrustnfe/Strings.py @@ -0,0 +1,40 @@ +''' +Created on Jun 17, 2015 + +@author: danimar +''' + +CONSULTA_CADASTRO_COMPLETA = ''\ + ''\ + ''\ + ''\ + '352.00'\ + ''\ + ''\ + ''\ + ''\ + ''\ + 'CONS-CADSP606081249112'\ + ''\ + ''\ + '' + +RETORNO_CONSULTA = ''\ + ''\ + ''\ + '352.00'\ + ''\ + ''\ + ''\ + 'SP_NFE_PL_008f111'\ + 'Consulta cadastro com uma ocorrênciaSP'\ + '6060812491122015-06-17T14:54:23-03:0035'\ + '60608124911202198926000169SP1'\ + '14C. R. TUNUSSI & CIA. LTDA'\ + 'NORMAL - REGIME PERIÓDICO DE APURAÇÃO2825900'\ + '1997-11-171997-11-17'\ + 'RUA JOSE NICOLAU LUX432'\ + 'CONJUNTO HABITACIONAL FRANCISCO DE CILLO (INOCOOP)3545803'\ + 'SANTA BARBARA D''OESTE13457162'\ + '' + \ No newline at end of file diff --git a/pytrustnfe/servicos/Comunicacao.py b/pytrustnfe/servicos/Comunicacao.py index 2fc92e9..a918525 100644 --- a/pytrustnfe/servicos/Comunicacao.py +++ b/pytrustnfe/servicos/Comunicacao.py @@ -15,6 +15,7 @@ logging.basicConfig(level=logging.INFO) logging.getLogger('suds.client').setLevel(logging.DEBUG) logging.getLogger('suds.transport').setLevel(logging.DEBUG) +from pytrustnfe.Strings import CONSULTA_CADASTRO_COMPLETA class Comunicacao(object): @@ -38,6 +39,15 @@ class Comunicacao(object): return chave_temp, certificado_temp + def consulta_cadastro(self, obj_consulta): + chave, certificado = self._preparar_temp_pem() + + client = HttpClient('nfe.fazenda.sp.gov.br', chave, certificado) + xml_retorno = client.post_xml('/ws/cadconsultacadastro2.asmx', CONSULTA_CADASTRO_COMPLETA) + + obj = objectify.fromstring(xml_retorno) + return xml_retorno, obj + def envio_nfe(self): chave, certificado = self._preparar_temp_pem() diff --git a/pytrustnfe/test/test_comunicacao.py b/pytrustnfe/test/test_comunicacao.py index 0a9b4e2..840dd79 100644 --- a/pytrustnfe/test/test_comunicacao.py +++ b/pytrustnfe/test/test_comunicacao.py @@ -16,6 +16,19 @@ class test_comunicacao(unittest.TestCase): caminho = os.path.dirname(__file__) + def test_consulta_cadastro(self): + try: + dir_pfx = '/home/danimar/Desktop/isotelha.pfx' #Hack + + com = Comunicacao(dir_pfx, 'iso@#telha') + xml, objeto = com.consulta_cadastro(None) + + print xml + print objeto + except Exception as e: + print(str(e)) + + #Teste temporario def test_envio_without_mock(self): try: diff --git a/pytrustnfe/test/test_xml_serializacao.py b/pytrustnfe/test/test_xml_serializacao.py new file mode 100644 index 0000000..8bc2e2c --- /dev/null +++ b/pytrustnfe/test/test_xml_serializacao.py @@ -0,0 +1,29 @@ +#coding=utf-8 + +import unittest +from lxml.etree import Element, ElementTree +from pytrustnfe.xml.DynamicXml import DynamicXml, gerar_xml + + +class test_xml_serializacao(unittest.TestCase): + + def test_serializacao(self): + t = DynamicXml("enviNFe") + t(versao="3.10") + t.idLote = "1" + t.indSinc = "1" + t.NFe.infNFe(versao="3.10", Id="NFe456465465465465654652123564878") + t.NFe.infNFe.ide.cUF = "32" + t.NFe.infNFe.ide.cNF = "0001" + t.NFe.infNFe.ide.natOp = "Venda de mercadorias" + + print("Iniciando a geração do xml") + print str(t) + root = Element(str(t)) + gerar_xml(root, t) + print(dir(t)) + + tree = ElementTree(root) + tree.write("/home/danimar/Desktop/nfe.xml") + + \ No newline at end of file diff --git a/pytrustnfe/xml/DynamicXml.py b/pytrustnfe/xml/DynamicXml.py new file mode 100644 index 0000000..a24effb --- /dev/null +++ b/pytrustnfe/xml/DynamicXml.py @@ -0,0 +1,67 @@ +''' +Created on Jun 17, 2015 + +@author: danimar +''' + +import xml.etree.ElementTree as ET +from collections import OrderedDict + +class DynamicXml(object): + def __getattr__(self, name): + try: + return object.__getattribute__(self,name) + except: + self.__setattr__(name, None) + return object.__getattribute__(self,name) + + def __setattr__(self, obj, val): + if(obj=="value" or obj=="atributos"): + object.__setattr__(self,obj, val) + else: + object.__setattr__(self,obj, DynamicXml(val)) + + def __init__(self, value): + self.value = value + self.atributos={} + object.__setattr__(self, '__dict__', OrderedDict()) + + def __str__(self): + return str(self.value) + def __call__(self, *args, **kw): + if(len(kw)>0): + self.atributos=kw + if(len(args)>0): + self.value= args[0] + else: + return self.value + + def __getitem__(self, i): + print(self.value) + if(i >= len(self.value)): + self.value.append(DynamicXml(None)) + return self.value[i] + + +def gerar_xml(xml, objeto): + for attr, value in objeto.__dict__.items(): + if(attr!="value" and attr!="atributos"): + if(type(value()) == type([])): + for item in value(): + print(item) + gerar_xml(xml, item) + else: + sub = ET.SubElement(xml,attr) + if(str(value)!="None"): + sub.text = str(value) + gerar_xml(sub, value) + elif(attr=="atributos"): + for atr, val in value.items(): + xml.set(atr.replace("__", ":"), str(val)) + + + + + + + diff --git a/pytrustnfe/xml/__init__.py b/pytrustnfe/xml/__init__.py new file mode 100644 index 0000000..e69de29