def _include(self, partialName, dictGlob, dictLoc, tmplResults): from caduceus.transform.templateParser import CaduceusTemplateParser # partialName may contain arguments #print("#### partialName : %s" % partialName) match = re.match("^([\S+]+)\((.+)\)$", partialName, re.DOTALL) if match: #print("#### %s =>\ngrp1=%s\ngrp2=%s" % (partialName, match.group(1), match.group(2))) partialName = match.group(1) templateDictLoc = dictLoc # dictLoc.copy() # Replace , by ; and evaluate arguments to pass to the partial try: args = match.group(2) templateDictLoc['addPartialArgs'] = CaduceusTemplatePython.addPartialArgs exec("addPartialArgs(globals(), locals(), %s)" % args, dictGlob, templateDictLoc) except IndexError: print("### Index error for partial %s" % partialName) else: templateDictLoc = dictLoc content = "" template = CaduceusTemplateParser.parsePartialFile(partialName, self._path, self._rootPath) if template: content = template.render(dictGlob, templateDictLoc, tmplResults) return content + CaduceusTemplateEntity.render(self, dictGlob, dictLoc, tmplResults)
def _processFile(self, filePath, fileName, outFilePath): inputFullPath = os.path.join(filePath, fileName) inputBasenamePath, ext = os.path.splitext(inputFullPath) outputFullPath = os.path.join(outFilePath, fileName) if ext == ".html": if self.allowedTemplateFile(filePath, fileName): print(" Processing file %s..." % inputFullPath) template = CaduceusTemplateParser.parseTemplateFile(inputFullPath, self.rootPath, self.caduceusPath) if template: CaduceusHelper.ensurePathExists(outFilePath) outputFile = open(outputFullPath, 'w') try: # Load controller for file dictGlob = {} dictLoc = {} try: controllerName, _ext = os.path.splitext(fileName) controllerName = controllerName + "Test" sys.path.append(os.path.dirname(inputFullPath)) #exec ("from %s import %s" % (controllerName, controllerName)) in dictGlob, dictLoc exec ("from %s import %s" % (controllerName, controllerName), dictGlob, dictLoc) # We must copy local dictionnary into global, to allow some symbols resolution dictGlob.update(dictLoc) # Instanciate a controller in parsing context #exec ("__caduceus_controler__ = %s()" % controllerName) in dictGlob, dictLoc exec ("__caduceus_controler__ = %s()" % controllerName, dictGlob, dictLoc) os.environ["CADUCEUS_TEMPLATE_PATH"] = inputFullPath # Bind all controller public methods as global methods for key in dir(dictLoc[controllerName]): if key[0] != "_": proc = eval("__caduceus_controler__.%s" % key, dictGlob, dictLoc) dictLoc[key] = proc except IOError: print("Warning: No controller file for '%s'" % inputFullPath) except ImportError: print("Warning: No controller file for '%s'" % inputFullPath) import traceback traceback.print_exc() # Render template using context tmplResults = CaduceusTemplateResults(outputFullPath) result = template.render(dictGlob, dictLoc, tmplResults) self.results.addTemplateResult(tmplResults) outputFile.write(result) finally: outputFile.close() elif ext not in [".py", ".pyc"]: # File may be stylesheet, javasript or other resource # copy as it print(" Copy file %s..." % inputFullPath) CaduceusHelper.ensurePathExists(outFilePath) shutil.copyfile(inputFullPath, outputFullPath)