openlp-core team mailing list archive
-
openlp-core team
-
Mailing list archive
-
Message #00885
[Merge] lp:~mgorven/openlp/get-strings-ast into lp:openlp
Michael Gorven has proposed merging lp:~mgorven/openlp/get-strings-ast into lp:openlp.
Requested reviews:
OpenLP Core (openlp-core)
--
https://code.launchpad.net/~mgorven/openlp/get-strings-ast/+merge/15328
Your team OpenLP Core is subscribed to branch lp:openlp.
=== modified file 'openlp/core/ui/amendthemeform.py'
--- openlp/core/ui/amendthemeform.py 2009-11-21 14:32:19 +0000
+++ openlp/core/ui/amendthemeform.py 2009-11-27 17:45:24 +0000
@@ -193,7 +193,7 @@
def onImageToolButtonClicked(self):
filename = QtGui.QFileDialog.getOpenFileName(
- self, self.trUtf8('Open file'))
+ self, self.trUtf8(u'Open file'))
if filename:
self.ImageLineEdit.setText(filename)
self.theme.background_filename = filename
=== added directory 'scripts'
=== renamed file 'openlp-get-strings.py' => 'scripts/get-strings.py'
--- openlp-get-strings.py 2009-11-21 12:53:36 +0000
+++ scripts/get-strings.py 2009-11-27 17:45:24 +0000
@@ -24,7 +24,7 @@
###############################################################################
import os
-import re
+from ast import parse, NodeVisitor, Str
ts_file = u"""<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
@@ -42,27 +42,37 @@
<translation type="unfinished"></translation>
</message>
"""
-find_trUtf8 = re.compile(r"trUtf8\(u?(['\"])([^\1]+)\1\)", re.UNICODE)
-strings = {}
-
-def parse_file(filename):
- global strings
+
+class StringExtractor(NodeVisitor):
+
+ def __init__(self, strings, filename):
+ self.filename = filename
+ self.strings = strings
+ self.classname = 'unknown'
+
+ def visit_ClassDef(self, node):
+ self.classname = node.name
+ self.generic_visit(node)
+
+ def visit_Call(self, node):
+ if hasattr(node.func, 'attr') and node.func.attr == 'trUtf8' and isinstance(node.args[0], Str):
+ string = node.args[0].s
+ key = '%s-%s' % (self.classname, string)
+ self.strings[key] = [self.classname, self.filename, node.lineno, string]
+ self.generic_visit(node)
+
+def parse_file(filename, strings):
file = open(filename, u'r')
- class_name = u''
- line_number = 0
- for line in file:
- line_number += 1
- if line[:5] == u'class':
- class_name = line[6:line.find(u'(')]
- continue
- for match in find_trUtf8.finditer(line):
- key = u'%s-%s' % (class_name, match.group(2))
- if not key in strings:
- strings[key] = [class_name, filename, line_number, match.group(2)]
+ try:
+ ast = parse(file.read())
+ except SyntaxError, e:
+ print "Unable to parse %s: %s" % (filename, e)
+ return
file.close()
-def write_file(filename):
- global strings
+ StringExtractor(strings, filename).visit(ast)
+
+def write_file(filename, strings):
translation_file = u''
translation_contexts = []
translation_messages = []
@@ -79,18 +89,19 @@
translation_contexts.append(current_context)
translation_file = ts_file % (u''.join(translation_contexts))
file = open(filename, u'w')
- file.write(translation_file)
+ file.write(translation_file.encode('utf8'))
file.close()
def main():
+ strings = {}
start_dir = u'.'
for root, dirs, files in os.walk(start_dir):
for file in files:
if file.endswith(u'.py'):
print u'Parsing "%s"' % file
- parse_file(os.path.join(root, file))
+ parse_file(os.path.join(root, file), strings)
print u'Generating TS file...',
- write_file(os.path.join(start_dir, u'i18n', u'openlp_en.ts'))
+ write_file(os.path.join(start_dir, u'i18n', u'openlp_en.ts'), strings)
print u'done.'
if __name__ == u'__main__':
Follow ups