X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FAutoGen%2FUniClassObject.py;h=58f865ed8bd4ce99e8e049e5789bcf3018741343;hp=1eb65c1e9a4bd6bf9db105c2ad9411ab19cac233;hb=97fa0ee9b1cffbb4b97ee35365afa7afcf50e174;hpb=08dd311f5dc735c595d39faf2e6f7e2810bb79a9 diff --git a/BaseTools/Source/Python/AutoGen/UniClassObject.py b/BaseTools/Source/Python/AutoGen/UniClassObject.py index 1eb65c1e9a..58f865ed8b 100644 --- a/BaseTools/Source/Python/AutoGen/UniClassObject.py +++ b/BaseTools/Source/Python/AutoGen/UniClassObject.py @@ -1,4 +1,7 @@ -# Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.
+## @file +# This file is used to collect all defined strings in multiple uni files +# +# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at @@ -7,20 +10,16 @@ # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -# -#This file is used to collect all defined strings in multiple uni files -# - ## # Import Modules # -import os, codecs, re +import Common.LongFilePathOs as os, codecs, re import distutils.util import Common.EdkLogger as EdkLogger from Common.BuildToolError import * from Common.String import GetLineNo from Common.Misc import PathClass - +from Common.LongFilePathSupport import LongFilePath ## # Static definitions # @@ -38,6 +37,9 @@ LF = u'\u000A' NULL = u'\u0000' TAB = u'\t' BACK_SPLASH = u'\\' +DOBULE_QUOTED_SPLASH = u'\\"' +SIGLE_QUOTED_SPLASH = u"\\'" +TAB_BACK_SLASH = u"\\/" gIncludePattern = re.compile("^#include +[\"<]+([^\"< >]+)[>\"]+$", re.MULTILINE | re.UNICODE) @@ -121,7 +123,7 @@ def GetLanguageCode(LangName, IsCompatibleMode, File): if length == 3 and LangName.isalpha(): TempLangName = LangConvTable.get(LangName.lower()) if TempLangName != None: - return TempLangName + return TempLangName return LangName else: EdkLogger.error("Unicode File Parser", FORMAT_INVALID, "Invalid ISO 639-2 language code : %s" % LangName, File) @@ -193,6 +195,8 @@ class UniFileClassObject(object): self.Token = 2 self.LanguageDef = [] #[ [u'LanguageIdentifier', u'PrintableName'], ... ] self.OrderedStringList = {} #{ u'LanguageIdentifier' : [StringDefClassObject] } + self.OrderedStringDict = {} #{ u'LanguageIdentifier' : {StringName:(IndexInList)} } + self.OrderedStringListByToken = {} #{ u'LanguageIdentifier' : {Token: StringDefClassObject} } self.IsCompatibleMode = IsCompatibleMode self.IncludePathList = IncludePathList if len(self.FileList) > 0: @@ -205,7 +209,7 @@ class UniFileClassObject(object): Lang = distutils.util.split_quoted((Line.split(u"//")[0])) if len(Lang) != 3: try: - FileIn = codecs.open(File.Path, mode='rb', encoding='utf-16').read() + FileIn = codecs.open(LongFilePath(File.Path), mode='rb', encoding='utf-16').read() except UnicodeError, X: EdkLogger.error("build", FILE_READ_FAILURE, "File read failure: %s" % str(X), ExtraData=File); except: @@ -246,20 +250,19 @@ class UniFileClassObject(object): else: OtherLang = FirstLangName self.OrderedStringList[LangName].append (StringDefClassObject(Item.StringName, '', Item.Referenced, Item.Token, OtherLang)) - + self.OrderedStringDict[LangName][Item.StringName] = len(self.OrderedStringList[LangName]) - 1 return True # # Get String name and value # def GetStringObject(self, Item): - Name = '' Language = '' Value = '' Name = Item.split()[1] # Check the string name is the upper character - if not self.IsCompatibleMode and Name != '': + if Name != '': MatchString = re.match('[A-Z0-9_]+', Name, re.UNICODE) if MatchString == None or MatchString.end(0) != len(Name): EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid lower case character.' %(Name, self.File)) @@ -288,7 +291,7 @@ class UniFileClassObject(object): EdkLogger.error("Unicode File Parser", FILE_NOT_FOUND, ExtraData=File.Path) try: - FileIn = codecs.open(File.Path, mode='rb', encoding='utf-16').readlines() + FileIn = codecs.open(LongFilePath(File.Path), mode='rb', encoding='utf-16').readlines() except UnicodeError, X: EdkLogger.error("build", FILE_READ_FAILURE, "File read failure: %s" % str(X), ExtraData=File.Path); except: @@ -298,23 +301,46 @@ class UniFileClassObject(object): # # Use unique identifier # + FindFlag = -1 + LineCount = 0 for Line in FileIn: + Line = FileIn[LineCount] + LineCount += 1 Line = Line.strip() # # Ignore comment line and empty line # if Line == u'' or Line.startswith(u'//'): continue + + # + # Process comment embeded in string define lines + # + FindFlag = Line.find(u'//') + if FindFlag != -1: + Line = Line.replace(Line[FindFlag:], u' ') + if FileIn[LineCount].strip().startswith('#language'): + Line = Line + FileIn[LineCount] + FileIn[LineCount-1] = Line + FileIn[LineCount] = os.linesep + LineCount -= 1 + for Index in xrange (LineCount + 1, len (FileIn) - 1): + if (Index == len(FileIn) -1): + FileIn[Index] = os.linesep + else: + FileIn[Index] = FileIn[Index + 1] + continue + Line = Line.replace(u'/langdef', u'#langdef') Line = Line.replace(u'/string', u'#string') Line = Line.replace(u'/language', u'#language') Line = Line.replace(u'/include', u'#include') + Line = Line.replace(u'\\\\', u'\u0006') Line = Line.replace(UNICODE_WIDE_CHAR, WIDE_CHAR) Line = Line.replace(UNICODE_NARROW_CHAR, NARROW_CHAR) Line = Line.replace(UNICODE_NON_BREAKING_CHAR, NON_BREAKING_CHAR) - Line = Line.replace(u'\\\\', u'\u0006') Line = Line.replace(u'\\r\\n', CR + LF) Line = Line.replace(u'\\n', CR + LF) Line = Line.replace(u'\\r', CR) @@ -322,7 +348,10 @@ class UniFileClassObject(object): Line = Line.replace(u'''\"''', u'''"''') Line = Line.replace(u'\t', u' ') Line = Line.replace(u'\u0006', u'\\') - + Line = Line.replace(DOBULE_QUOTED_SPLASH, u'"') + Line = Line.replace(SIGLE_QUOTED_SPLASH, u"'") + Line = Line.replace(TAB_BACK_SLASH, u"/") + # if Line.find(u'\\x'): # hex = Line[Line.find(u'\\x') + 2 : Line.find(u'\\x') + 6] # hex = "u'\\u" + hex + "'" @@ -453,20 +482,22 @@ class UniFileClassObject(object): if Language not in self.OrderedStringList: self.OrderedStringList[Language] = [] + self.OrderedStringDict[Language] = {} IsAdded = True - for Item in self.OrderedStringList[Language]: - if Name == Item.StringName: - IsAdded = False - if Value != None: - Item.UpdateValue(Value) - Item.UseOtherLangDef = '' - break + if Name in self.OrderedStringDict[Language]: + IsAdded = False + if Value != None: + ItemIndexInList = self.OrderedStringDict[Language][Name] + Item = self.OrderedStringList[Language][ItemIndexInList] + Item.UpdateValue(Value) + Item.UseOtherLangDef = '' if IsAdded: Token = len(self.OrderedStringList[Language]) if Index == -1: self.OrderedStringList[Language].append(StringDefClassObject(Name, Value, Referenced, Token, UseOtherLangDef)) + self.OrderedStringDict[Language][Name] = Token for LangName in self.LanguageDef: # # New STRING token will be added into all language string lists. @@ -478,8 +509,10 @@ class UniFileClassObject(object): else: OtherLangDef = Language self.OrderedStringList[LangName[0]].append(StringDefClassObject(Name, '', Referenced, Token, OtherLangDef)) + self.OrderedStringDict[LangName[0]][Name] = len(self.OrderedStringList[LangName[0]]) - 1 else: self.OrderedStringList[Language].insert(Index, StringDefClassObject(Name, Value, Referenced, Token, UseOtherLangDef)) + self.OrderedStringDict[Language][Name] = Index # # Set the string as referenced @@ -490,17 +523,18 @@ class UniFileClassObject(object): # So, only update the status of string stoken in first language string list. # Lang = self.LanguageDef[0][0] - for Item in self.OrderedStringList[Lang]: - if Name == Item.StringName: - Item.Referenced = True - break + if Name in self.OrderedStringDict[Lang]: + ItemIndexInList = self.OrderedStringDict[Lang][Name] + Item = self.OrderedStringList[Lang][ItemIndexInList] + Item.Referenced = True + # # Search the string in language definition by Name # def FindStringValue(self, Name, Lang): - for Item in self.OrderedStringList[Lang]: - if Item.StringName == Name: - return Item + if Name in self.OrderedStringDict[Lang]: + ItemIndexInList = self.OrderedStringDict[Lang][Name] + return self.OrderedStringList[Lang][ItemIndexInList] return None @@ -523,6 +557,10 @@ class UniFileClassObject(object): # FirstLangName = self.LanguageDef[0][0] + # Convert the OrderedStringList to be OrderedStringListByToken in order to faciliate future search by token + for LangNameItem in self.LanguageDef: + self.OrderedStringListByToken[LangNameItem[0]] = {} + # # Use small token for all referred string stoken. # @@ -535,6 +573,7 @@ class UniFileClassObject(object): OtherLangItem = self.OrderedStringList[LangName][Index] OtherLangItem.Referenced = True OtherLangItem.Token = RefToken + self.OrderedStringListByToken[LangName][OtherLangItem.Token] = OtherLangItem RefToken = RefToken + 1 # @@ -548,6 +587,7 @@ class UniFileClassObject(object): LangName = LangNameItem[0] OtherLangItem = self.OrderedStringList[LangName][Index] OtherLangItem.Token = RefToken + UnRefToken + self.OrderedStringListByToken[LangName][OtherLangItem.Token] = OtherLangItem UnRefToken = UnRefToken + 1 # @@ -566,6 +606,6 @@ class UniFileClassObject(object): if __name__ == '__main__': EdkLogger.Initialize() EdkLogger.SetLevel(EdkLogger.DEBUG_0) - a = UniFileClassObject(['C:\\Edk\\Strings.uni', 'C:\\Edk\\Strings2.uni']) + a = UniFileClassObject([PathClass("C:\\Edk\\Strings.uni"), PathClass("C:\\Edk\\Strings2.uni")]) a.ReToken() a.ShowMe()