Ejemplo n.º 1
0
def compilefile(file, mode=None):
    assert mode in ("html", "xml", None)
    if mode is None:
        ext = os.path.splitext(file)[1]
        if ext.lower() in (".html", ".htm"):
            mode = "html"
        else:
            mode = "xml"
    # make sure we can find the file
    prefix = os.path.dirname(os.path.abspath(__file__)) + os.path.sep
    if (not os.path.exists(file)
        and os.path.exists(os.path.join(prefix, file))):
        file = os.path.join(prefix, file)
    # normalize filenames for test output
    filename = os.path.abspath(file)
    if filename.startswith(prefix):
        filename = filename[len(prefix):]
    filename = filename.replace(os.sep, '/') # test files expect slashes
    # parse
    from zope.tal.talgenerator import TALGenerator
    if mode == "html":
        from zope.tal.htmltalparser import HTMLTALParser
        p = HTMLTALParser(gen=TALGenerator(source_file=filename, xml=0))
    else:
        from zope.tal.talparser import TALParser
        p = TALParser(gen=TALGenerator(source_file=filename))
    p.parseFile(file)
    return p.getCode()
Ejemplo n.º 2
0
def compilefile(file, mode=None):
    assert mode in ("html", "xml", None)
    if mode is None:
        ext = os.path.splitext(file)[1]
        if ext.lower() in (".html", ".htm"):
            mode = "html"
        else:
            mode = "xml"
    # make sure we can find the file
    prefix = os.path.dirname(os.path.abspath(__file__)) + os.path.sep
    if (not os.path.exists(file)
            and os.path.exists(os.path.join(prefix, file))):
        file = os.path.join(prefix, file)
    # normalize filenames for test output
    filename = os.path.abspath(file)
    if filename.startswith(prefix):
        filename = filename[len(prefix):]
    filename = filename.replace(os.sep, '/')  # test files expect slashes
    # parse
    from zope.tal.talgenerator import TALGenerator
    if mode == "html":
        from zope.tal.htmltalparser import HTMLTALParser
        p = HTMLTALParser(gen=TALGenerator(source_file=filename, xml=0))
    else:
        from zope.tal.talparser import TALParser
        p = TALParser(gen=TALGenerator(source_file=filename))
    p.parseFile(file)
    return p.getCode()
Ejemplo n.º 3
0
def time_tal(fn, count):
    p = HTMLTALParser()
    p.parseFile(fn)
    program, macros = p.getCode()
    engine = DummyEngine(macros)
    engine.globals = data
    tal = TALInterpreter(program, macros, engine, StringIO(), wrap=0,
                         tal=1, strictinsert=0)
    return time_apply(tal, (), {}, count)
Ejemplo n.º 4
0
def profile_tal(fn, count, profiler):
    p = HTMLTALParser()
    p.parseFile(fn)
    program, macros = p.getCode()
    engine = DummyEngine(macros)
    engine.globals = data
    tal = TALInterpreter(program, macros, engine, StringIO(), wrap=0,
                         tal=1, strictinsert=0)
    for i in range(4):
        tal()
    r = [None] * count
    for i in r:
        profiler.runcall(tal)
Ejemplo n.º 5
0
def time_tal(fn, count):
    p = HTMLTALParser()
    p.parseFile(fn)
    program, macros = p.getCode()
    engine = DummyEngine(macros)
    engine.globals = data
    tal = TALInterpreter(program,
                         macros,
                         engine,
                         StringIO(),
                         wrap=0,
                         tal=1,
                         strictinsert=0)
    return time_apply(tal, (), {}, count)
Ejemplo n.º 6
0
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()):
    """Retrieve all TAL messages from `dir` that are in the `domain`.
    """
    # We import zope.tal.talgettext here because we can't rely on the
    # right sys path until app_dir has run
    from zope.tal.talgettext import POEngine, POTALInterpreter
    from zope.tal.htmltalparser import HTMLTALParser
    from zope.tal.talparser import TALParser
    import sys
    # i18n patch if templates using special characters
    reload(sys)
    sys.setdefaultencoding('UTF8')
    engine = POEngine()

    class Devnull(object):
        def write(self, s):
            pass

    for filename in (find_files(dir, '*.*pt', exclude=tuple(exclude)) +
                     find_files(dir, '*.html', exclude=tuple(exclude)) +
                     find_files(dir, '*.kupu', exclude=tuple(exclude)) +
                     find_files(dir, '*.pox', exclude=tuple(exclude)) +
                     find_files(dir, '*.xsl', exclude=tuple(exclude))):
        try:
            engine.file = filename
            name, ext = os.path.splitext(filename)
            if ext == '.html' or ext.endswith('pt'):
                p = HTMLTALParser()
            else:
                p = TALParser()
            p.parseFile(filename)
            program, macros = p.getCode()
            POTALInterpreter(program, macros, engine, stream=Devnull(),
                             metal=False)()
        except:  # Hee hee, I love bare excepts!
            print 'There was an error processing', filename
            traceback.print_exc()

    # See whether anything in the domain was found
    if not domain in engine.catalog:
        return {}
    # We do not want column numbers.
    catalog = engine.catalog[domain].copy()
    # When the Domain is 'default', then this means that none was found;
    # Include these strings; yes or no?
    if include_default_domain:
        catalog.update(engine.catalog['default'])
    for msgid, locations in catalog.items():
        catalog[msgid] = [(l[0], l[1][0]) for l in locations]
    return catalog
