自动向pot文件添加评论

2024-05-23 14:29:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从py文件中提取一些注释,这些注释提供翻译的上下文,而不是手动编辑.pot文件基本上我想从这个python文件开始:

# For Translators: some useful info about the sentence below
_("Some string blah blah")

此pot文件:

^{pr2}$

Tags: 文件thepyinfo编辑forsome手动
2条回答

在我大发雷霆之后,我找到了最好的办法:

#. Translators:
# Blah blah blah
_("String")

然后使用搜索注释。是这样的:

^{pr2}$

我本打算建议使用compiler模块,但它忽略了注释:

f.py公司:

# For Translators: some useful info about the sentence below
_("Some string blah blah")

…以及编译器模块:

^{pr2}$

python2.6中的AST模块似乎也是这样做的。在

不确定是否可行,但如果您使用三重引号字符串。。在

"""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))]))

我试图编写一个mode complete脚本来提取docstring—它是不完整的,但似乎抓住了大多数docstring: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]

..输出:

Line Number: 1
Message: ['For Translators: some useful info about the sentence below']
Line: _("Some string blah blah")

不确定.pot文件是如何生成的,因此我对该部分没有任何帮助。。在

相关问题 更多 >