From af52ee68d9e52a42e1fa50cb434ee18719f7257b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sun, 5 Feb 2012 22:58:17 +0200 Subject: [PATCH] Updated the translation_utils.py script to download from Transifex instead of Pootle. --- scripts/translation_utils.py | 85 ++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/scripts/translation_utils.py b/scripts/translation_utils.py index 935ef97e8..707b11fa0 100755 --- a/scripts/translation_utils.py +++ b/scripts/translation_utils.py @@ -51,20 +51,26 @@ This is done easily via the ``-d``, ``-p`` and ``-u`` options:: """ import os -import urllib +import urllib2 import re from shutil import copy +from getpass import getpass +import base64 +import json +import webbrowser from optparse import OptionParser from PyQt4 import QtCore from BeautifulSoup import BeautifulSoup -SERVER_URL = u'http://pootle.projecthq.biz/export/openlp/' +SERVER_URL = u'http://www.transifex.net/api/2/project/openlp/' IGNORED_PATHS = [u'scripts'] IGNORED_FILES = [u'setup.py'] verbose_mode = False quiet_mode = False +username = '' +password = '' class Command(object): """ @@ -172,35 +178,38 @@ def run(command): print_verbose(u'Error(s):\n%s' % process.readAllStandardError()) print_verbose(u'Output:\n%s' % process.readAllStandardOutput()) -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() - def download_translations(): """ This method downloads the translation files from the Pootle server. """ - print_quiet(u'Download translation files from Pootle') - page = urllib.urlopen(SERVER_URL) - soup = BeautifulSoup(page) - languages = soup.findAll(text=re.compile(r'.*\.ts')) - for language_file in languages: - update_export_at_pootle(language_file) - for language_file in languages: + global username, password + if not username: + username = raw_input('Transifex username: ') + if not password: + password = getpass('Transifex password: ') + print_quiet(u'Download translation files from Transifex') + # First get the list of languages + url = SERVER_URL + 'resource/ents/' + base64string = base64.encodestring( + '%s:%s' % (username, password))[:-1] + auth_header = 'Basic %s' % base64string + request = urllib2.Request(url + '?details') + request.add_header('Authorization', auth_header) + print_verbose('Downloading list of languages from: %s' % url) + json_response = urllib2.urlopen(request) + json_dict = json.loads(json_response.read()) + languages = [lang['code'] for lang in json_dict['available_languages']] + for language in languages: + lang_url = url + 'translation/%s/?file' % language + request = urllib2.Request(lang_url) + request.add_header('Authorization', auth_header) filename = os.path.join(os.path.abspath(u'..'), u'resources', u'i18n', - language_file) + language + '.ts') print_verbose(u'Get Translation File: %s' % filename) - urllib.urlretrieve(SERVER_URL + language_file, filename) + response = urllib2.urlopen(request) + fd = open(filename, 'w') + fd.write(response.read()) + fd.close() print_quiet(u' Done.') def prepare_project(): @@ -281,17 +290,11 @@ def create_translation(language): ``language`` The language file to create. """ - print_quiet(u'Create new Translation File') - if not language.endswith(u'.ts'): - language += u'.ts' - filename = os.path.join(os.path.abspath(u'..'), u'resources', u'i18n', language) - urllib.urlretrieve(SERVER_URL + u'en.ts', filename) - 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.') + print_quiet(u'Please request a new language at the OpenLP project on ' + 'Transifex.') + webbrowser.open('https://www.transifex.net/projects/p/openlp/' + 'resource/ents/') + print_quiet(u'Opening browser to OpenLP project...') def process_stack(command_stack): """ @@ -321,12 +324,16 @@ def process_stack(command_stack): print_quiet(u'No commands to process.') def main(): - global verbose_mode, quiet_mode + global verbose_mode, quiet_mode, username, password # Set up command line options. 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.' parser = OptionParser(usage=usage) + parser.add_option('-U', '--username', dest='username', metavar='USERNAME', + help='Transifex username, used for authentication') + parser.add_option('-P', '--password', dest='password', metavar='PASSWORD', + help='Transifex password, used for authentication') parser.add_option('-d', '--download-ts', dest='download', action='store_true', help='download language files from Pootle') parser.add_option('-c', '--create', dest='create', metavar='LANG', @@ -356,6 +363,10 @@ def main(): command_stack.append(Command.Generate) verbose_mode = options.verbose quiet_mode = options.quiet + if options.username: + username = options.username + if options.password: + password = options.password if not command_stack: command_stack.append(Command.Download) command_stack.append(Command.Prepare)