Ejemplo n.º 7
0
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()):
    """Retrieve all TAL messages from `dir` that are in the `domain`.
    """
    # We import zope.tal.talgettext here because we can't rely on the
    # right sys path until app_dir has run
    from zope.tal.talgettext import POEngine, POTALInterpreter
    from zope.tal.htmltalparser import HTMLTALParser
    from zope.tal.talparser import TALParser
    engine = POEngine()

    class Devnull(object):
        def write(self, s):
            pass

    for filename in (find_files(dir, '*.*pt', exclude=tuple(exclude)) +
                     find_files(dir, '*.html', exclude=tuple(exclude)) +
                     find_files(dir, '*.kupu', exclude=tuple(exclude)) +
                     find_files(dir, '*.pox', exclude=tuple(exclude)) +
                     find_files(dir, '*.xsl', exclude=tuple(exclude))):
        try:
            engine.file = filename
            name, ext = os.path.splitext(filename)
            if ext == '.html' or ext.endswith('pt'):
                p = HTMLTALParser()
            else:
                p = TALParser()
            p.parseFile(filename)
            program, macros = p.getCode()
            POTALInterpreter(program,
                             macros,
                             engine,
                             stream=Devnull(),
                             metal=False)()
        except:  # Hee hee, I love bare excepts!
            print 'There was an error processing', filename
            traceback.print_exc()

    # See whether anything in the domain was found
    if not domain in engine.catalog:
        return {}
    # We do not want column numbers.
    catalog = engine.catalog[domain].copy()
    # When the Domain is 'default', then this means that none was found;
    # Include these strings; yes or no?
    if include_default_domain:
        catalog.update(engine.catalog['default'])
    for msgid, locations in catalog.items():
        catalog[msgid] = [(l[0], l[1][0]) for l in locations]
    return catalog
Ejemplo n.º 8
0
def profile_tal(fn, count, profiler):
    p = HTMLTALParser()
    p.parseFile(fn)
    program, macros = p.getCode()
    engine = DummyEngine(macros)
    engine.globals = data
    tal = TALInterpreter(program,
                         macros,
                         engine,
                         StringIO(),
                         wrap=0,
                         tal=1,
                         strictinsert=0)
    for i in range(4):
        tal()
    r = [None] * count
    for i in r:
        profiler.runcall(tal)
Ejemplo n.º 9
0
def compilefile(file, mode=None):
    assert mode in ("html", "xml", None)
    if mode is None:
        ext = os.path.splitext(file)[1]
        if ext.lower() in (".html", ".htm"):
            mode = "html"
        else:
            mode = "xml"
    from zope.tal.talgenerator import TALGenerator
    filename = os.path.abspath(file)
    prefix = os.path.dirname(os.path.abspath(__file__)) + os.path.sep
    if filename.startswith(prefix):
        filename = filename[len(prefix):]
    filename = filename.replace(os.sep, '/')  # test files expect slashes
    if mode == "html":
        from zope.tal.htmltalparser import HTMLTALParser
        p = HTMLTALParser(gen=TALGenerator(source_file=filename, xml=0))
    else:
        from zope.tal.talparser import TALParser
        p = TALParser(gen=TALGenerator(source_file=filename))
    p.parseFile(file)
    return p.getCode()
Ejemplo n.º 10
0
def compilefile(file, mode=None):
    assert mode in ("html", "xml", None)
    if mode is None:
        ext = os.path.splitext(file)[1]
        if ext.lower() in (".html", ".htm"):
            mode = "html"
        else:
            mode = "xml"
    from zope.tal.talgenerator import TALGenerator
    filename = os.path.abspath(file)
    prefix = os.path.dirname(os.path.abspath(__file__)) + os.path.sep
    if filename.startswith(prefix):
        filename = filename[len(prefix):]
    filename = filename.replace(os.sep, '/') # test files expect slashes
    if mode == "html":
        from zope.tal.htmltalparser import HTMLTALParser
        p = HTMLTALParser(gen=TALGenerator(source_file=filename, xml=0))
    else:
        from zope.tal.talparser import TALParser
        p = TALParser(gen=TALGenerator(source_file=filename))
    p.parseFile(file)
    return p.getCode()
