2010-04-30 22:07:51 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2011-02-24 05:47:38 +00:00
|
|
|
# Copyright (c) 2008-2011 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
|
2011-03-24 19:04:02 +00:00
|
|
|
# Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, Armin Köhler, #
|
|
|
|
# Andreas Preikschat, Mattias Põldaru, Christian Richter, Philip Ridout, #
|
2011-05-24 20:47:05 +00:00
|
|
|
# Jeffrey Smith, Maikel Stuivenberg, Martin Thompson, Jon Tibble, Frode #
|
|
|
|
# Woldsund #
|
2010-04-30 22:07:51 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
|
|
|
|
2010-09-02 08:59:21 +00:00
|
|
|
"""
|
|
|
|
This script is used to maintain the translation files in OpenLP. It downloads
|
|
|
|
the latest translation files from the Pootle translation server, updates the
|
|
|
|
local translation files from both the source code and the files from Pootle,
|
|
|
|
and can also generate the compiled translation files.
|
|
|
|
|
|
|
|
Create New Language
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
To create a new language, simply run this script with the ``-a`` command line
|
|
|
|
option::
|
|
|
|
|
|
|
|
@:~$ ./translation_utils.py -a
|
|
|
|
|
|
|
|
Update Translation Files
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
The best way to update the translations is to download the files from Pootle,
|
|
|
|
and then update the local files using both the downloaded files and the source.
|
|
|
|
This is done easily via the ``-d``, ``-p`` and ``-u`` options::
|
|
|
|
|
|
|
|
@:~$ ./translation_utils.py -dpu
|
|
|
|
|
|
|
|
"""
|
2010-04-30 22:07:51 +00:00
|
|
|
import os
|
|
|
|
import urllib
|
2010-06-22 17:16:39 +00:00
|
|
|
import re
|
2010-09-21 19:22:34 +00:00
|
|
|
from shutil import copy
|
2010-06-09 17:09:32 +00:00
|
|
|
|
|
|
|
from optparse import OptionParser
|
2010-04-30 22:07:51 +00:00
|
|
|
from PyQt4 import QtCore
|
2010-06-22 17:16:39 +00:00
|
|
|
from BeautifulSoup import BeautifulSoup
|
2010-04-30 22:07:51 +00:00
|
|
|
|
2010-09-02 08:59:21 +00:00
|
|
|
SERVER_URL = u'http://pootle.projecthq.biz/export/openlp/'
|
|
|
|
IGNORED_PATHS = [u'scripts']
|
|
|
|
IGNORED_FILES = [u'setup.py']
|
|
|
|
|
|
|
|
verbose_mode = False
|
2010-10-10 15:40:54 +00:00
|
|
|
quiet_mode = False
|
2010-09-02 08:59:21 +00:00
|
|
|
|
|
|
|
class Command(object):
|
|
|
|
"""
|
|
|
|
Provide an enumeration of commands.
|
|
|
|
"""
|
|
|
|
Download = 1
|
|
|
|
Create = 2
|
|
|
|
Prepare = 3
|
|
|
|
Update = 4
|
|
|
|
Generate = 5
|
|
|
|
|
|
|
|
class CommandStack(object):
|
|
|
|
"""
|
|
|
|
This class provides an iterable stack.
|
|
|
|
"""
|
2010-06-22 17:16:39 +00:00
|
|
|
def __init__(self):
|
2010-09-02 08:59:21 +00:00
|
|
|
self.current_index = 0
|
|
|
|
self.data = []
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.data)
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
2010-09-21 19:55:40 +00:00
|
|
|
if not index in self.data:
|
|
|
|
return None
|
|
|
|
elif self.data[index].get(u'arguments'):
|
2010-09-02 08:59:21 +00:00
|
|
|
return self.data[index][u'command'], self.data[index][u'arguments']
|
2010-06-22 17:16:39 +00:00
|
|
|
else:
|
2010-09-02 08:59:21 +00:00
|
|
|
return self.data[index][u'command']
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
if self.current_index == len(self.data):
|
|
|
|
raise StopIteration
|
|
|
|
else:
|
|
|
|
current_item = self.data[self.current_index][u'command']
|
|
|
|
self.current_index += 1
|
|
|
|
return current_item
|
|
|
|
|
|
|
|
def append(self, command, **kwargs):
|
|
|
|
data = {u'command': command}
|
|
|
|
if u'arguments' in kwargs:
|
|
|
|
data[u'arguments'] = kwargs[u'arguments']
|
|
|
|
self.data.append(data)
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.current_index = 0
|
|
|
|
|
2010-09-21 19:55:40 +00:00
|
|
|
def arguments(self):
|
|
|
|
if self.data[self.current_index - 1].get(u'arguments'):
|
|
|
|
return self.data[self.current_index - 1][u'arguments']
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
results = []
|
|
|
|
for item in self.data:
|
|
|
|
if item.get(u'arguments'):
|
|
|
|
results.append(str((item[u'command'], item[u'arguments'])))
|
|
|
|
else:
|
|
|
|
results.append(str((item[u'command'], )))
|
|
|
|
return u'[%s]' % u', '.join(results)
|
|
|
|
|
2010-10-10 15:40:54 +00:00
|
|
|
def print_quiet(text, linefeed=True):
|
|
|
|
"""
|
|
|
|
This method checks to see if we are in quiet mode, and if not prints
|
|
|
|
``text`` out.
|
|
|
|
|
|
|
|
``text``
|
|
|
|
The text to print.
|
|
|
|
"""
|
|
|
|
global quiet_mode
|
|
|
|
if not quiet_mode:
|
|
|
|
if linefeed:
|
|
|
|
print text
|
|
|
|
else:
|
|
|
|
print text,
|
2010-09-02 08:59:21 +00:00
|
|
|
|
|
|
|
def print_verbose(text):
|
|
|
|
"""
|
|
|
|
This method checks to see if we are in verbose mode, and if so prints
|
|
|
|
``text`` out.
|
|
|
|
|
|
|
|
``text``
|
|
|
|
The text to print.
|
|
|
|
"""
|
2010-10-10 15:40:54 +00:00
|
|
|
global verbose_mode, quiet_mode
|
|
|
|
if not quiet_mode and verbose_mode:
|
2010-09-02 08:59:21 +00:00
|
|
|
print u' %s' % text
|
|
|
|
|
|
|
|
def run(command):
|
|
|
|
"""
|
|
|
|
This method runs an external application.
|
|
|
|
|
|
|
|
``command``
|
|
|
|
The command to run.
|
|
|
|
"""
|
|
|
|
print_verbose(command)
|
|
|
|
process = QtCore.QProcess()
|
|
|
|
process.start(command)
|
|
|
|
while (process.waitForReadyRead()):
|
|
|
|
print_verbose(u'ReadyRead: %s' % QtCore.QString(process.readAll()))
|
|
|
|
print_verbose(u'Error(s):\n%s' % process.readAllStandardError())
|
|
|
|
print_verbose(u'Output:\n%s' % process.readAllStandardOutput())
|
|
|
|
|
2010-09-22 18:03:10 +00:00
|
|
|
def update_export_at_pootle(source_filename):
|
|
|
|
"""
|
|
|
|
This is needed because of database and exported *.ts file can be out of sync
|
|
|
|
|
|
|
|
``source_filename``
|
|
|
|
The file to sync.
|
|
|
|
|
|
|
|
"""
|
|
|
|
language = source_filename[:-3]
|
|
|
|
REVIEW_URL = u'http://pootle.projecthq.biz/%s/openlp/review.html' % language
|
|
|
|
print_verbose(u'Accessing: %s' % (REVIEW_URL))
|
|
|
|
page = urllib.urlopen(REVIEW_URL)
|
|
|
|
page.close()
|
2010-10-10 15:40:54 +00:00
|
|
|
|
2010-09-22 18:03:10 +00:00
|
|
|
|
2010-09-02 08:59:21 +00:00
|
|
|
def download_file(source_filename, dest_filename):
|
|
|
|
"""
|
|
|
|
Download a file and save it to disk.
|
|
|
|
|
|
|
|
``source_filename``
|
|
|
|
The file to download.
|
|
|
|
|
|
|
|
``dest_filename``
|
|
|
|
The new local file name.
|
|
|
|
"""
|
|
|
|
print_verbose(u'Downloading from: %s' % (SERVER_URL + source_filename))
|
|
|
|
page = urllib.urlopen(SERVER_URL + source_filename)
|
|
|
|
content = page.read().decode('utf8')
|
|
|
|
page.close()
|
|
|
|
file = open(dest_filename, u'w')
|
|
|
|
file.write(content.encode('utf8'))
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
def download_translations():
|
|
|
|
"""
|
|
|
|
This method downloads the translation files from the Pootle server.
|
|
|
|
"""
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'Download translation files from Pootle')
|
2010-09-02 08:59:21 +00:00
|
|
|
page = urllib.urlopen(SERVER_URL)
|
|
|
|
soup = BeautifulSoup(page)
|
|
|
|
languages = soup.findAll(text=re.compile(r'.*\.ts'))
|
2010-09-22 18:03:10 +00:00
|
|
|
for language_file in languages:
|
|
|
|
update_export_at_pootle(language_file)
|
|
|
|
for language_file in languages:
|
2010-09-02 08:59:21 +00:00
|
|
|
filename = os.path.join(os.path.abspath(u'..'), u'resources', u'i18n',
|
2010-09-22 18:03:10 +00:00
|
|
|
language_file)
|
2010-09-02 08:59:21 +00:00
|
|
|
print_verbose(u'Get Translation File: %s' % filename)
|
2010-09-22 18:03:10 +00:00
|
|
|
download_file(language_file, filename)
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u' Done.')
|
2010-09-02 08:59:21 +00:00
|
|
|
|
|
|
|
def prepare_project():
|
|
|
|
"""
|
|
|
|
This method creates the project file needed to update the translation files
|
|
|
|
and compile them into .qm files.
|
|
|
|
"""
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'Generating the openlp.pro file')
|
2010-09-02 08:59:21 +00:00
|
|
|
lines = []
|
|
|
|
start_dir = os.path.abspath(u'..')
|
|
|
|
start_dir = start_dir + os.sep
|
|
|
|
print_verbose(u'Starting directory: %s' % start_dir)
|
|
|
|
for root, dirs, files in os.walk(start_dir):
|
|
|
|
for file in files:
|
|
|
|
path = root.replace(start_dir, u'').replace(u'\\', u'/') #.replace(u'..', u'.')
|
|
|
|
if file.startswith(u'hook-') or file.startswith(u'test_'):
|
|
|
|
continue
|
|
|
|
ignore = False
|
|
|
|
for ignored_path in IGNORED_PATHS:
|
|
|
|
if path.startswith(ignored_path):
|
|
|
|
ignore = True
|
|
|
|
break
|
|
|
|
if ignore:
|
|
|
|
continue
|
|
|
|
ignore = False
|
|
|
|
for ignored_file in IGNORED_FILES:
|
|
|
|
if file == ignored_file:
|
|
|
|
ignore = True
|
|
|
|
break
|
|
|
|
if ignore:
|
|
|
|
continue
|
|
|
|
if file.endswith(u'.py') or file.endswith(u'.pyw'):
|
|
|
|
if path:
|
2010-06-22 17:16:39 +00:00
|
|
|
line = u'%s/%s' % (path, file)
|
2010-09-02 08:59:21 +00:00
|
|
|
else:
|
|
|
|
line = file
|
|
|
|
print_verbose(u'Parsing "%s"' % line)
|
|
|
|
lines.append(u'SOURCES += %s' % line)
|
|
|
|
elif file.endswith(u'.ts'):
|
|
|
|
line = u'%s/%s' % (path, file)
|
|
|
|
print_verbose(u'Parsing "%s"' % line)
|
|
|
|
lines.append(u'TRANSLATIONS += %s' % line)
|
|
|
|
lines.sort()
|
|
|
|
file = open(os.path.join(start_dir, u'openlp.pro'), u'w')
|
|
|
|
file.write(u'\n'.join(lines).encode('utf8'))
|
|
|
|
file.close()
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u' Done.')
|
2010-09-02 08:59:21 +00:00
|
|
|
|
|
|
|
def update_translations():
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'Update the translation files')
|
2010-09-02 08:59:21 +00:00
|
|
|
if not os.path.exists(os.path.join(os.path.abspath(u'..'), u'openlp.pro')):
|
|
|
|
print u'You have no generated a project file yet, please run this ' + \
|
|
|
|
u'script with the -p option.'
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
os.chdir(os.path.abspath(u'..'))
|
|
|
|
run(u'pylupdate4 -verbose -noobsolete openlp.pro')
|
2010-09-21 19:22:34 +00:00
|
|
|
os.chdir(os.path.abspath(u'scripts'))
|
2010-09-02 08:59:21 +00:00
|
|
|
|
|
|
|
def generate_binaries():
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'Generate the related *.qm files')
|
2010-09-02 08:59:21 +00:00
|
|
|
if not os.path.exists(os.path.join(os.path.abspath(u'..'), u'openlp.pro')):
|
2010-09-21 19:55:40 +00:00
|
|
|
print u'You have not generated a project file yet, please run this ' + \
|
2010-09-02 08:59:21 +00:00
|
|
|
u'script with the -p option. It is also recommended that you ' + \
|
|
|
|
u'this script with the -u option to update the translation ' + \
|
|
|
|
u'files as well.'
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
os.chdir(os.path.abspath(u'..'))
|
|
|
|
run(u'lrelease openlp.pro')
|
2010-09-21 19:22:34 +00:00
|
|
|
os.chdir(os.path.abspath(u'scripts'))
|
|
|
|
src_path = os.path.join(os.path.abspath(u'..'), u'resources', u'i18n')
|
2011-05-24 17:42:30 +00:00
|
|
|
dest_path = os.path.join(os.path.abspath(u'..'), u'resources', u'i18n')
|
2010-09-21 19:22:34 +00:00
|
|
|
if not os.path.exists(dest_path):
|
|
|
|
os.makedirs(dest_path)
|
|
|
|
src_list = os.listdir(src_path)
|
|
|
|
for file in src_list:
|
|
|
|
if re.search('.qm$', file):
|
2010-10-10 15:40:54 +00:00
|
|
|
copy(os.path.join(src_path, u'%s' % file),
|
2010-09-21 19:22:34 +00:00
|
|
|
os.path.join(dest_path, u'%s' % file))
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u' Done.')
|
2010-09-21 19:22:34 +00:00
|
|
|
|
2010-09-02 08:59:21 +00:00
|
|
|
|
|
|
|
def create_translation(language):
|
|
|
|
"""
|
|
|
|
This method creates a new translation file.
|
|
|
|
|
|
|
|
``language``
|
|
|
|
The language file to create.
|
|
|
|
"""
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'Create new Translation File')
|
2010-09-21 19:55:40 +00:00
|
|
|
if not language.endswith(u'.ts'):
|
|
|
|
language += u'.ts'
|
2010-09-02 08:59:21 +00:00
|
|
|
filename = os.path.join(os.path.abspath(u'..'), u'resources', u'i18n', language)
|
|
|
|
download_file(u'en.ts', filename)
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u' ** Please Note **')
|
|
|
|
print_quiet(u' In order to get this file into OpenLP and onto the '
|
|
|
|
u'Pootle translation server you will need to subscribe to the '
|
|
|
|
u'OpenLP Translators mailing list, and request that your language '
|
|
|
|
u'file be added to the project.')
|
|
|
|
print_quiet(u' Done.')
|
2010-09-02 08:59:21 +00:00
|
|
|
|
|
|
|
def process_stack(command_stack):
|
|
|
|
"""
|
|
|
|
This method looks at the commands in the command stack, and processes them
|
|
|
|
in the order they are in the stack.
|
|
|
|
|
|
|
|
``command_stack``
|
|
|
|
The command stack to process.
|
|
|
|
"""
|
|
|
|
if command_stack:
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'Processing %d commands...' % len(command_stack))
|
2010-09-02 08:59:21 +00:00
|
|
|
for command in command_stack:
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'%d.' % (command_stack.current_index), False)
|
2010-09-02 08:59:21 +00:00
|
|
|
if command == Command.Download:
|
|
|
|
download_translations()
|
|
|
|
elif command == Command.Prepare:
|
|
|
|
prepare_project()
|
|
|
|
elif command == Command.Update:
|
|
|
|
update_translations()
|
|
|
|
elif command == Command.Generate:
|
|
|
|
generate_binaries()
|
|
|
|
elif command == Command.Create:
|
2010-09-21 19:55:40 +00:00
|
|
|
arguments = command_stack.arguments()
|
2010-09-02 08:59:21 +00:00
|
|
|
create_translation(*arguments)
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'Finished processing commands.')
|
2010-09-02 08:59:21 +00:00
|
|
|
else:
|
2010-10-10 15:40:54 +00:00
|
|
|
print_quiet(u'No commands to process.')
|
2010-04-30 22:07:51 +00:00
|
|
|
|
|
|
|
def main():
|
2010-10-10 15:40:54 +00:00
|
|
|
global verbose_mode, quiet_mode
|
2010-04-30 22:07:51 +00:00
|
|
|
# Set up command line options.
|
2010-09-02 08:59:21 +00:00
|
|
|
usage = u'%prog [options]\nOptions are parsed in the order they are ' + \
|
|
|
|
u'listed below. If no options are given, "-dpug" will be used.\n\n' + \
|
|
|
|
u'This script is used to manage OpenLP\'s translation files.'
|
2010-04-30 22:07:51 +00:00
|
|
|
parser = OptionParser(usage=usage)
|
2010-09-02 08:59:21 +00:00
|
|
|
parser.add_option('-d', '--download-ts', dest='download',
|
|
|
|
action='store_true', help='download language files from Pootle')
|
2010-09-21 19:55:40 +00:00
|
|
|
parser.add_option('-c', '--create', dest='create', metavar='LANG',
|
2010-09-02 08:59:21 +00:00
|
|
|
help='create a new translation file for language LANG, e.g. "en_GB"')
|
|
|
|
parser.add_option('-p', '--prepare', dest='prepare', action='store_true',
|
|
|
|
help='generate a project file, used to update the translations')
|
2010-06-22 17:16:39 +00:00
|
|
|
parser.add_option('-u', '--update', action='store_true', dest='update',
|
2010-09-02 08:59:21 +00:00
|
|
|
help='update translation files (needs a project file)')
|
|
|
|
parser.add_option('-g', '--generate', dest='generate', action='store_true',
|
|
|
|
help='compile .ts files into .qm files')
|
|
|
|
parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
|
|
|
|
help='show extra information while processing translations')
|
2010-10-10 15:40:54 +00:00
|
|
|
parser.add_option('-q', '--quiet', dest='quiet', action='store_true',
|
|
|
|
help='suppress all output other than errors')
|
2010-04-30 22:07:51 +00:00
|
|
|
(options, args) = parser.parse_args()
|
2010-09-02 08:59:21 +00:00
|
|
|
# Create and populate the command stack
|
|
|
|
command_stack = CommandStack()
|
2010-04-30 22:07:51 +00:00
|
|
|
if options.download:
|
2010-09-02 08:59:21 +00:00
|
|
|
command_stack.append(Command.Download)
|
2010-06-22 17:16:39 +00:00
|
|
|
if options.create:
|
2010-09-02 08:59:21 +00:00
|
|
|
command_stack.append(Command.Create, arguments=[options.create])
|
2010-06-22 17:16:39 +00:00
|
|
|
if options.prepare:
|
2010-09-02 08:59:21 +00:00
|
|
|
command_stack.append(Command.Prepare)
|
2010-06-22 17:16:39 +00:00
|
|
|
if options.update:
|
2010-09-02 08:59:21 +00:00
|
|
|
command_stack.append(Command.Update)
|
2010-06-22 17:16:39 +00:00
|
|
|
if options.generate:
|
2010-09-02 08:59:21 +00:00
|
|
|
command_stack.append(Command.Generate)
|
|
|
|
verbose_mode = options.verbose
|
2010-10-10 15:40:54 +00:00
|
|
|
quiet_mode = options.quiet
|
2010-09-02 08:59:21 +00:00
|
|
|
if not command_stack:
|
|
|
|
command_stack.append(Command.Download)
|
|
|
|
command_stack.append(Command.Prepare)
|
|
|
|
command_stack.append(Command.Update)
|
|
|
|
command_stack.append(Command.Generate)
|
|
|
|
# Process the commands
|
|
|
|
process_stack(command_stack)
|
2010-04-30 22:07:51 +00:00
|
|
|
|
|
|
|
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:
|
2010-07-27 09:32:52 +00:00
|
|
|
main()
|