emailuis/emailui/sis/pkg-localizer.py
branchRCL_3
changeset 25 3533d4323edc
equal deleted inserted replaced
24:d189ee25cf9d 25:3533d4323edc
       
     1 # -*- coding: ISO-8859-1 -*-
       
     2 """
       
     3 /*
       
     4 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     5 * All rights reserved.
       
     6 * This component and the accompanying materials are made available
       
     7 * under the terms of "Eclipse Public License v1.0"
       
     8 * which accompanies this distribution, and is available
       
     9 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    10 *
       
    11 * Initial Contributors:
       
    12 * Nokia Corporation - initial contribution.
       
    13 *
       
    14 * Contributors:
       
    15 *
       
    16 * Description: Python script that can be used to make localized pkg-files 
       
    17 * based on Engineering English pkg-files. Meant only for internal use in Email.
       
    18 *
       
    19 */
       
    20 """
       
    21 
       
    22 import sys, string
       
    23 
       
    24 LINE_FEED = "\n"
       
    25 INDENTATION = "    "
       
    26 
       
    27 # Added to converted file name, so e.g. "filename.pkg" -> filename_loc.pkg"
       
    28 CONVERTED_FILE_NAME_ADDITION = "_loc"
       
    29 # Path identifiers for the paths that include files to be localized
       
    30 PATHS_TO_LOCALIZE = [ "resource\\" ]
       
    31 
       
    32 # Exclude paths of ECom plugin registeration files and extended startup list ctrl files
       
    33 PATHS_TO_EXCLUDE = [ "resource\\plugins\\", "private\\101f875a\\", "resource\\messaging\\", "resource\\fsmailbrandmanager.r",
       
    34                      "\\esmragnversit2strings.rsc", "\\esmrurlparserplugindata.rsc", "\\esmrpolicies.rsc", "\\esmralarminfo.rsc", # Non-localized resource files from mrui
       
    35                      "\\neascpnodenames.rsc", "\\neasicalstrings.rsc", "\\neasfreestylepluginresource.rsc" ] # Non-localized resource files from eas
       
    36 
       
    37 # File extension identifiers for the files to be localized
       
    38 EXTENSIONS_TO_LOCALIZE = [ ".rsc" ]
       
    39 # Embedded sisx packages to be localized
       
    40 EMBEDDED_PACKAGES_TO_LOCALIZE = [ "eas_engine.sisx", "eas_engine_udeb.sisx" ]
       
    41 EMBEDDED_PACKAGE_LINE_IDENTIFIER = "@"
       
    42 EMBEDDED_PACKAGE_FILE_EXTENSION = ".sisx"
       
    43 
       
    44 # List of language codes
       
    45 # LANGUAGE_CODES = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 25, 26, 27, 28, 29, 30, 31, 32, 33, 37, 39, 42, 44, 45, 49, 50, 51, 54, 57, 58, 59, 67, 68, 70, 76, 78, 79, 83, 93, 94, 129, 157, 158, 159, 160, 161, 326, 327, 401, 402 ]
       
    46 LANGUAGE_CODES = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 25, 26, 27, 28, 29, 30, 31, 32, 33, 37, 39, 42, 44, 45, 49, 50, 51, 54, 57, 59, 67, 68, 70, 76, 78, 79, 83, 93, 94, 96, 102, 103, 129, 157, 158, 159, 160, 161, 326, 327 ]
       
    47 
       
    48 # Comment line added with commented-out engineering english resource file
       
    49 PKG_FILE_COMMENT_LINE_START_TAG = ";"
       
    50 LOCALIZATION_COMMENT_LINE = PKG_FILE_COMMENT_LINE_START_TAG + " Following line is localized, so this Engineering English line is commented out\n"
       
    51 
       
    52 LOCALIZATION_LANG_CODE_IDENTIFIER = "***"
       
    53 LOCALIZATION_LANG_CODE_BLOCK_START_LINE = 'IF EXISTS( "z:\\resource\\avkon.r' + LOCALIZATION_LANG_CODE_IDENTIFIER + '" )' + LINE_FEED
       
    54 LOCALIZATION_LANG_CODE_BLOCK_END_LINE = 'ENDIF' + LINE_FEED + LINE_FEED
       
    55 
       
    56 # Identifier to identify the line of the starter app, which needs to be the last item in pkg file
       
    57 STARTER_APP_IDENTIFIER = "sys\\bin\\emailservermonitor.exe"
       
    58 
       
    59 HELP_TEXT0 = "Python script that can be used to generate localized pkg-file based on engineering English pkg-file.\nIt converts e.g. all *.rsc file names in input file to *.r01, *.r02, *.r03 etc. file names.\n"
       
    60 HELP_TEXT1 = "Usage:\n    "
       
    61 HELP_TEXT2 = " engineering_english_to_convert.pkg\n"
       
    62 HELP_TEXT3 = "Output file name format:\n    engineering_english_to_convert"
       
    63 HELP_TEXT4 = ".pkg\n"
       
    64 
       
    65 ##########################################################################
       
    66 # MAIN
       
    67 #
       
    68 def main():
       
    69 	if( len(sys.argv) == 1 ):
       
    70 		ind = sys.argv[0].rfind( "\\" )
       
    71 		if( ind >= 0 and len(sys.argv[0]) >= ind+2 ):
       
    72 			script_file = sys.argv[0][ind+1:]
       
    73 		else:
       
    74 			script_file = sys.argv[0]
       
    75 		
       
    76 		print HELP_TEXT0
       
    77 		print HELP_TEXT1 + script_file + HELP_TEXT2
       
    78 		print HELP_TEXT3 + CONVERTED_FILE_NAME_ADDITION + HELP_TEXT4
       
    79 		return
       
    80 		
       
    81 	files = sys.argv[1:]
       
    82 	for file_name in files:
       
    83 		ConvertFile( file_name )
       
    84 	
       
    85 	return
       
    86 
       
    87 
       
    88 def ConstructLocalizedLines( lines_to_localize, converted_lines ):
       
    89 	for lang_code in LANGUAGE_CODES:
       
    90 		lang_code = str( lang_code )
       
    91 		lang_code = lang_code.zfill(2)
       
    92 
       
    93 		start_line = LOCALIZATION_LANG_CODE_BLOCK_START_LINE
       
    94 		converted_lines.append( start_line.replace( LOCALIZATION_LANG_CODE_IDENTIFIER, lang_code ) )
       
    95 
       
    96 		for line in lines_to_localize:
       
    97 			extension = line[1]
       
    98 			line = line[0]
       
    99 			converted_lines.append( INDENTATION + line.replace( extension, extension[0:2] + lang_code ) )
       
   100 
       
   101 		converted_lines.append( LOCALIZATION_LANG_CODE_BLOCK_END_LINE )
       
   102 
       
   103 	return
       
   104 
       
   105 	
       
   106 def DivideConvertedLines( converted ):
       
   107 	footer = []
       
   108 	
       
   109 	starter_app_found = False
       
   110 	i = len( converted ) - 1
       
   111 	while( i >= 0 ):
       
   112 		line = converted[i]
       
   113 		# Append all files below STARTER_APP_IDENTIFIER to footer, including
       
   114 		# also the line where STARTER_APP_IDENTIFIER is found
       
   115 		if not( starter_app_found ):
       
   116 			footer.insert( 0, line )
       
   117 			if( line.find( STARTER_APP_IDENTIFIER ) >= 0 ):
       
   118 				starter_app_found = True
       
   119 
       
   120 		# Append also all the pure comment lines before the starter app line,
       
   121 		# because they are most probably related to starter app. When we
       
   122 		# found first non comment line, we can quit
       
   123 		else:
       
   124 			if( (len(line) > 0) and (line[0] == PKG_FILE_COMMENT_LINE_START_TAG) ):
       
   125 				footer.insert( 0, line )
       
   126 			else:
       
   127 				# First non comment line found, quit the while loop
       
   128 				break
       
   129 
       
   130 		i = i - 1
       
   131 		
       
   132 	# If starter app is not found, then there's no need to divide anything,
       
   133 	# so keep the original converted list untouched and return empty footer
       
   134 	if not( starter_app_found ):
       
   135 		return []
       
   136 
       
   137 	# If starter app is found, remove the footer lines from the original
       
   138 	# converted list and then return the footer
       
   139 	del converted[i+1:]
       
   140 	return footer
       
   141 
       
   142 
       
   143 def ConvertFile( file_name ):
       
   144 	data = ReadFileToList( file_name )
       
   145 	
       
   146 	converted = []
       
   147 	lines_to_localize = []
       
   148 	for line in data:
       
   149 		line_localized = False
       
   150 		for path in PATHS_TO_LOCALIZE:
       
   151 			# Check does this line contain path to be localized
       
   152 			if( (line.find( path ) >= 0) and not(LineIncludesExcludedPath( line.lower() ))  ):
       
   153 				for extension in EXTENSIONS_TO_LOCALIZE:
       
   154 					if( line.lower().find( extension ) >= 0 ):
       
   155 						lines_to_localize.append( [line.lower(), extension] )
       
   156 						converted.append( LOCALIZATION_COMMENT_LINE )
       
   157 						converted.append( PKG_FILE_COMMENT_LINE_START_TAG + line )
       
   158 						line_localized = True
       
   159 
       
   160 				break # for path in PATHS_TO_LOCALIZE
       
   161 				
       
   162 		if not( line_localized ):
       
   163 			# Check does this line include some embedded sis-package that is localized
       
   164 			if( line.find( EMBEDDED_PACKAGE_LINE_IDENTIFIER) >= 0 ):
       
   165 				for embedded_pkg in EMBEDDED_PACKAGES_TO_LOCALIZE:
       
   166 					if( line.lower().find( embedded_pkg.lower() ) >= 0 ):
       
   167 						# This is embedded sis-package that is localized
       
   168 						# -> Change the name to *_loc.sisx
       
   169 						extension_pos = line.lower().find( EMBEDDED_PACKAGE_FILE_EXTENSION.lower() )
       
   170 						converted.append( line[:extension_pos] + CONVERTED_FILE_NAME_ADDITION + line[extension_pos:] )
       
   171 						line_localized = True
       
   172 
       
   173 		if not( line_localized ):
       
   174 			converted.append( line )
       
   175 	
       
   176 	converted_footer = DivideConvertedLines( converted )
       
   177 
       
   178 	# Add extra line feed just in case as it seems to be needed with some files.
       
   179 	# Not needed if adding localised list to the middle of the file.
       
   180 	if( len(converted_footer) == 0 ):
       
   181 		converted.append( LINE_FEED )
       
   182 	
       
   183 	if( len( lines_to_localize ) > 0 ):
       
   184 		ConstructLocalizedLines( lines_to_localize, converted )
       
   185 	
       
   186 	WriteListToFile( ConvertedFileName(file_name), converted, converted_footer )
       
   187 	
       
   188 	return
       
   189 
       
   190 
       
   191 def LineIncludesExcludedPath( line ):
       
   192 	for path in PATHS_TO_EXCLUDE:
       
   193 		if( line.find( path ) >= 0 ):
       
   194 			return True
       
   195 	
       
   196 	return False
       
   197 	
       
   198 
       
   199 def ConvertedFileName( file_name ):
       
   200 	ind = file_name.rfind( "." )
       
   201 	if( ind < 0 ):
       
   202 		converted_file_name = file_name + CONVERTED_FILE_NAME_ADDITION
       
   203 	else:
       
   204 		converted_file_name = file_name[:ind] + CONVERTED_FILE_NAME_ADDITION + file_name[ind:]
       
   205 	
       
   206 	return converted_file_name
       
   207 	
       
   208 	
       
   209 def WriteListToFile( file_name, data1, data2 ):
       
   210 	file_handle = open( file_name, 'w' )
       
   211 	for line in data1:
       
   212 		file_handle.write( line )
       
   213 
       
   214 	for line in data2:
       
   215 		file_handle.write( line )
       
   216 	
       
   217 	file_handle.close()
       
   218 	
       
   219 	return
       
   220 
       
   221 
       
   222 def ReadFileToList( file_name ):
       
   223 	file_handle = open( file_name, 'r' )
       
   224 	data = [ ]
       
   225 	for line in file_handle.readlines():
       
   226 		data.append( line )
       
   227 	
       
   228 	file_handle.close()
       
   229 	
       
   230 	return data
       
   231 
       
   232 
       
   233 ##########################################################################
       
   234 # The End Complete
       
   235 #
       
   236 if __name__ == '__main__':
       
   237     main()