sync_compiler_translation.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #   Aseba - an event-based framework for distributed robot control
00004 #   Copyright (C) 2007--2011:
00005 #           Stephane Magnenat <stephane at magnenat dot net>
00006 #           (http://stephane.magnenat.net)
00007 #           and other contributors, see authors.txt for details
00008 #
00009 #   This program is free software: you can redistribute it and/or modify
00010 #   it under the terms of the GNU Lesser General Public License as published
00011 #   by the Free Software Foundation, version 3 of the License.
00012 #
00013 #   This program is distributed in the hope that it will be useful,
00014 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 #   GNU Lesser General Public License for more details.
00017 #
00018 #   You should have received a copy of the GNU Lesser General Public License
00019 #   along with this program. If not, see <http://www.gnu.org/licenses/>.
00020 
00021 # System lib
00022 import os
00023 import os.path
00024 import sys
00025 from string import Template
00026 import re
00027 import subprocess
00028 
00029 INPUT_DIRECTORY = '../../compiler'
00030 INPUT_FILE = 'errors.cpp'
00031 
00032 OUTPUT_CODE_FILE = 'errors_code.h'
00033 OUTPUT_CODE_DIRECTORY = INPUT_DIRECTORY
00034 
00035 OUTPUT_QT_FILE = 'CompilerTranslator.cpp'
00036 OUTPUT_QT_DIRECTORY = '../../studio/translations'
00037 
00038 OUTPUT_TRANSLATION_DIRECTORY = '../../studio'
00039 translation_files = list()
00040 translation_files.append(['french', 'fr', 'compiler_fr.ts'])
00041 translation_files.append(['german', 'de', 'compiler_de.ts'])
00042 translation_files.append(['spanish', 'es', 'compiler_es.ts'])
00043 translation_files.append(['italian', 'it', 'compiler_it.ts'])
00044 
00045 comment_regexp = re.compile(r'\A\s*// (.*)')
00046 error_regexp = re.compile(r'error_map\[(.*?)\]\s*=\s*L"(.*?)";')
00047 
00048 output_code_file = \
00049 """
00050 #ifndef __ERRORS_H
00051 #define __ERRORS_H
00052 
00053 /*
00054 This file was automatically generated. Do not modify it,
00055 or your changes will be lost!
00056 You can manually run the script 'sync_translation.py'
00057 to generate a new version, based on errors.cpp
00058 */
00059 
00060 // define error codes
00061 namespace Aseba
00062 {
00063         enum ErrorCode
00064         {
00065                 // ${start_comment}
00066                 ${id_0} = 0,
00067 ${id_others}
00068                 ERROR_END
00069         };
00070 };
00071 
00072 #endif // __ERRORS_H
00073 """
00074 output_code_file_template = Template(output_code_file)
00075 
00076 output_qt_file = \
00077 """
00078 #include "CompilerTranslator.h"
00079 #include "../../compiler/errors_code.h"
00080 
00081 #include <CompilerTranslator.moc>
00082 
00083 namespace Aseba
00084 {
00085         CompilerTranslator::CompilerTranslator()
00086         {
00087 
00088         }
00089 
00090         const std::wstring CompilerTranslator::translate(ErrorCode error)
00091         {
00092                 QString msg;
00093 
00094                 switch (error)
00095                 {
00096 ${elements}
00097                         default:
00098                                 msg = tr("Unknown error");
00099                 }
00100 
00101                 return msg.toStdWString();
00102         }
00103 };
00104 """
00105 output_qt_file_template = Template(output_qt_file)
00106 
00107 output_qt_element = \
00108 """
00109                         case ${error_id}:
00110                                 msg = tr("${error_msg}");
00111                                 break;
00112 """
00113 output_qt_element_template = Template(output_qt_element)
00114 
00115 # process input file
00116 filename = os.path.join(INPUT_DIRECTORY, INPUT_FILE)
00117 print "Reading " + filename
00118 try:
00119     fh = file(filename)
00120 except:
00121     print >> sys.stderr, "Invalid file " + filename
00122     exit(1)
00123 
00124 case_vector = ""
00125 count = 0
00126 id_0 = None
00127 start_comment = ""
00128 id_others = ""
00129 
00130 print "Processing..."
00131 for line in fh:
00132     # a comment?
00133     match = comment_regexp.search(line)
00134     if match:
00135         # add it to the error codes file
00136         if not id_0:
00137             start_comment = match.group(1)
00138         else:
00139             id_others += "              // " + match.group(1) + "\n"
00140         continue
00141 
00142     match = error_regexp.search(line)
00143     if match:
00144         count = count + 1
00145         code = match.group(1)
00146         msg = match.group(2)
00147         # error codes file
00148         if not id_0:
00149             id_0 = code
00150         else:
00151             id_others += "              " + code + ",\n"
00152         # qt file
00153         case_vector += output_qt_element_template.substitute(error_id=code, error_msg=msg)
00154 
00155 fh.close()
00156 print "Found " + str(count) + " string(s)"
00157 
00158 # writing errors_code.h
00159 result = output_code_file_template.substitute(start_comment=start_comment, id_0=id_0, id_others=id_others)
00160 filename = os.path.join(OUTPUT_CODE_DIRECTORY, OUTPUT_CODE_FILE)
00161 print "Writing to " + filename
00162 try:
00163     fh = file(filename, 'w')
00164 except:
00165     print >> sys.stderr, "Invalid file " + filename
00166     exit(1)
00167 
00168 fh.write(result)
00169 fh.close()
00170 
00171 # writing the qt file
00172 result = output_qt_file_template.substitute(elements=case_vector)
00173 filename = os.path.join(OUTPUT_QT_DIRECTORY, OUTPUT_QT_FILE)
00174 print "Writing to " + filename
00175 try:
00176     fh = file(filename, 'w')
00177 except:
00178     print >> sys.stderr, "Invalid file " + filename
00179     exit(1)
00180 
00181 fh.write(result)
00182 fh.close()
00183 print "Done!"
00184 
00185 # update translation files
00186 for translation in translation_files:
00187     translation_name = translation[0]
00188     translation_code = translation[1]
00189     translation_file = translation[2]
00190     answer = raw_input("Do you want to update " + translation_name + " [" + translation_code + "] translation file (" + translation_file + ")? [y/n] ")
00191     if answer == 'y' or answer == 'Y':
00192         print "Updating " + translation_file + "..."
00193         file1 = os.path.join(OUTPUT_QT_DIRECTORY, OUTPUT_QT_FILE)
00194         file2 = os.path.join(OUTPUT_TRANSLATION_DIRECTORY, translation_file)
00195         cmd = "lupdate-qt4 " + file1 + " -ts " + file2
00196         print "> " + cmd
00197         subprocess.call(cmd, shell=True)
00198     else:
00199         print "Skipping..."


aseba
Author(s): Stéphane Magnenat
autogenerated on Thu Jan 2 2014 11:17:17