From 4e5e5143cb977a32b41776d39e8c9d82ee33ff74 Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Mon, 22 Apr 2013 10:57:51 +0200 Subject: [PATCH] [pod] Allowed to call a pod template within another pod template via new POD function 'pod'. --- bin/{startoo.sh => startoo} | 0 pod/doc_importers.py | 28 ++++++++++++++++++++++++++++ pod/renderer.py | 29 +++++++++++++++++++++++++---- 3 files changed, 53 insertions(+), 4 deletions(-) rename bin/{startoo.sh => startoo} (100%) diff --git a/bin/startoo.sh b/bin/startoo similarity index 100% rename from bin/startoo.sh rename to bin/startoo diff --git a/pod/doc_importers.py b/pod/doc_importers.py index 18ee4db..34af098 100644 --- a/pod/doc_importers.py +++ b/pod/doc_importers.py @@ -133,6 +133,34 @@ class OdtImporter(DocImporter): self.linkNs, self.importPath, self.textNs, self.textNs) return self.res +class PodImporter(DocImporter): + '''This class allows to import the result of applying another POD template, + into the current POD result.''' + def getImportFolder(self): return '%s/docImports' % self.tempFolder + + def setContext(self, context): + '''Defines the context to user for the PodImporter.''' + self.context = context + + def run(self): + # Define where to store the pod result in the temp folder + r = self.renderer + # Define where to store the ODT result. + op = os.path + resFolder = op.dirname(self.importPath) + resName = '%s.res.odt' % op.splitext(op.basename(self.importPath))[0] + resOdt = op.join(resFolder, resName) + # The POD template is in self.importPath + renderer = r.__class__(self.importPath, self.context, resOdt, + pythonWithUnoPath=r.pyPath, + ooPort=r.ooPort, forceOoCall=r.forceOoCall, + imageResolver=r.imageResolver) + renderer.stylesManager.stylesMapping = r.stylesManager.stylesMapping + renderer.run() + # The POD result is in "resOdt". Import it into the main POD result + # using an OdtImporter. + return OdtImporter(None, resOdt, 'odt', self.renderer).run() + class PdfImporter(DocImporter): '''This class allows to import the content of a PDF file into a pod template. It calls gs to split the PDF into images and calls the diff --git a/pod/renderer.py b/pod/renderer.py index 25399be..55677f4 100644 --- a/pod/renderer.py +++ b/pod/renderer.py @@ -31,7 +31,7 @@ from appy.pod.converter import FILE_TYPES from appy.pod.buffers import FileBuffer from appy.pod.xhtml2odt import Xhtml2OdtConverter from appy.pod.doc_importers import \ - OdtImporter, ImageImporter, PdfImporter, ConvertImporter + OdtImporter, ImageImporter, PdfImporter, ConvertImporter, PodImporter from appy.pod.styles_manager import StylesManager # ------------------------------------------------------------------------------ @@ -232,7 +232,8 @@ class Renderer: evalContext = {'xhtml': self.renderXhtml, 'text': self.renderText, 'test': self.evalIfExpression, - 'document': self.importDocument} # Default context + 'document': self.importDocument, + 'pod': self.importPod } # Default context if hasattr(context, '__dict__'): evalContext.update(context.__dict__) elif isinstance(context, dict) or isinstance(context, UserDict): @@ -333,8 +334,28 @@ class Renderer: imp = importer(content, at, format, self) # Initialise image-specific parameters if isImage: imp.setImageInfo(anchor, wrapInPara, size, sizeUnit, style) - res = imp.run() - return res + return imp.run() + + def importPod(self, content=None, at=None, format='odt', context=None): + '''Similar to m_importDocument, but allows to import the result of + executing the POD template specified in p_content or p_at, and + include it in the POD result.''' + # Is there a pod template defined? + if not content and not at: + raise PodError(DOC_NOT_SPECIFIED) + # If the POD template is specified as a Zope file, convert it into a + # Appy FileWrapper. + if content.__class__.__name__ == 'File': + content = FileWrapper(content) + imp = PodImporter(content, at, format, self) + self.forceOoCall = True + # Define the context to use: either the current context of the current + # POD renderer, or p_context if given. + if context: + imp.setContext(context) + else: + imp.setContext(self.contentParser.env.context) + return imp.run() def prepareFolders(self): # Check if I can write the result