Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
Arquivo: template.py Projeto: 42qu/zqa
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
Exemplo n.º 11
0
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
Exemplo n.º 12
0
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)