def _compile(self, data, compfile=None): """Private method called to parse the template text and compile it into a runtime form. Creates and delegates a template.parser.Parser object to handle the compilation, or uses the object passed in PARSER. On success, the compiled template is stored in the 'data' attribute of the 'data' object and returned. On error, an exception is raised, or None is returned if the TOLERANT flag is set. The optional 'compiled' parameter may be passed to specify the name of a compiled template file to which the generated Python code should be written. Errors are (for now...) silently ignored, assuming that failures to open a file for writing are intentional (e.g directory write permission). """ if data is None: return None text = data.text error = None if not self.__parser: self.__parser = Config.parser(self.__params) # discard the template text - we don't need it any more # del data.text parsedoc = self.__parser.parse(text, data) parsedoc["METADATA"].setdefault("name", data.name) parsedoc["METADATA"].setdefault("modtime", data.time) # write the Python code to the file compfile, if defined if compfile: basedir = os.path.dirname(compfile) if not os.path.isdir(basedir): try: os.makedirs(basedir) except IOError as e: error = Error("failed to create compiled templates " "directory: %s (%s)" % (basedir, e)) if not error: try: self.__document.write_python_file(compfile, parsedoc) except Exception as e: error = Error("cache failed to write %s: %s" % (os.path.basename(compfile), e)) if error is None and data.time is not None: if not compfile: raise Error("invalid null filename") ctime = int(data.time) os.utime(compfile, (ctime, ctime)) if not error: data.data = Document(parsedoc) return data if self.__tolerant: return None else: raise error
def testDocument(self): # Create a document and check accessor methods for blocks and metadata doc = Document({ 'BLOCK': lambda *_: 'some output', 'DEFBLOCKS': { 'foo': lambda *_: 'the foo block', 'bar': lambda *_: 'the bar block' }, 'METADATA': { 'author': 'Andy Wardley', 'version': 3.14 } }) c = DummyContext() self.assertTrue(doc) self.assertEqual('Andy Wardley', doc.author) self.assertEqual(3.14, doc.version) self.assertEqual('some output', doc.process(c)) self.assertTrue(isinstance(doc.block(), collections.Callable)) self.assertTrue(isinstance(doc.blocks()['foo'], collections.Callable)) self.assertTrue(isinstance(doc.blocks()['bar'], collections.Callable)) self.assertEqual('some output', doc.block()()) self.assertEqual('the foo block', doc.blocks()['foo']()) self.assertEqual('the bar block', doc.blocks()['bar']()) tproc = Template({'INCLUDE_PATH': 'test/src'}) self.Expect(DATA, tproc, {'mydoc': doc})
def testDocument(self): # Create a document and check accessor methods for blocks and metadata doc = Document( { 'BLOCK': lambda *_: 'some output', 'DEFBLOCKS': { 'foo': lambda *_: 'the foo block', 'bar': lambda *_: 'the bar block' }, 'METADATA': { 'author': 'Andy Wardley', 'version': 3.14 }}) c = DummyContext() self.failUnless(doc) self.assertEquals('Andy Wardley', doc.author) self.assertEquals(3.14, doc.version) self.assertEquals('some output', doc.process(c)) self.failUnless(callable(doc.block())) self.failUnless(callable(doc.blocks()['foo'])) self.failUnless(callable(doc.blocks()['bar'])) self.assertEquals('some output', doc.block()()) self.assertEquals('the foo block', doc.blocks()['foo']()) self.assertEquals('the bar block', doc.blocks()['bar']()) tproc = Template({ 'INCLUDE_PATH': 'test/src' }) self.Expect(DATA, tproc, { 'mydoc': doc })
def _load_compiled(self, path): try: return Document.evaluate_file(path) except TemplateException as e: raise Error("compiled template %s: %s" % (path, e))
def _load_compiled(self, path): try: return Document.evaluate_file(path) except TemplateException, e: raise Error("compiled template %s: %s" % (path, e))
error = Error("failed to create compiled templates " "directory: %s (%s)" % (basedir, e)) if not error: try: self.__document.write_python_file(compfile, parsedoc) except Exception, e: error = Error("cache failed to write %s: %s" % (os.path.basename(compfile), e)) if error is None and data.time is not None: if not compfile: raise Error("invalid null filename") ctime = int(data.time) os.utime(compfile, (ctime, ctime)) if not error: data.data = Document(parsedoc) return data if self.__tolerant: return None else: raise error def _compiled_is_current(self, template_name): """Returns True if template_name and its compiled name exists and they have the same mtime. """ compiled_name = self._compiled_filename(template_name) if not compiled_name: return False