changeset 282:c41f8421db10 draft default tip

Support out parameter of render bug49
author Cédric Krier <ced@b2ck.com>
date Fri, 09 Nov 2018 15:24:55 +0100
parents b50d7f84c7db
children
files CHANGES examples/demo_odf.py relatorio/templates/base.py relatorio/templates/chart.py relatorio/templates/opendocument.py relatorio/templates/pdf.py
diffstat 6 files changed, 45 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES	Thu Nov 08 19:11:32 2018 +0100
+++ b/CHANGES	Fri Nov 09 15:24:55 2018 +0100
@@ -1,3 +1,4 @@
+* Support out parameter of render
 * Write opendocument stream directly to the ZipFile
 
 0.8.1 - 20180930
--- a/examples/demo_odf.py	Thu Nov 08 19:11:32 2018 +0100
+++ b/examples/demo_odf.py	Fri Nov 09 15:24:55 2018 +0100
@@ -71,6 +71,5 @@
     # Big document
     print("generating output_big.odt... ", end='')
     report = Report(abspath(join(pwd, 'big.odt')), ODT_MIME)
-    content = report().render().getvalue()
-    open(join(pwd, 'output_big.odt'), 'wb').write(content)
+    content = report().render(out=open(join(pwd, 'output_big.odt'), 'wb'))
     print("done")
--- a/relatorio/templates/base.py	Thu Nov 08 19:11:32 2018 +0100
+++ b/relatorio/templates/base.py	Fri Nov 09 15:24:55 2018 +0100
@@ -31,7 +31,8 @@
 
     def render(self, method=None, encoding='utf-8', out=None, **kwargs):
         "calls the serializer to render the template"
-        return self.serializer(self.events)
+        return self.serializer(
+            self.events, method=method, encoding=encoding, out=out)
 
     def serialize(self, method='xml', **kwargs):
         "generates the bitstream corresponding to the template"
--- a/relatorio/templates/chart.py	Thu Nov 08 19:11:32 2018 +0100
+++ b/relatorio/templates/chart.py	Fri Nov 09 15:24:55 2018 +0100
@@ -68,10 +68,13 @@
     def __init__(self):
         self.text_serializer = genshi.output.TextSerializer()
 
-    def __call__(self, stream):
+    def __call__(self, stream, method=None, encoding='utf-8', out=None):
         if not PYCHA_TYPE:
             raise NotImplementedError
-        result = BytesIO()
+        if out is None:
+            result = BytesIO()
+        else:
+            result = out
         yml = StringIO(_encode(self.text_serializer(stream)))
         chart_yaml = yaml.load(yml.read())
         chart_info = chart_yaml['chart']
@@ -95,6 +98,8 @@
         elif chart_type == 'svg':
             surface.finish()
 
-        return result
+        if out is None:
+            return result
+
 
 MIMETemplateLoader.add_factory('chart', Template, Template.id_function)
--- a/relatorio/templates/opendocument.py	Thu Nov 08 19:11:32 2018 +0100
+++ b/relatorio/templates/opendocument.py	Fri Nov 09 15:24:55 2018 +0100
@@ -1122,15 +1122,20 @@
         self.inzip = get_zip_file(source)
         self.manifest = Manifest(self.inzip.read(MANIFEST))
         self.meta = Meta(self.inzip.read(META))
-        self.new_oo = BytesIO()
-        self.outzip = zipfile.ZipFile(
-            self.new_oo, mode='w', compression=zipfile.ZIP_DEFLATED)
         self.xml_serializer = genshi.output.XMLSerializer()
         self._files = files
         self.chunksize = chunksize
-        self._deferred = []
+        self.outzip = None
+        self._deferred = None
 
-    def __call__(self, stream):
+    def __call__(self, stream, method=None, encoding='utf-8', out=None):
+        if out is None:
+            result = BytesIO()
+        else:
+            result = out
+        self.outzip = zipfile.ZipFile(
+            result, mode='w', compression=zipfile.ZIP_DEFLATED)
+        self._deferred = []
         files = {}
         now = time.localtime()[:6]
         manifest_info = None
@@ -1155,7 +1160,7 @@
 
         writer = _ZipWriteSplitStream(self.outzip, self.chunksize)
         output_encode(
-            self.xml_serializer(writer(stream)), encoding='utf-8', out=writer)
+            self.xml_serializer(writer(stream)), encoding=encoding, out=writer)
 
         for args in self._deferred:
             self.add_file(*args)
@@ -1165,10 +1170,11 @@
         self.inzip.close()
         self.outzip.close()
 
-        return self.new_oo
+        if out is None:
+            return result
 
     def add_file(self, path, content, mimetype):
-        if path not in self.outzip.namelist():
+        if self.outzip and path not in self.outzip.namelist():
             try:
                 self.outzip.writestr(path, content)
                 self.manifest.add_file_entry(path, mimetype)
--- a/relatorio/templates/pdf.py	Thu Nov 08 19:11:32 2018 +0100
+++ b/relatorio/templates/pdf.py	Fri Nov 09 15:24:55 2018 +0100
@@ -48,23 +48,30 @@
 class PDFSerializer:
 
     def __init__(self):
-        self.working_dir = tempfile.mkdtemp(prefix='relatorio')
-        self.tex_file = os.path.join(self.working_dir, 'report.tex')
-        self.pdf_file = os.path.join(self.working_dir, 'report.pdf')
         self.text_serializer = genshi.output.TextSerializer()
 
-    def __call__(self, stream):
-        tex_file = open(self.tex_file, 'w')
-        tex_file.write(_encode(self.text_serializer(stream)))
-        tex_file.close()
+    def __call__(self, stream, method=None, encoding='utf-8', out=None):
+        if out is None:
+            result = BytesIO()
+        else:
+            result = out
+        working_dir = tempfile.mkdtemp(prefix='relatorio')
+        tex_file = os.path.join(working_dir, 'report.tex')
+        pdf_file = os.path.join(working_dir, 'report.pdf')
+
+        with open(tex_file, 'w') as fp:
+            fp.write(_encode(self.text_serializer(stream)))
 
         subprocess.check_call([TEXEXEC, '--purge', 'report.tex'],
-                              cwd=self.working_dir)
+                              cwd=working_dir)
+
+        with open(pdf_file, 'r') as fp:
+            result.write(fp.read())
 
-        pdf = BytesIO()
-        pdf.write(open(self.pdf_file, 'r').read())
+        shutil.rmtree(working_dir, ignore_errors=True)
 
-        shutil.rmtree(self.working_dir, ignore_errors=True)
-        return pdf
+        if out is None:
+            return result
+
 
 MIMETemplateLoader.add_factory('pdf', Template)

Repository Layout

/ Tryton top level repositories
modules Modules
public Public repositories
tpf Tryton Foundation repositories
sandbox Sandbox