Ejemplo n.º 11
0
def tal_strings(dir, domain="zope", include_default_domain=False):
    """Retrieve all TAL messages from `dir` that are in the `domain`.
    """
    # We import zope.tal.talgettext here because we can't rely on the
    # right sys path until app_dir has run
    from zope.tal.talgettext import POEngine, POTALInterpreter
    from zope.tal.htmltalparser import HTMLTALParser
    engine = POEngine()

    class Devnull(object):
        def write(self, s):
            pass

    for filename in find_files(dir, '*.pt'):
        try:
            engine.file = filename
            p = HTMLTALParser()
            p.parseFile(filename)
            program, macros = p.getCode()
            POTALInterpreter(program, macros, engine, stream=Devnull(),
                             metal=False)()
        except: # Hee hee, I love bare excepts!
            print 'There was an error processing', filename
            traceback.print_exc()

    # See whether anything in the domain was found
    if not engine.catalog.has_key(domain):
        return {}
    # We do not want column numbers.
    catalog = engine.catalog[domain].copy()
    # When the Domain is 'default', then this means that none was found;
    # Include these strings; yes or no?
    if include_default_domain:
        catalog.update(engine.catalog['zope'])
    for msgid, locations in catalog.items():
        catalog[msgid] = map(lambda l: (l[0], l[1][0]), locations)
    return catalog
Ejemplo n.º 12
0
    # We don't care about the rendered output of the .pt file
    class Devnull(object):
        def write(self, s):
            pass

    # check if we've already instantiated an engine;
    # if not, use the stupidest one available
    if not engine:
        engine = POEngine()

    # process each file specified
    for filename in args:
        try:
            engine.file = filename
            p = HTMLTALParser()
            p.parseFile(filename)
            program, macros = p.getCode()
            POTALInterpreter(program, macros, engine, stream=Devnull(),
                             metal=False)()
        except: # Hee hee, I love bare excepts!
            print 'There was an error processing', filename
            traceback.print_exc()

    # Now output the keys in the engine.  Write them to a file if --output or
    # --update was specified; otherwise use standard out.
    if (outfile is None):
        outfile = sys.stdout
    else:
        outfile = file(outfile, update_mode and "a" or "w")

    catalog = {}
Ejemplo n.º 13
0
    # We don't care about the rendered output of the .pt file
    class Devnull(object):
        def write(self, s):
            pass

    # check if we've already instantiated an engine;
    # if not, use the stupidest one available
    if not engine:
        engine = POEngine()

    # process each file specified
    for filename in args:
        try:
            engine.file = filename
            p = HTMLTALParser()
            p.parseFile(filename)
            program, macros = p.getCode()
            POTALInterpreter(program,
                             macros,
                             engine,
                             stream=Devnull(),
                             metal=False)()
        except:  # Hee hee, I love bare excepts!
            print 'There was an error processing', filename
            traceback.print_exc()

    # Now output the keys in the engine.  Write them to a file if --output or
    # --update was specified; otherwise use standard out.
    if (outfile is None):
        outfile = sys.stdout
    else:
Ejemplo n.º 14
0
def main():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:],
            'ho:u:',
            ['help', 'output=', 'update='])
    except getopt.error as msg:
        usage(1, msg)

    outfile = None
    engine = None
    update_mode = False
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-o', '--output'):
            outfile = arg
        elif opt in ('-u', '--update'):
            update_mode = True
            if outfile is None:
                outfile = arg
            engine = UpdatePOEngine(filename=arg)

    if not args:
        print('nothing to do')
        return

    # We don't care about the rendered output of the .pt file
    class Devnull(object):
        def write(self, s):
            pass

    # check if we've already instantiated an engine;
    # if not, use the stupidest one available
    if not engine:
        engine = POEngine()

    # process each file specified
    for filename in args:
        try:
            engine.file = filename
            p = HTMLTALParser()
            p.parseFile(filename)
            program, macros = p.getCode()
            POTALInterpreter(program, macros, engine, stream=Devnull(),
                             metal=False)()
        except: # Hee hee, I love bare excepts!
            print('There was an error processing', filename)
            traceback.print_exc()

    # Now output the keys in the engine.  Write them to a file if --output or
    # --update was specified; otherwise use standard out.
    if (outfile is None):
        outfile = sys.stdout
    else:
        outfile = file(outfile, update_mode and "a" or "w")

    catalog = {}
    for domain in engine.catalog:
        catalog.update(engine.catalog[domain])

    messages = catalog.copy()
    try:
        messages.update(engine.base)
    except AttributeError:
        pass
    if '' not in messages:
        print(pot_header % {'time': time.ctime(), 'version': __version__},
              file=outfile)

    # TODO: You should not sort by msgid, but by filename and position. (SR)
    msgids = sorted(catalog)
    for msgid in msgids:
        positions = engine.catalog[msgid]
        for filename, position in positions:
            outfile.write('#: %s:%s\n' % (filename, position[0]))

        outfile.write('msgid "%s"\n' % msgid)
        outfile.write('msgstr ""\n')
        outfile.write('\n')
