<commit_before># -*- coding: utf-8 -*-
# © 2016 <NAME>, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import os
from lxml import etree
from pytrustnfe import HttpClient
from pytrustnfe.xml import render_xml, sanitize_response


def _render(certificado, method, **kwargs):
    path = os.path.join(os.path.dirname(__file__), 'templates')
    xml_send = render_xml(path, '%s.xml' % method, True, **kwargs)
    return etree.tostring(xml_send)


def _send(certificado, method, **kwargs):
    base_url = ''
    if kwargs['ambiente'] == 'producao':
        base_url = 'https://nfe.etransparencia.com.br/rj.petropolis/webservice/aws_nfe.aspx'  # noqa
    else:
        base_url = 'https://nfehomologacao.etransparencia.com.br/rj.petropolis/webservice/aws_nfe.aspx'  # noqa
    xml_send = kwargs["xml"]
    path = os.path.join(os.path.dirname(__file__), 'templates')
    soap = render_xml(path, 'SoapRequest.xml', False, soap_body=xml_send.decode())
    client = HttpClient(base_url)
    response = client.post_soap(soap, 'NFeaction/AWS_NFE.%s' % method)
    response, obj = sanitize_response(response.encode('utf-8'))
    return {
        'sent_xml': xml_send.decode(),
        'received_xml': response.decode(),
        'object': obj
    }


def xml_processa_rps(certificado, **kwargs):
    return _render(certificado, 'PROCESSARPS', **kwargs)


def processa_rps(certificado, **kwargs):
    if "xml" not in kwargs:
        kwargs['xml'] = xml_processa_rps(certificado, **kwargs)
    return _send(certificado, 'PROCESSARPS', **kwargs)


def xml_consulta_protocolo(certificado, **kwargs):
    return _render(certificado, 'CONSULTAPROTOCOLO', **kwargs)


def consulta_protocolo(certificado, **kwargs):
    if "xml" not in kwargs:
        kwargs['xml'] = xml_consulta_protocolo(certificado, **kwargs)
    return _send(certificado, 'CONSULTAPROTOCOLO', **kwargs)


def xml_consulta_notas_protocolo(certificado, **kwargs):
    return _render(certificado, 'CONSULTANOTASPROTOCOLO', **kwargs)


def consulta_notas_protocolo(certificado, **kwargs):
    if "xml" not in kwargs:
        kwargs['xml'] = xml_consulta_notas_protocolo(certificado, **kwargs)
    return _send(certificado, 'CONSULTANOTASPROTOCOLO', **kwargs)


def xml_cancelar_nfse(certificado, **kwargs):
    return _render(certificado, 'CANCELANOTAELETRONICA', **kwargs)


def cancelar_nfse(certificado, **kwargs):
    if "xml" not in kwargs:
        kwargs['xml'] = xml_cancelar_nfse(certificado, **kwargs)
    return _send(certificado, 'CANCELANOTAELETRONICA', **kwargs)<commit_msg>Fix 'bytes' object has no attribute 'encode'<commit_after># -*- coding: utf-8 -*-
# © 2016 <NAME>, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import os
from lxml import etree
from pytrustnfe import HttpClient
from pytrustnfe.xml import render_xml, sanitize_response


def _render(certificado, method, **kwargs):
    path = os.path.join(os.path.dirname(__file__), 'templates')
    xml_send = render_xml(path, '%s.xml' % method, True, **kwargs)
    return etree.tostring(xml_send)


def _send(certificado, method, **kwargs):
    base_url = ''
    if kwargs['ambiente'] == 'producao':
        base_url = 'https://nfe.etransparencia.com.br/rj.petropolis/webservice/aws_nfe.aspx'  # noqa
    else:
        base_url = 'https://nfehomologacao.etransparencia.com.br/rj.petropolis/webservice/aws_nfe.aspx'  # noqa
    xml_send = kwargs["xml"]
    path = os.path.join(os.path.dirname(__file__), 'templates')
    soap = render_xml(path, 'SoapRequest.xml', False, soap_body=xml_send.decode())
    client = HttpClient(base_url)
    response = client.post_soap(soap, 'NFeaction/AWS_NFE.%s' % method)
    response, obj = sanitize_response(response)
    return {
        'sent_xml': xml_send.decode(),
        'received_xml': response,
        'object': obj
    }


def xml_processa_rps(certificado, **kwargs):
    return _render(certificado, 'PROCESSARPS', **kwargs)


def processa_rps(certificado, **kwargs):
    if "xml" not in kwargs:
        kwargs['xml'] = xml_processa_rps(certificado, **kwargs)
    return _send(certificado, 'PROCESSARPS', **kwargs)


def xml_consulta_protocolo(certificado, **kwargs):
    return _render(certificado, 'CONSULTAPROTOCOLO', **kwargs)


def consulta_protocolo(certificado, **kwargs):
    if "xml" not in kwargs:
        kwargs['xml'] = xml_consulta_protocolo(certificado, **kwargs)
    return _send(certificado, 'CONSULTAPROTOCOLO', **kwargs)


def xml_consulta_notas_protocolo(certificado, **kwargs):
    return _render(certificado, 'CONSULTANOTASPROTOCOLO', **kwargs)


def consulta_notas_protocolo(certificado, **kwargs):
    if "xml" not in kwargs:
        kwargs['xml'] = xml_consulta_notas_protocolo(certificado, **kwargs)
    return _send(certificado, 'CONSULTANOTASPROTOCOLO', **kwargs)


def xml_cancelar_nfse(certificado, **kwargs):
    return _render(certificado, 'CANCELANOTAELETRONICA', **kwargs)


def cancelar_nfse(certificado, **kwargs):
    if "xml" not in kwargs:
        kwargs['xml'] = xml_cancelar_nfse(certificado, **kwargs)
    return _send(certificado, 'CANCELANOTAELETRONICA', **kwargs)

{'fraction_non_alphanumeric': 0.13723963402764258, 'fraction_numerical': 0.0029199922133540976, 'mean_word_length': 4.664829106945976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 62}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6702507', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1838, 'n_words': 347}