
from satcfdi.cfdi import CFDI

import json
from collections.abc import Sequence
from lxml.etree import QName

from satcfdi.xelement import XElement


import sys
#'/home/ambagasdowa/gitdowa/api/core/clients/gst/render/environment.py'
sys.path.append('/home/ambagasdowa/gitdowa/api/')

from core.clients.gst import render
#from core.clients.gst.render.environment import DefaultCFDIEnvironment

import os

print(os.path.dirname(__file__))

# from file
invoice = CFDI.from_file('./FDTIJ-223-EF56532B-B393-5B86-8560-35812A3DB41D.xml')


print(invoice.get('TipoDeComprobante'))
invoice['x'] ={} 
invoice['x']['Sample'] = 'XXX'
invoice['x']['SampleOther'] = 'ZZZ'
invoice['x']['data_one'] = 'one'
invoice['x']['data_two'] = 'one'
invoice['x']['data_end'] = 'one'


js = render.json_str(invoice)

print(invoice.keys())

print(json.dumps(json.loads(js), indent=4))


#invoice['JHeader']['Nombre'] = 'XXXX'


# PDF
pdf = render.pdf_bytes(invoice)

#print(pdf)
# save to file
#render.pdf_write(invoice, "_comprobante_.pdf")
# .. or alternative
with open("03_stream_comprobante_.pdf", 'wb') as f:
    render.pdf_write(invoice, f)



