#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin #
# Thompson, Jon Tibble, Carsten Tinggaard #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
import os
from cgi import escape
from ast import parse, NodeVisitor, Str
ts_file = u"""
%s
"""
ts_context = u""" %s
%s
"""
ts_message = u"""
"""
class StringExtractor(NodeVisitor):
def __init__(self, strings, filename, base_path):
self.base_path = base_path
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[len(self.base_path) + 1:], node.lineno, escape(string)]
self.generic_visit(node)
def parse_file(base_path, filename, strings):
file = open(filename, u'r')
try:
ast = parse(file.read())
except SyntaxError, e:
print "Unable to parse %s: %s" % (filename, e)
return
file.close()
StringExtractor(strings, filename, base_path).visit(ast)
def write_file(filename, strings):
translation_file = u''
translation_contexts = []
translation_messages = []
class_name = strings[strings.keys()[0]][0]
current_context = u''
for key, translation in strings.iteritems():
if class_name != translation[0]:
current_context = ts_context % (class_name, u''.join(translation_messages))
translation_contexts.append(current_context)
translation_messages = []
class_name = translation[0]
translation_messages.append(ts_message % (translation[1], translation[2], translation[3]))
current_context = ts_context % (class_name, u''.join(translation_messages))
translation_contexts.append(current_context)
translation_file = ts_file % (u''.join(translation_contexts))
file = open(filename, u'w')
file.write(translation_file.encode('utf8'))
file.close()
def main():
strings = {}
start_dir = os.path.abspath(u'..')
for root, dirs, files in os.walk(start_dir):
for file in files:
if file.startswith(u'hook-') or file.startswith(u'test_'):
continue
if file.endswith(u'.py'):
print u'Parsing "%s"' % file
parse_file(start_dir, os.path.join(root, file), strings)
print u'Generating TS file...',
write_file(os.path.join(start_dir, u'resources', u'i18n', u'openlp_en.ts'), strings)
print u'done.'
if __name__ == u'__main__':
if os.path.split(os.path.abspath(u'.'))[1] != u'scripts':
print u'You need to run this script from the scripts directory.'
else:
main()