X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FAutoGen%2FUniClassObject.py;h=384f31b165de08222f36e8f5330549ec7182da7e;hb=ecbaa856da0c94b7054f795d001ee3f5aaee033e;hp=5b674dfbc2f0a04cf6ee3728b072b04efa97f5d0;hpb=018f7b827fa4def3476f76cdf1d6400d4a8e6ebc;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/AutoGen/UniClassObject.py b/BaseTools/Source/Python/AutoGen/UniClassObject.py index 5b674dfbc2..384f31b165 100644 --- a/BaseTools/Source/Python/AutoGen/UniClassObject.py +++ b/BaseTools/Source/Python/AutoGen/UniClassObject.py @@ -16,12 +16,13 @@ ## # Import Modules # +from __future__ import print_function import Common.LongFilePathOs as os, codecs, re import distutils.util import Common.EdkLogger as EdkLogger -import StringIO +from io import BytesIO from Common.BuildToolError import * -from Common.String import GetLineNo +from Common.StringUtils import GetLineNo from Common.Misc import PathClass from Common.LongFilePathSupport import LongFilePath from Common.GlobalData import * @@ -118,13 +119,11 @@ LangConvTable = {'eng':'en', 'fra':'fr', \ # @retval LangName: Valid lanugage code in RFC 4646 format or None # def GetLanguageCode(LangName, IsCompatibleMode, File): - global LangConvTable - length = len(LangName) if IsCompatibleMode: if length == 3 and LangName.isalpha(): TempLangName = LangConvTable.get(LangName.lower()) - if TempLangName != None: + if TempLangName is not None: return TempLangName return LangName else: @@ -136,7 +135,7 @@ def GetLanguageCode(LangName, IsCompatibleMode, File): if LangName.isalpha(): return LangName elif length == 3: - if LangName.isalpha() and LangConvTable.get(LangName.lower()) == None: + if LangName.isalpha() and LangConvTable.get(LangName.lower()) is None: return LangName elif length == 5: if LangName[0:2].isalpha() and LangName[2] == '-': @@ -144,7 +143,7 @@ def GetLanguageCode(LangName, IsCompatibleMode, File): elif length >= 6: if LangName[0:2].isalpha() and LangName[2] == '-': return LangName - if LangName[0:3].isalpha() and LangConvTable.get(LangName.lower()) == None and LangName[3] == '-': + if LangName[0:3].isalpha() and LangConvTable.get(LangName.lower()) is None and LangName[3] == '-': return LangName EdkLogger.error("Unicode File Parser", FORMAT_INVALID, "Invalid RFC 4646 language code : %s" % LangName, File) @@ -195,14 +194,14 @@ class StringDefClassObject(object): self.UseOtherLangDef = UseOtherLangDef self.Length = 0 - if Name != None: + if Name is not None: self.StringName = Name self.StringNameByteList = UniToHexList(Name) - if Value != None: + if Value is not None: self.StringValue = Value + u'\x00' # Add a NULL at string tail self.StringValueByteList = UniToHexList(self.StringValue) self.Length = len(self.StringValueByteList) - if Token != None: + if Token is not None: self.Token = Token def __str__(self): @@ -213,11 +212,24 @@ class StringDefClassObject(object): repr(self.UseOtherLangDef) def UpdateValue(self, Value = None): - if Value != None: + if Value is not None: self.StringValue = Value + u'\x00' # Add a NULL at string tail self.StringValueByteList = UniToHexList(self.StringValue) self.Length = len(self.StringValueByteList) +def StripComments(Line): + Comment = u'//' + CommentPos = Line.find(Comment) + while CommentPos >= 0: + # if there are non matched quotes before the comment header + # then we are in the middle of a string + # but we need to ignore the escaped quotes and backslashes. + if ((Line.count(u'"', 0, CommentPos) - Line.count(u'\\"', 0, CommentPos)) & 1) == 1: + CommentPos = Line.find (Comment, CommentPos + 1) + else: + return Line[:CommentPos].strip() + return Line.strip() + ## UniFileClassObject # # A structure for .uni file definition @@ -242,8 +254,8 @@ class UniFileClassObject(object): Lang = distutils.util.split_quoted((Line.split(u"//")[0])) if len(Lang) != 3: try: - FileIn = self.OpenUniFile(LongFilePath(File.Path)) - except UnicodeError, X: + FileIn = UniFileClassObject.OpenUniFile(LongFilePath(File.Path)) + except UnicodeError as X: EdkLogger.error("build", FILE_READ_FAILURE, "File read failure: %s" % str(X), ExtraData=File); except: EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=File); @@ -272,7 +284,7 @@ class UniFileClassObject(object): if not IsLangInDef: # # The found STRING tokens will be added into new language string list - # so that the unique STRING identifier is reserved for all languages in the package list. + # so that the unique STRING identifier is reserved for all languages in the package list. # FirstLangName = self.LanguageDef[0][0] if LangName != FirstLangName: @@ -286,7 +298,8 @@ class UniFileClassObject(object): self.OrderedStringDict[LangName][Item.StringName] = len(self.OrderedStringList[LangName]) - 1 return True - def OpenUniFile(self, FileName): + @staticmethod + def OpenUniFile(FileName): # # Read file # @@ -305,14 +318,15 @@ class UniFileClassObject(object): FileIn.startswith(codecs.BOM_UTF16_LE)): Encoding = 'utf-16' - self.VerifyUcs2Data(FileIn, FileName, Encoding) + UniFileClassObject.VerifyUcs2Data(FileIn, FileName, Encoding) - UniFile = StringIO.StringIO(FileIn) + UniFile = BytesIO(FileIn) Info = codecs.lookup(Encoding) (Reader, Writer) = (Info.streamreader, Info.streamwriter) return codecs.StreamReaderWriter(UniFile, Reader, Writer) - def VerifyUcs2Data(self, FileIn, FileName, Encoding): + @staticmethod + def VerifyUcs2Data(FileIn, FileName, Encoding): Ucs2Info = codecs.lookup('ucs-2') # # Convert to unicode @@ -321,7 +335,7 @@ class UniFileClassObject(object): FileDecoded = codecs.decode(FileIn, Encoding) Ucs2Info.encode(FileDecoded) except: - UniFile = StringIO.StringIO(FileIn) + UniFile = BytesIO(FileIn) Info = codecs.lookup(Encoding) (Reader, Writer) = (Info.streamreader, Info.streamwriter) File = codecs.StreamReaderWriter(UniFile, Reader, Writer) @@ -351,8 +365,8 @@ class UniFileClassObject(object): Name = Item.split()[1] # Check the string name if Name != '': - MatchString = re.match('^[a-zA-Z][a-zA-Z0-9_]*$', Name, re.UNICODE) - if MatchString == None or MatchString.end(0) != len(Name): + MatchString = gIdentifierPattern.match(Name) + if MatchString is None: EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid character.' % (Name, self.File)) LanguageList = Item.split(u'#language ') for IndexI in range(len(LanguageList)): @@ -371,20 +385,6 @@ class UniFileClassObject(object): FileName = Item[Item.find(u'#include ') + len(u'#include ') :Item.find(u' ', len(u'#include '))][1:-1] self.LoadUniFile(FileName) - def StripComments(self, Line): - Comment = u'//' - CommentPos = Line.find(Comment) - while CommentPos >= 0: - # if there are non matched quotes before the comment header - # then we are in the middle of a string - # but we need to ignore the escaped quotes and backslashes. - if ((Line.count(u'"', 0, CommentPos) - Line.count(u'\\"', 0, CommentPos)) & 1) == 1: - CommentPos = Line.find (Comment, CommentPos + 1) - else: - return Line[:CommentPos].strip() - return Line.strip() - - # # Pre-process before parse .uni file # @@ -393,8 +393,8 @@ class UniFileClassObject(object): EdkLogger.error("Unicode File Parser", FILE_NOT_FOUND, ExtraData=File.Path) try: - FileIn = self.OpenUniFile(LongFilePath(File.Path)) - except UnicodeError, X: + FileIn = UniFileClassObject.OpenUniFile(LongFilePath(File.Path)) + except UnicodeError as X: EdkLogger.error("build", FILE_READ_FAILURE, "File read failure: %s" % str(X), ExtraData=File.Path); except: EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=File.Path); @@ -406,15 +406,15 @@ class UniFileClassObject(object): for Line in FileIn: Line = Line.strip() Line = Line.replace(u'\\\\', BACK_SLASH_PLACEHOLDER) - Line = self.StripComments(Line) + Line = StripComments(Line) # # Ignore empty line # - if len(Line) == 0: - continue - - + if len(Line) == 0: + continue + + Line = Line.replace(u'/langdef', u'#langdef') Line = Line.replace(u'/string', u'#string') Line = Line.replace(u'/language', u'#language') @@ -429,8 +429,8 @@ class UniFileClassObject(object): Line = Line.replace(u'\\r', CR) Line = Line.replace(u'\\t', u' ') Line = Line.replace(u'\t', u' ') - Line = Line.replace(u'\\"', u'"') - Line = Line.replace(u"\\'", u"'") + Line = Line.replace(u'\\"', u'"') + Line = Line.replace(u"\\'", u"'") Line = Line.replace(BACK_SLASH_PLACEHOLDER, u'\\') StartPos = Line.find(u'\\x') @@ -466,7 +466,7 @@ class UniFileClassObject(object): # Load a .uni file # def LoadUniFile(self, File = None): - if File == None: + if File is None: EdkLogger.error("Unicode File Parser", PARSER_ERROR, 'No unicode file is given') self.File = File # @@ -521,8 +521,8 @@ class UniFileClassObject(object): Language = GetLanguageCode(Language, self.IsCompatibleMode, self.File) # Check the string name if not self.IsCompatibleMode and Name != '': - MatchString = re.match('^[a-zA-Z][a-zA-Z0-9_]*$', Name, re.UNICODE) - if MatchString == None or MatchString.end(0) != len(Name): + MatchString = gIdentifierPattern.match(Name) + if MatchString is None: EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid character.' % (Name, self.File)) self.AddStringToList(Name, Language, Value) continue @@ -570,7 +570,7 @@ class UniFileClassObject(object): else: EdkLogger.error('Unicode File Parser', FORMAT_NOT_SUPPORTED, "The language '%s' for %s is not defined in Unicode file %s." \ % (Language, Name, self.File)) - + if Language not in self.OrderedStringList: self.OrderedStringList[Language] = [] self.OrderedStringDict[Language] = {} @@ -578,7 +578,7 @@ class UniFileClassObject(object): IsAdded = True if Name in self.OrderedStringDict[Language]: IsAdded = False - if Value != None: + if Value is not None: ItemIndexInList = self.OrderedStringDict[Language][Name] Item = self.OrderedStringList[Language][ItemIndexInList] Item.UpdateValue(Value) @@ -592,7 +592,7 @@ class UniFileClassObject(object): for LangName in self.LanguageDef: # # New STRING token will be added into all language string lists. - # so that the unique STRING identifier is reserved for all languages in the package list. + # so that the unique STRING identifier is reserved for all languages in the package list. # if LangName[0] != Language: if UseOtherLangDef != '': @@ -685,12 +685,12 @@ class UniFileClassObject(object): # Show the instance itself # def ShowMe(self): - print self.LanguageDef + print(self.LanguageDef) #print self.OrderedStringList for Item in self.OrderedStringList: - print Item + print(Item) for Member in self.OrderedStringList[Item]: - print str(Member) + print(str(Member)) # This acts like the main() function for the script, unless it is 'import'ed into another # script.