def _compile_module_file(template, text, filename, outputpath): identifier = template.module_id lexer = Lexer(text, filename, disable_unicode=template.disable_unicode, input_encoding=template.input_encoding, preprocessor=template.preprocessor) node = lexer.parse() source = codegen.compile(node, template.uri, filename, default_filters=template.default_filters, buffer_filters=template.buffer_filters, imports=template.imports, source_encoding=lexer.encoding, generate_magic_comment=True) # make tempfiles in the same location as the ultimate # location. this ensures they're on the same filesystem, # avoiding synchronization issues. (dest, name) = tempfile.mkstemp(dir=os.path.dirname(outputpath)) if isinstance(source, unicode): source = source.encode(lexer.encoding or 'ascii') os.write(dest, source) os.close(dest) shutil.move(name, outputpath)
def _compile_module_file(template, text, filename, outputpath): identifier = template.module_id lexer = Lexer(text, filename, disable_unicode=template.disable_unicode, input_encoding=template.input_encoding, preprocessor=template.preprocessor) node = lexer.parse() source = codegen.compile(node, template.uri, filename, default_filters=template.default_filters, buffer_filters=template.buffer_filters, imports=template.imports, source_encoding=lexer.encoding, generate_magic_comment=True) (dest, name) = tempfile.mkstemp() if isinstance(source, unicode): source = source.encode(lexer.encoding or 'ascii') os.write(dest, source) os.close(dest) shutil.move(name, outputpath)
def _compile_text(template, text, filename): identifier = template.module_id lexer = Lexer(text, filename, disable_unicode=template.disable_unicode, input_encoding=template.input_encoding, preprocessor=template.preprocessor) node = lexer.parse() source = codegen.compile(node, template.uri, filename, default_filters=template.default_filters, buffer_filters=template.buffer_filters, imports=template.imports, source_encoding=lexer.encoding, generate_magic_comment=template.disable_unicode) cid = identifier if not util.py3k and isinstance(cid, unicode): cid = cid.encode() module = types.ModuleType(cid) code = compile(source, cid, 'exec') exec code in module.__dict__, module.__dict__ return (source, module)
def test_lexer(): from mako.lexer import Lexer from mako import codegen lexer = Lexer("hello, ${name}!") node = lexer.parse() print(lexer.template) source = codegen.compile(node, "a", default_filters=[]) print(source)
def _compile_module_file(template, text, filename, outputpath): identifier = template.module_id (dest, name) = tempfile.mkstemp() lexer = Lexer(text, filename, input_encoding=template.input_encoding, preprocessor=template.preprocessor) node = lexer.parse() source = codegen.compile(node, template.uri, filename, default_filters=template.default_filters, buffer_filters=template.buffer_filters, imports=template.imports, source_encoding=lexer.encoding) os.write(dest, source) os.close(dest) shutil.move(name, outputpath)
def _compile_text(template, text, filename): identifier = template.module_id lexer = Lexer(text, filename, input_encoding=template.input_encoding, preprocessor=template.preprocessor) node = lexer.parse() source = codegen.compile(node, template.uri, filename, default_filters=template.default_filters, buffer_filters=template.buffer_filters, imports=template.imports, source_encoding=lexer.encoding) #print source cid = identifier module = imp.new_module(cid) code = compile(source, cid, 'exec') exec code in module.__dict__, module.__dict__ return (source, module)
def _mako_file_check(filename, show_source): with codecs.open(filename, "rb", "utf-8") as f: text = f.read() lexer = Lexer(text, filename) visitor = MakoVisitor() node = lexer.parse() node.accept_visitor(visitor) count = 0 if visitor.locations: text_lines = text.splitlines() for line, _ in visitor.locations: if not text_lines[line - 1].endswith( ("# noqa", "<%doc>noqa</%doc>")): print(f"{filename}:{line}") if show_source: print(f" {text_lines[line - 1].lstrip()}") count += 1 return count
def _compile(template, text, filename, generate_magic_comment): lexer = Lexer(text, filename, disable_unicode=template.disable_unicode, input_encoding=template.input_encoding, preprocessor=template.preprocessor) node = lexer.parse() source = codegen.compile(node, template.uri, filename, default_filters=template.default_filters, buffer_filters=template.buffer_filters, imports=template.imports, source_encoding=lexer.encoding, generate_magic_comment=generate_magic_comment, disable_unicode=template.disable_unicode, strict_undefined=template.strict_undefined, enable_loop=template.enable_loop, reserved_names=template.reserved_names) return source, lexer