def _compile_from_file(self, path, filename): if path is not None: util.verify_directory(os.path.dirname(path)) filemtime = os.stat(filename)[stat.ST_MTIME] if (not os.path.exists(path) or os.stat(path)[stat.ST_MTIME] < filemtime): data = util.read_file(filename) _compile_module_file(self, data, filename, path, self.module_writer) module = compat.load_module(self.module_id, path) del sys.modules[self.module_id] if module._magic_number != codegen.MAGIC_NUMBER: data = util.read_file(filename) _compile_module_file(self, data, filename, path, self.module_writer) module = compat.load_module(self.module_id, path) del sys.modules[self.module_id] ModuleInfo(module, path, self, filename, None, None) else: # template filename and no module directory, compile code # in memory data = util.read_file(filename) code, module = _compile_text(self, data, filename) self._source = None self._code = code ModuleInfo(module, None, self, filename, code, None) return module
def test_crlf(self): template = util.read_file(self._file_path("crlf.html")) nodes = Lexer(template).parse() self._compare( nodes, TemplateNode({}, [ Text('<html>\r\n\r\n', (1, 1)), PageTag('page', { 'args': "a=['foo',\n 'bar']" }, (3, 1), []), Text('\r\n\r\nlike the name says.\r\n\r\n', (4, 26)), ControlLine('for', 'for x in [1,2,3]:', False, (8, 1)), Text(' ', (9, 1)), Expression('x', [], (9, 9)), ControlLine('for', 'endfor', True, (10, 1)), Text('\r\n', (11, 1)), Expression("trumpeter == 'Miles' and " "trumpeter or \\\n 'Dizzy'", [], (12, 1)), Text('\r\n\r\n', (13, 15)), DefTag('def', {'name': 'hi()'}, (15, 1), [ Text('\r\n hi!\r\n', (15, 19))]), Text('\r\n\r\n</html>\r\n', (17, 8)) ]) ) assert flatten_result(Template(template).render()) \ == """<html> like the name says. 1 2 3 Dizzy </html>"""
def _do_test_traceback(self, utf8, memory, syntax): if memory: if syntax: source = u('## coding: utf-8\n<% print "m’a réveillé. '\ 'Elle disait: « S’il vous plaît… dessine-moi un mouton! » %>') else: source = u('## coding: utf-8\n<% print u"m’a réveillé. '\ 'Elle disait: « S’il vous plaît… dessine-moi un mouton! »" + str(5/0) %>') if utf8: source = source.encode('utf-8') else: source = source templateargs = {'text': source} else: if syntax: filename = 'unicode_syntax_error.html' else: filename = 'unicode_runtime_error.html' source = util.read_file(self._file_path(filename), 'rb') if not utf8: source = source.decode('utf-8') templateargs = {'filename': self._file_path(filename)} try: template = Template(**templateargs) if not syntax: template.render_unicode() assert False except Exception: tback = exceptions.RichTraceback() if utf8: assert tback.source == source.decode('utf-8') else: assert tback.source == source
def get_deps(filename): text = util.read_file(filename) lex = lexer.Lexer(text=text, filename=filename) lex.parse() deps = [] for n in lex.template.nodes: if getattr(n, 'keyword', None) == "inherit": deps.append(n.attributes['file']) # TODO: include tags are not handled return deps
def get_deps(self, filename): """Get dependencies for a template (internal function).""" text = util.read_file(filename) lex = lexer.Lexer(text=text, filename=filename) lex.parse() deps = [] for n in lex.template.nodes: keyword = getattr(n, 'keyword', None) if keyword in ["inherit", "namespace"] or isinstance(n, parsetree.IncludeTag): deps.append(n.attributes['file']) return deps
def get_deps(self, filename): """Get dependencies for a template (internal function).""" text = util.read_file(filename) lex = lexer.Lexer(text=text, filename=filename) lex.parse() deps = [] for n in lex.template.nodes: keyword = getattr(n, "keyword", None) if keyword in ["inherit", "namespace"] or isinstance(n, parsetree.IncludeTag): deps.append(n.attributes["file"]) return deps
def get_deps(self, filename): """Get dependencies for a template (internal function).""" text = util.read_file(filename) lex = lexer.Lexer(text=text, filename=filename) lex.parse() deps = [] for n in lex.template.nodes: keyword = getattr(n, 'keyword', None) if keyword in ["inherit", "namespace"]: deps.append(n.attributes['file']) # TODO: include tags are not handled return deps
def source(self): if self.template_source is None: data = util.read_file(self.template_filename) if self.module._source_encoding: return data.decode(self.module._source_encoding) else: return data elif self.module._source_encoding and not isinstance( self.template_source, str): return self.template_source.decode(self.module._source_encoding) else: return self.template_source
def _compile_from_file(self, path, filename): if path is not None: util.verify_directory(os.path.dirname(path)) filemtime = os.stat(filename)[stat.ST_MTIME] if not os.path.exists(path) or \ os.stat(path)[stat.ST_MTIME] < filemtime: data = util.read_file(filename) _compile_module_file( self, data, filename, path, self.module_writer) module = compat.load_module(self.module_id, path) del sys.modules[self.module_id] if module._magic_number != codegen.MAGIC_NUMBER: data = util.read_file(filename) _compile_module_file( self, data, filename, path, self.module_writer) module = compat.load_module(self.module_id, path) del sys.modules[self.module_id] ModuleInfo(module, path, self, filename, None, None) else: # template filename and no module directory, compile code # in memory data = util.read_file(filename) code, module = _compile_text( self, data, filename) self._source = None self._code = code ModuleInfo(module, None, self, filename, code, None) return module
def source(self): if self.template_source is not None: if self.module._source_encoding and not isinstance( self.template_source, compat.text_type): return self.template_source.decode( self.module._source_encoding) else: return self.template_source else: data = util.read_file(self.template_filename) if self.module._source_encoding: return data.decode(self.module._source_encoding) else: return data
def source(self): if self.template_source is not None: if self.module._source_encoding and \ not isinstance(self.template_source, compat.text_type): return self.template_source.decode( self.module._source_encoding) else: return self.template_source else: data = util.read_file(self.template_filename) if self.module._source_encoding: return data.decode(self.module._source_encoding) else: return data
def test_unicode_literal_in_tag(self): self._do_file_test("unicode_arguments.html", [ u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), ], filters=result_lines) self._do_memory_test(util.read_file( self._file_path("unicode_arguments.html")), [ u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), ], filters=result_lines)
def test_crlf(self): template = util.read_file(self._file_path("crlf.html")) nodes = Lexer(template).parse() self._compare( nodes, TemplateNode( {}, [ Text("<html>\r\n\r\n", (1, 1)), PageTag( "page", {"args": "a=['foo',\n 'bar']"}, (3, 1), [], ), Text("\r\n\r\nlike the name says.\r\n\r\n", (4, 26)), ControlLine("for", "for x in [1,2,3]:", False, (8, 1)), Text(" ", (9, 1)), Expression("x", [], (9, 9)), ControlLine("for", "endfor", True, (10, 1)), Text("\r\n", (11, 1)), Expression( "trumpeter == 'Miles' and " "trumpeter or \\\n 'Dizzy'", [], (12, 1), ), Text("\r\n\r\n", (13, 15)), DefTag( "def", {"name": "hi()"}, (15, 1), [Text("\r\n hi!\r\n", (15, 19))], ), Text("\r\n\r\n</html>\r\n", (17, 8)), ], ), ) assert ( flatten_result(Template(template).render()) == """<html> like the name says. 1 2 3 Dizzy </html>""" )
def test_unicode_literal_in_tag(self): self._do_file_test( "unicode_arguments.html", [ u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), ], filters=result_lines ) self._do_memory_test( util.read_file(self._file_path("unicode_arguments.html")), [ u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), u('x is: drôle de petite voix m’a réveillé'), ], filters=result_lines )
def get_deps(self, filename): """Get paths to dependencies for a template.""" text = util.read_file(filename) return self.get_string_deps(text, filename)
def test_read_file(self): fn = os.path.join(os.path.dirname(__file__), 'test_util.py') data = util.read_file(fn, 'rb') self.failUnless('test_util' in str(data)) # str() for py3k
def test_read_file(self): fn = os.path.join(os.path.dirname(__file__), "test_util.py") data = util.read_file(fn, "rb") assert b"test_util" in data
def code(self): if self.module_source is not None: return self.module_source else: return util.read_file(self.module_filename)
def test_read_file(self): fn = os.path.join(os.path.dirname(__file__), "test_util.py") data = util.read_file(fn, "rb") assert "test_util" in str(data) # str() for py3k
def test_read_file(self): fn = os.path.join(os.path.dirname(__file__), 'test_util.py') data = util.read_file(fn, 'rb') assert 'test_util' in str(data) # str() for py3k