Merge lp:~mgorven/openlp/get-strings-ast into lp:openlp

Proposed by Michael Gorven
Status: Merged
Approved by: Michael Gorven
Approved revision: 668
Merged at revision: not available
Proposed branch: lp:~mgorven/openlp/get-strings-ast
Merge into: lp:openlp
Diff against target: 105 lines (+34/-23)
2 files modified
openlp/core/ui/amendthemeform.py (+1/-1)
scripts/get-strings.py (+33/-22)
To merge this branch: bzr merge lp:~mgorven/openlp/get-strings-ast
Reviewer Review Type Date Requested Status
Michael Gorven (community) Approve
Raoul Snyman Approve
Tim Bentley Approve
Review via email: mp+15328@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Tim Bentley (trb143) wrote :

Approved

review: Approve
Revision history for this message
Raoul Snyman (raoul-snyman) wrote :

"La, la, la; don't mind me..."???

You're making me worried!

review: Approve
Revision history for this message
Michael Gorven (mgorven) wrote :

 status approved

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openlp/core/ui/amendthemeform.py'
2--- openlp/core/ui/amendthemeform.py 2009-11-21 14:32:19 +0000
3+++ openlp/core/ui/amendthemeform.py 2009-11-27 17:45:24 +0000
4@@ -193,7 +193,7 @@
5
6 def onImageToolButtonClicked(self):
7 filename = QtGui.QFileDialog.getOpenFileName(
8- self, self.trUtf8('Open file'))
9+ self, self.trUtf8(u'Open file'))
10 if filename:
11 self.ImageLineEdit.setText(filename)
12 self.theme.background_filename = filename
13
14=== added directory 'scripts'
15=== renamed file 'openlp-get-strings.py' => 'scripts/get-strings.py'
16--- openlp-get-strings.py 2009-11-21 12:53:36 +0000
17+++ scripts/get-strings.py 2009-11-27 17:45:24 +0000
18@@ -24,7 +24,7 @@
19 ###############################################################################
20
21 import os
22-import re
23+from ast import parse, NodeVisitor, Str
24
25 ts_file = u"""<?xml version="1.0" encoding="utf-8"?>
26 <!DOCTYPE TS>
27@@ -42,27 +42,37 @@
28 <translation type="unfinished"></translation>
29 </message>
30 """
31-find_trUtf8 = re.compile(r"trUtf8\(u?(['\"])([^\1]+)\1\)", re.UNICODE)
32-strings = {}
33-
34-def parse_file(filename):
35- global strings
36+
37+class StringExtractor(NodeVisitor):
38+
39+ def __init__(self, strings, filename):
40+ self.filename = filename
41+ self.strings = strings
42+ self.classname = 'unknown'
43+
44+ def visit_ClassDef(self, node):
45+ self.classname = node.name
46+ self.generic_visit(node)
47+
48+ def visit_Call(self, node):
49+ if hasattr(node.func, 'attr') and node.func.attr == 'trUtf8' and isinstance(node.args[0], Str):
50+ string = node.args[0].s
51+ key = '%s-%s' % (self.classname, string)
52+ self.strings[key] = [self.classname, self.filename, node.lineno, string]
53+ self.generic_visit(node)
54+
55+def parse_file(filename, strings):
56 file = open(filename, u'r')
57- class_name = u''
58- line_number = 0
59- for line in file:
60- line_number += 1
61- if line[:5] == u'class':
62- class_name = line[6:line.find(u'(')]
63- continue
64- for match in find_trUtf8.finditer(line):
65- key = u'%s-%s' % (class_name, match.group(2))
66- if not key in strings:
67- strings[key] = [class_name, filename, line_number, match.group(2)]
68+ try:
69+ ast = parse(file.read())
70+ except SyntaxError, e:
71+ print "Unable to parse %s: %s" % (filename, e)
72+ return
73 file.close()
74
75-def write_file(filename):
76- global strings
77+ StringExtractor(strings, filename).visit(ast)
78+
79+def write_file(filename, strings):
80 translation_file = u''
81 translation_contexts = []
82 translation_messages = []
83@@ -79,18 +89,19 @@
84 translation_contexts.append(current_context)
85 translation_file = ts_file % (u''.join(translation_contexts))
86 file = open(filename, u'w')
87- file.write(translation_file)
88+ file.write(translation_file.encode('utf8'))
89 file.close()
90
91 def main():
92+ strings = {}
93 start_dir = u'.'
94 for root, dirs, files in os.walk(start_dir):
95 for file in files:
96 if file.endswith(u'.py'):
97 print u'Parsing "%s"' % file
98- parse_file(os.path.join(root, file))
99+ parse_file(os.path.join(root, file), strings)
100 print u'Generating TS file...',
101- write_file(os.path.join(start_dir, u'i18n', u'openlp_en.ts'))
102+ write_file(os.path.join(start_dir, u'i18n', u'openlp_en.ts'), strings)
103 print u'done.'
104
105 if __name__ == u'__main__':