我要建议的compiler
模块,但是却忽略了评论:
f.py:
# For Translators: some useful info about the sentence below
_("Some string blah blah")
..和编译器模块:
>>> import compiler
>>> m = compiler.parseFile("f.py")
>>> m
Module(None, Stmt([Discard(CallFunc(Name('_'), [Const('Some string blah blah')], None, None))]))
的AST模块在Python 2.6,似乎这样做。
不知道这是可能的,但如果你使用三引号字符串,而不是..
"""For Translators: some useful info about the sentence below"""
_("Some string blah blah")
..你能可靠地解析和编译器模块Python的文件:
>>> m = compiler.parseFile("f.py")
>>> m
Module('For Translators: some useful info about the sentence below', Stmt([Discard(CallFunc(Name('_'), [Const('Some string blah blah')], None, None))]))
我尝试编写模式完整的脚本来提取文档 - 它不完整,但似乎抓住了大多数文档:http://pastie.org/446156(或github.com/dbr/so_scripts)
其他,更简单,选择是使用正则表达式,例如:
f = """# For Translators: some useful info about the sentence below
_("Some string blah blah")
""".split("\n")
import re
for i, line in enumerate(f):
m = re.findall("\S*# (For Translators: .*)$", line)
if len(m) > 0 and i != len(f):
print "Line Number:", i+1
print "Message:", m
print "Line:", f[i + 1]
..outputs:
Line Number: 1
Message: ['For Translators: some useful info about the sentence below']
Line: _("Some string blah blah")
不知道如何生成.pot
文件,所以我不能要不惜一切的那部分的任何帮助..
来源
2009-04-14 16:00:27
dbr
这是一个.. *位*比编译器模块搞乱更好.. – dbr 2009-04-14 20:24:28