From f76516f2a0a024e660492daf3faba0bd4941acb6 Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 6 Nov 2009 11:29:38 +0200 Subject: [PATCH 01/10] Improve transaltion string regex. --- openlp-get-strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 openlp-get-strings.py diff --git a/openlp-get-strings.py b/openlp-get-strings.py old mode 100644 new mode 100755 index e53924b02..dc101e945 --- a/openlp-get-strings.py +++ b/openlp-get-strings.py @@ -42,7 +42,7 @@ ts_message = u""" """ -find_trUtf8 = re.compile(r"trUtf8\(u'([\.:;\\&\w]+)'\)", re.UNICODE) +find_trUtf8 = re.compile(r"trUtf8\(u['\"]([^)]+)['\"]\)", re.UNICODE) strings = {} def parse_file(filename): From ab42730f1ee747fc1edd94277be007f0905acb90 Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 6 Nov 2009 11:40:31 +0200 Subject: [PATCH 02/10] Use string quotes to detect end. --- openlp-get-strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openlp-get-strings.py b/openlp-get-strings.py index dc101e945..adb2f8821 100755 --- a/openlp-get-strings.py +++ b/openlp-get-strings.py @@ -42,7 +42,7 @@ ts_message = u""" """ -find_trUtf8 = re.compile(r"trUtf8\(u['\"]([^)]+)['\"]\)", re.UNICODE) +find_trUtf8 = re.compile(r"trUtf8\(u?(['\"])([^\1]+)\1\)", re.UNICODE) strings = {} def parse_file(filename): From a89cec05da8099fde6a29668ee498247dc76b759 Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 6 Nov 2009 11:52:04 +0200 Subject: [PATCH 03/10] Change to Unicode string. --- openlp/core/ui/amendthemeform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openlp/core/ui/amendthemeform.py b/openlp/core/ui/amendthemeform.py index 2723c17d8..533f49691 100644 --- a/openlp/core/ui/amendthemeform.py +++ b/openlp/core/ui/amendthemeform.py @@ -200,7 +200,7 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): def onImageToolButtonClicked(self): filename = QtGui.QFileDialog.getOpenFileName( - self, self.trUtf8('Open file')) + self, self.trUtf8(u'Open file')) if filename != u'': self.ImageLineEdit.setText(filename) self.theme.background_filename = filename From e9d6fe6f7628c7a90687fede0ef0fe8ac923210f Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 27 Nov 2009 12:50:39 +0200 Subject: [PATCH 04/10] Use AST to find strings. --- openlp-get-strings.py | 51 +++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/openlp-get-strings.py b/openlp-get-strings.py index adb2f8821..040171d54 100755 --- a/openlp-get-strings.py +++ b/openlp-get-strings.py @@ -25,6 +25,9 @@ import os import re +from os import walk +from ast import parse, NodeVisitor, Str +from sys import exit ts_file = u""" @@ -42,27 +45,41 @@ ts_message = u""" """ -find_trUtf8 = re.compile(r"trUtf8\(u?(['\"])([^\1]+)\1\)", re.UNICODE) strings = {} +counts=0 + +class StringExtractor(NodeVisitor): + + def __init__(self, strings, filename): + self.strings = strings + self.filename = filename + self.classname = 'unknown' + + def visit_ClassDef(self, node): + self.classname = node.name + self.generic_visit(node) + + def visit_Call(self, node): + global counts + if hasattr(node.func, 'attr') and node.func.attr == 'trUtf8' and isinstance(node.args[0], Str): + counts += 1 + string = node.args[0].s + print string + key = '%s-%s' % (self.classname, string) + strings[key] = [self.classname, self.filename, node.lineno, string] + self.generic_visit(node) def parse_file(filename): - global 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(1)) - if not key in strings: - strings[key] = [class_name, filename, line_number, match.group(1)] + try: + ast = parse(file.read()) + except SyntaxError, e: + return file.close() + StringExtractor(strings, filename).visit(ast) + def write_file(filename): - global strings translation_file = u'' translation_contexts = [] translation_messages = [] @@ -87,9 +104,11 @@ def main(): for root, dirs, files in os.walk(start_dir): for file in files: if file.endswith(u'.py'): - print u'Parsing "%s"' % file + #print u'Parsing "%s"' % file parse_file(os.path.join(root, file)) - print u'Generating TS file...', + #print u'Generating TS file...', + print counts + exit(0) write_file(os.path.join(start_dir, u'i18n', u'openlp_en.ts')) print u'done.' From 13557089b986e54787b6f22c6547e3978dbbc55c Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 27 Nov 2009 12:53:39 +0200 Subject: [PATCH 05/10] Write translation file. --- openlp-get-strings.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/openlp-get-strings.py b/openlp-get-strings.py index 040171d54..f070735f9 100755 --- a/openlp-get-strings.py +++ b/openlp-get-strings.py @@ -46,7 +46,6 @@ ts_message = u""" """ strings = {} -counts=0 class StringExtractor(NodeVisitor): @@ -60,11 +59,8 @@ class StringExtractor(NodeVisitor): self.generic_visit(node) def visit_Call(self, node): - global counts if hasattr(node.func, 'attr') and node.func.attr == 'trUtf8' and isinstance(node.args[0], Str): - counts += 1 string = node.args[0].s - print string key = '%s-%s' % (self.classname, string) strings[key] = [self.classname, self.filename, node.lineno, string] self.generic_visit(node) @@ -74,6 +70,7 @@ def parse_file(filename): try: ast = parse(file.read()) except SyntaxError, e: + print "Unable to parse %s: %s" % (filename, e) return file.close() @@ -96,7 +93,7 @@ def write_file(filename): 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(): @@ -104,11 +101,9 @@ def main(): for root, dirs, files in os.walk(start_dir): for file in files: if file.endswith(u'.py'): - #print u'Parsing "%s"' % file + print u'Parsing "%s"' % file parse_file(os.path.join(root, file)) - #print u'Generating TS file...', - print counts - exit(0) + print u'Generating TS file...', write_file(os.path.join(start_dir, u'i18n', u'openlp_en.ts')) print u'done.' From 30a3897d3dd7541b3b0bfc4243c7995d12cd36a9 Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 27 Nov 2009 13:01:11 +0200 Subject: [PATCH 06/10] Cleanup imports. --- openlp-get-strings.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/openlp-get-strings.py b/openlp-get-strings.py index f070735f9..e9dd26539 100755 --- a/openlp-get-strings.py +++ b/openlp-get-strings.py @@ -24,10 +24,7 @@ ############################################################################### import os -import re -from os import walk from ast import parse, NodeVisitor, Str -from sys import exit ts_file = u""" From bbd6c2ed78f35f88c33ccf8e0b1754a11f7262b0 Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 27 Nov 2009 13:06:39 +0200 Subject: [PATCH 07/10] Count number of strings found. --- openlp-get-strings.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/openlp-get-strings.py b/openlp-get-strings.py index e9dd26539..032fbd02f 100755 --- a/openlp-get-strings.py +++ b/openlp-get-strings.py @@ -43,11 +43,11 @@ ts_message = u""" """ strings = {} +count = 0 class StringExtractor(NodeVisitor): def __init__(self, strings, filename): - self.strings = strings self.filename = filename self.classname = 'unknown' @@ -56,8 +56,10 @@ class StringExtractor(NodeVisitor): self.generic_visit(node) def visit_Call(self, node): + global count if hasattr(node.func, 'attr') and node.func.attr == 'trUtf8' and isinstance(node.args[0], Str): string = node.args[0].s + count += 1 key = '%s-%s' % (self.classname, string) strings[key] = [self.classname, self.filename, node.lineno, string] self.generic_visit(node) @@ -100,6 +102,7 @@ def main(): if file.endswith(u'.py'): print u'Parsing "%s"' % file parse_file(os.path.join(root, file)) + print u'Found %s strings' % count print u'Generating TS file...', write_file(os.path.join(start_dir, u'i18n', u'openlp_en.ts')) print u'done.' From 6cf149b4818134c4452caffffc1dd2b093bf5f45 Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 27 Nov 2009 16:41:13 +0200 Subject: [PATCH 08/10] Global variables are ugly. --- openlp-get-strings.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/openlp-get-strings.py b/openlp-get-strings.py index 032fbd02f..f89391252 100755 --- a/openlp-get-strings.py +++ b/openlp-get-strings.py @@ -42,13 +42,13 @@ ts_message = u""" """ -strings = {} count = 0 class StringExtractor(NodeVisitor): def __init__(self, strings, filename): self.filename = filename + self.strings = strings self.classname = 'unknown' def visit_ClassDef(self, node): @@ -61,10 +61,10 @@ class StringExtractor(NodeVisitor): string = node.args[0].s count += 1 key = '%s-%s' % (self.classname, string) - strings[key] = [self.classname, self.filename, node.lineno, string] + self.strings[key] = [self.classname, self.filename, node.lineno, string] self.generic_visit(node) -def parse_file(filename): +def parse_file(filename, strings): file = open(filename, u'r') try: ast = parse(file.read()) @@ -75,7 +75,7 @@ def parse_file(filename): StringExtractor(strings, filename).visit(ast) -def write_file(filename): +def write_file(filename, strings): translation_file = u'' translation_contexts = [] translation_messages = [] @@ -96,15 +96,16 @@ def write_file(filename): 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'Found %s strings' % count 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__': From 9f992478b76d3400dfb080e229cb6c500bef345f Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 27 Nov 2009 16:41:42 +0200 Subject: [PATCH 09/10] Remove count. --- openlp-get-strings.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/openlp-get-strings.py b/openlp-get-strings.py index f89391252..df6db6369 100755 --- a/openlp-get-strings.py +++ b/openlp-get-strings.py @@ -42,7 +42,6 @@ ts_message = u""" """ -count = 0 class StringExtractor(NodeVisitor): @@ -56,10 +55,8 @@ class StringExtractor(NodeVisitor): self.generic_visit(node) def visit_Call(self, node): - global count if hasattr(node.func, 'attr') and node.func.attr == 'trUtf8' and isinstance(node.args[0], Str): string = node.args[0].s - count += 1 key = '%s-%s' % (self.classname, string) self.strings[key] = [self.classname, self.filename, node.lineno, string] self.generic_visit(node) @@ -103,7 +100,6 @@ def main(): if file.endswith(u'.py'): print u'Parsing "%s"' % file parse_file(os.path.join(root, file), strings) - print u'Found %s strings' % count print u'Generating TS file...', write_file(os.path.join(start_dir, u'i18n', u'openlp_en.ts'), strings) print u'done.' From 1eef20bedcf8908dc0180c2d0ffe33e0f54b7cef Mon Sep 17 00:00:00 2001 From: Michael Gorven Date: Fri, 27 Nov 2009 16:44:02 +0200 Subject: [PATCH 10/10] La, la, la; don't mind me... --- openlp-get-strings.py => scripts/get-strings.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename openlp-get-strings.py => scripts/get-strings.py (100%) diff --git a/openlp-get-strings.py b/scripts/get-strings.py similarity index 100% rename from openlp-get-strings.py rename to scripts/get-strings.py