本文整理汇总了Python中zope.tal.htmltalparser.HTMLTALParser.parseFile方法的典型用法代码示例。如果您正苦于以下问题:Python HTMLTALParser.parseFile方法的具体用法?Python HTMLTALParser.parseFile怎么用?Python HTMLTALParser.parseFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zope.tal.htmltalparser.HTMLTALParser
的用法示例。
在下文中一共展示了HTMLTALParser.parseFile方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compilefile
# 需要导入模块: from zope.tal.htmltalparser import HTMLTALParser [as 别名]
# 或者: from zope.tal.htmltalparser.HTMLTALParser import parseFile [as 别名]
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()
示例2: time_tal
# 需要导入模块: from zope.tal.htmltalparser import HTMLTALParser [as 别名]
# 或者: from zope.tal.htmltalparser.HTMLTALParser import parseFile [as 别名]
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)
示例3: profile_tal
# 需要导入模块: from zope.tal.htmltalparser import HTMLTALParser [as 别名]
# 或者: from zope.tal.htmltalparser.HTMLTALParser import parseFile [as 别名]
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)
示例4: tal_strings
# 需要导入模块: from zope.tal.htmltalparser import HTMLTALParser [as 别名]
# 或者: from zope.tal.htmltalparser.HTMLTALParser import parseFile [as 别名]
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
示例5: compilefile
# 需要导入模块: from zope.tal.htmltalparser import HTMLTALParser [as 别名]
# 或者: from zope.tal.htmltalparser.HTMLTALParser import parseFile [as 别名]
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()
示例6: Devnull
# 需要导入模块: from zope.tal.htmltalparser import HTMLTALParser [as 别名]
# 或者: from zope.tal.htmltalparser.HTMLTALParser import parseFile [as 别名]
# 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 = {}
示例7: main
# 需要导入模块: from zope.tal.htmltalparser import HTMLTALParser [as 别名]
# 或者: from zope.tal.htmltalparser.HTMLTALParser import parseFile [as 别名]
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')
示例8: tal_strings
# 需要导入模块: from zope.tal.htmltalparser import HTMLTALParser [as 别名]
# 或者: from zope.tal.htmltalparser.HTMLTALParser import parseFile [as 别名]
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