Ejemplo n.º 15
0
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()):
    """Retrieve all TAL messages from `dir` that are in the `domain`.
    """
    # We import zope.tal.talgettext here because we can't rely on the
    # right sys path until app_dir has run
    from zope.tal.talgettext import POEngine, POTALInterpreter
    from zope.tal.htmltalparser import HTMLTALParser
    from zope.tal.talparser import TALParser
    engine = POEngine()

    class Devnull(object):

        def write(self, s):
            pass

    for filename in (find_files(dir, '*.*pt', exclude=tuple(exclude)) +
                     find_files(dir, '*.html', exclude=tuple(exclude)) +
                     find_files(dir, '*.kupu', exclude=tuple(exclude)) +
                     find_files(dir, '*.pox', exclude=tuple(exclude)) +
                     find_files(dir, '*.xsl', exclude=tuple(exclude))):
        engine.file = filename
        name, ext = os.path.splitext(filename)
        # First try with standard zope.tal parsers.
        if ext == '.html' or ext.endswith('pt'):
            parser = HTMLTALParser()
        else:
            parser = TALParser()
        try:
            parser.parseFile(filename)
            program, macros = parser.getCode()
            POTALInterpreter(program, macros, engine, stream=Devnull(),
                             metal=False)()
        except KeyboardInterrupt:
            raise
        except:  # Hee hee, I love bare excepts!
            if ext == '.html' or ext.endswith('pt'):
                # We can have one retry with our own generator.
                gen = DudeGenerator(xml=0)
                parser = HTMLTALParser(gen=gen)
                try:
                    parser.parseFile(filename)
                    program, macros = parser.getCode()
                    POTALInterpreter(program, macros, engine, stream=Devnull(),
                                     metal=False)()
                except:  # Hee hee, I love bare excepts!
                    print 'There was an error processing', filename
                    traceback.print_exc()
            else:
                print 'There was an error processing', filename
                traceback.print_exc()

    # See whether anything in the domain was found
    if domain not in engine.catalog:
        return {}
    # We do not want column numbers.
    catalog = engine.catalog[domain].copy()
    # When the Domain is 'default', then this means that none was found;
    # Include these strings; yes or no?
    if include_default_domain:
        catalog.update(engine.catalog['default'])
    for msgid, locations in catalog.items():
        catalog[msgid] = [(l[0], l[1][0]) for l in locations]
    return catalog
Ejemplo n.º 16
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'ho:u:',
                                   ['help', 'output=', 'update='])
    except getopt.error as msg:
        usage(1, msg)

    outfile = None
    engine = None
    update_mode = False
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-o', '--output'):
            outfile = arg
        elif opt in ('-u', '--update'):
            update_mode = True
            if outfile is None:
                outfile = arg
            engine = UpdatePOEngine(filename=arg)

    if not args:
        print('nothing to do')
        return

    # We don't care about the rendered output of the .pt file
    class Devnull(object):
        def write(self, s):
            pass

    # check if we've already instantiated an engine;
    # if not, use the stupidest one available
    if not engine:
        engine = POEngine()

    # process each file specified
    for filename in args:
        try:
            engine.file = filename
            p = HTMLTALParser()
            p.parseFile(filename)
            program, macros = p.getCode()
            POTALInterpreter(program,
                             macros,
                             engine,
                             stream=Devnull(),
                             metal=False)()
        except:  # Hee hee, I love bare excepts!
            print('There was an error processing', filename)
            traceback.print_exc()

    # Now output the keys in the engine.  Write them to a file if --output or
    # --update was specified; otherwise use standard out.
    if (outfile is None):
        outfile = sys.stdout
    else:
        outfile = file(outfile, update_mode and "a" or "w")

    catalog = {}
    for domain in engine.catalog:
        catalog.update(engine.catalog[domain])

    messages = catalog.copy()
    try:
        messages.update(engine.base)
    except AttributeError:
        pass
    if '' not in messages:
        print(pot_header % {
            'time': time.ctime(),
            'version': __version__
        },
              file=outfile)

    # TODO: You should not sort by msgid, but by filename and position. (SR)
    msgids = sorted(catalog)
    for msgid in msgids:
        positions = engine.catalog[msgid]
        for filename, position in positions:
            outfile.write('#: %s:%s\n' % (filename, position[0]))

        outfile.write('msgid "%s"\n' % msgid)
        outfile.write('msgstr ""\n')
        outfile.write('\n')