# Path: src/sii/lib/lib/xml.py
# XML_DECL = lambda enc: b'<?xml version="1.0" encoding="' + bytes(enc, enc) + b'"?>'
# class XML(object):
#     def __init__(self, node=None, name=None, text=None, namespaces=None):
#     def __repr__(self):
#     def __str__(self):
#     def __int__(self):
#     def __float__(self):
#     def __iter__(self):
#     def __setattr__(self, name, value):
#     def __getitem__(self, key):
#     def __setitem__(self, key, value):
#     def __remove__(self):
#     def __name__(self):
#     def __children__(self):
#     def __siblings__(self):
#     def __generation__(self):
#     def _etree(self):
#     def _xml(self):
#     def _str(self):
#     def _int(self):
#     def _float(self):
#     def _number(self):
#     def _list(self):
#     def _has(self, name):
# def create_xml(name, value=None, namespaces=None):
# def read_xml(path):
# def load_xml(xml_string):
# def wrap_xml(xml_etree):
# def dump_etree(xml_node):
# def dump_xml(xml_node, **kwargs):
# def print_xml(xml, file=sys.stdout.buffer, end='\n', encoding='UTF-8'):
# def write_xml(xml, fpath, end='\n', encoding='UTF-8', append=False):


import re
import copy
import base64
import datetime as dt
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash      import SHA as SHA1
from Crypto.PublicKey import RSA
from .lib import xml
    doc = xml.wrap_xml(doc_xml)

    stamp   = xml.create_xml(name='TED')
    doc.TED = stamp

    stamp['version'] = '1.0'

    stamp.DD       = xml.create_xml(name='DD')
    stamp.DD.RE    = doc.Encabezado.Emisor.RUTEmisor._str
    stamp.DD.TD    = doc.Encabezado.IdDoc.TipoDTE._str
    stamp.DD.F     = doc.Encabezado.IdDoc.Folio._int
    stamp.DD.FE    = doc.Encabezado.IdDoc.FchEmis._str
    stamp.DD.RR    = doc.Encabezado.Receptor.RUTRecep._str
    stamp.DD.RSR   = doc.Encabezado.Receptor.RznSocRecep._str
    stamp.DD.MNT   = doc.Encabezado.Totales.MntTotal._int
    stamp.DD.IT1   = doc.Detalle.NmbItem._str
    stamp.DD.CAF   = copy.deepcopy(caf.CAF)
    stamp.DD.TSTED = dt.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')

    digest              = xml.create_xml(name='FRMT', value=_build_digital_stamp_digest(doc_xml, caf_xml))
    digest['algoritmo'] = "SHA1withRSA"
    stamp.FRMT          = digest

    return xml.dump_etree(stamp)


def _build_digital_stamp_digest(doc_xml, caf_xml):
    doc = xml.wrap_xml(doc_xml)
    caf = xml.wrap_xml(caf_xml)


# Path: src/sii/lib/lib/xml.py
# XML_DECL = lambda enc: b'<?xml version="1.0" encoding="' + bytes(enc, enc) + b'"?>'
# class XML(object):
#     def __init__(self, node=None, name=None, text=None, namespaces=None):
#     def __repr__(self):
#     def __str__(self):
#     def __int__(self):
#     def __float__(self):
#     def __iter__(self):
#     def __setattr__(self, name, value):
#     def __getitem__(self, key):
#     def __setitem__(self, key, value):
#     def __remove__(self):
#     def __name__(self):
#     def __children__(self):
#     def __siblings__(self):
#     def __generation__(self):
#     def _etree(self):
#     def _xml(self):
#     def _str(self):
#     def _int(self):
#     def _float(self):
#     def _number(self):
#     def _list(self):
#     def _has(self, name):
# def create_xml(name, value=None, namespaces=None):
# def read_xml(path):
# def load_xml(xml_string):
# def wrap_xml(xml_etree):
# def dump_etree(xml_node):
# def dump_xml(xml_node, **kwargs):
# def print_xml(xml, file=sys.stdout.buffer, end='\n', encoding='UTF-8'):
# def write_xml(xml, fpath, end='\n', encoding='UTF-8', append=False):


# Path: src/sii/lib/stamping.py
import re
import copy
import base64
import datetime as dt
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash      import SHA as SHA1
from Crypto.PublicKey import RSA
from .lib import xml

    doc = xml.wrap_xml(doc_xml)

    stamp   = xml.create_xml(name='TED')
    doc.TED = stamp

    stamp['version'] = '1.0'

    stamp.DD       = xml.create_xml(name='DD')
    stamp.DD.RE    = doc.Encabezado.Emisor.RUTEmisor._str
    stamp.DD.TD    = doc.Encabezado.IdDoc.TipoDTE._str
    stamp.DD.F     = doc.Encabezado.IdDoc.Folio._int
    stamp.DD.FE    = doc.Encabezado.IdDoc.FchEmis._str
    stamp.DD.RR    = doc.Encabezado.Receptor.RUTRecep._str
    stamp.DD.RSR   = doc.Encabezado.Receptor.RznSocRecep._str
    stamp.DD.MNT   = doc.Encabezado.Totales.MntTotal._int
    stamp.DD.IT1   = doc.Detalle.NmbItem._str
    stamp.DD.CAF   = copy.deepcopy(caf.CAF)
    stamp.DD.TSTED = dt.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')

    digest              = xml.create_xml(name='FRMT', value=_build_digital_stamp_digest(doc_xml, caf_xml))
    digest['algoritmo'] = "SHA1withRSA"
    stamp.FRMT          = digest

    return xml.dump_etree(stamp)


def _build_digital_stamp_digest(doc_xml, caf_xml):
    doc = xml.wrap_xml(doc_xml)
    caf = xml.wrap_xml(caf_xml)


    xml_buff = xml.dump_xml(doc.TED.DD, encoding='ISO-8859-1', xml_declaration=False)