Skip to content

Commit dbb226e

Browse files
committed
refactorization of render code
1 parent bf48fe9 commit dbb226e

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

docs/pages/getting_started/30_cfdi_load_guide.rst

+9-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ ______________________
2121

2222
.. code-block:: python
2323
24-
# JSON
25-
json = invoice.json_str()
26-
# save to file
27-
invoice.json_write("_comprobante_.json")
28-
# .. or alternative
29-
with open("_stream_comprobante_.json", 'w', encoding='utf-8') as f:
30-
invoice.json_write(f)
24+
from satcfdi import render
3125
3226
# XML
3327
xml = invoice.xml_bytes()
@@ -37,6 +31,14 @@ ______________________
3731
with open("_stream_comprobante_.xml", 'wb') as f:
3832
invoice.xml_write(f)
3933
34+
# JSON
35+
json = render.json_str(invoice)
36+
# save to file
37+
render.json_write(invoice, "_comprobante_.json")
38+
# .. or alternative
39+
with open("_stream_comprobante_.json", 'w', encoding='utf-8') as f:
40+
render.json_write(invoice, f)
41+
4042
# HTML
4143
html = render.html_str(invoice)
4244
# save to file

readme.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,19 @@ ____________________
182182
invoice.xml_write("my_invoice.xml")
183183
184184
# JSON
185-
invoice.json_write("my_invoice.json", pretty_print=True)
185+
render.json_write(invoice, "my_invoice.json", pretty_print=True)
186186
187187
# HTML
188188
render.html_write(invoice, "my_invoice.html")
189189
190190
# PDF
191191
render.pdf_write(invoice, "my_invoice.pdf")
192192
193+
# Multiple HTML
194+
render.html_write([invoice1, invoice2], "my_invoice.html")
195+
196+
# Multiple PDF
197+
render.pdf_write([invoice1, invoice2], "my_invoice.pdf")
193198
194199
195200
Contributing

satcfdi/render/__init__.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from collections.abc import Sequence
23
from lxml.etree import QName
34

@@ -14,6 +15,19 @@
1415
PDF_INIT_TEMPLATE = DefaultCFDIEnvironment.get_template("_init.html")
1516

1617

18+
def json_write(xlm: XElement, target, pretty_print=False):
19+
if isinstance(target, str):
20+
with open(target, 'w') as f:
21+
json.dump(xlm, f, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
22+
return
23+
24+
json.dump(xlm, target, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
25+
26+
27+
def json_str(xlm: XElement, pretty_print=False) -> str:
28+
return json.dumps(xlm, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
29+
30+
1731
def html_write(xlm: XElement | Sequence[XElement], target, init_template=PDF_INIT_TEMPLATE):
1832
if isinstance(xlm, Sequence):
1933
init_template.stream({"c": [(QName(a.tag).localname, a) for a in xlm], "k": '_multiple'}).dump(target)
@@ -37,7 +51,6 @@ def pdf_write(xlm: XElement | Sequence[XElement], target, init_template=PDF_INIT
3751
stylesheets=[PDF_CSS]
3852
)
3953

40-
4154
def pdf_bytes(xlm: XElement | Sequence[XElement], init_template=PDF_INIT_TEMPLATE) -> bytes:
4255
if weasyprint is None:
4356
raise ImportError("weasyprint is not installed")

satcfdi/xelement.py

-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from lxml import etree
32

43
from .transform import SchemaCollector, cfdi_schemas, validate_xsd
@@ -61,17 +60,6 @@ def xml_bytes(self, pretty_print=False, xml_declaration=True, validate=False, in
6160
xml = self.to_xml(validate=validate, include_schema_location=include_schema_location)
6261
return etree.tostring(xml, xml_declaration=xml_declaration, encoding="UTF-8", pretty_print=pretty_print)
6362

64-
def json_write(self, target, pretty_print=False):
65-
if isinstance(target, str):
66-
with open(target, 'w') as f:
67-
json.dump(self, f, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
68-
return
69-
70-
json.dump(self, target, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
71-
72-
def json_str(self, pretty_print=False) -> str:
73-
return json.dumps(self, ensure_ascii=False, default=str, indent=2 if pretty_print else None)
74-
7563
def __repr__(self):
7664
# return '%s.%s(%s)' % (self.__class__.__module__,
7765
# self.__class__.__qualname__,

tests/test_create_addenda.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,4 @@ def test_copy_cfdi():
125125
copy = invoice.copy()
126126
verify_invoice(copy, f"{xml_file}")
127127

128-
copy.json_str(pretty_print=True)
128+
render.json_str(copy, pretty_print=True)

0 commit comments

Comments
 (0)