]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/AutoGen/UniClassObject.py
BaseTools: Replace StringIO.StringIO with io.BytesIO
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / UniClassObject.py
index 242402dfaeeb511f7bbf4420cbd7feafc8fe4d6f..88810f1ccc0d9b2fa3e2f4d7d0d6d520526765eb 100644 (file)
 ##\r
 # Import Modules\r
 #\r
+from __future__ import print_function\r
 import Common.LongFilePathOs as os, codecs, re\r
 import distutils.util\r
 import Common.EdkLogger as EdkLogger\r
-import StringIO\r
+from io import BytesIO\r
 from Common.BuildToolError import *\r
-from Common.String import GetLineNo\r
+from Common.StringUtils import GetLineNo\r
 from Common.Misc import PathClass\r
 from Common.LongFilePathSupport import LongFilePath\r
 from Common.GlobalData import *\r
@@ -118,8 +119,6 @@ LangConvTable = {'eng':'en', 'fra':'fr', \
 # @retval LangName:  Valid lanugage code in RFC 4646 format or None\r
 #\r
 def GetLanguageCode(LangName, IsCompatibleMode, File):\r
-    global LangConvTable\r
-\r
     length = len(LangName)\r
     if IsCompatibleMode:\r
         if length == 3 and LangName.isalpha():\r
@@ -218,6 +217,19 @@ class StringDefClassObject(object):
             self.StringValueByteList = UniToHexList(self.StringValue)\r
             self.Length = len(self.StringValueByteList)\r
 \r
+def StripComments(Line):\r
+    Comment = u'//'\r
+    CommentPos = Line.find(Comment)\r
+    while CommentPos >= 0:\r
+    # if there are non matched quotes before the comment header\r
+    # then we are in the middle of a string\r
+    # but we need to ignore the escaped quotes and backslashes.\r
+        if ((Line.count(u'"', 0, CommentPos) - Line.count(u'\\"', 0, CommentPos)) & 1) == 1:\r
+            CommentPos = Line.find (Comment, CommentPos + 1)\r
+        else:\r
+            return Line[:CommentPos].strip()\r
+    return Line.strip()\r
+\r
 ## UniFileClassObject\r
 #\r
 # A structure for .uni file definition\r
@@ -242,8 +254,8 @@ class UniFileClassObject(object):
         Lang = distutils.util.split_quoted((Line.split(u"//")[0]))\r
         if len(Lang) != 3:\r
             try:\r
-                FileIn = self.OpenUniFile(LongFilePath(File.Path))\r
-            except UnicodeError, X:\r
+                FileIn = UniFileClassObject.OpenUniFile(LongFilePath(File.Path))\r
+            except UnicodeError as X:\r
                 EdkLogger.error("build", FILE_READ_FAILURE, "File read failure: %s" % str(X), ExtraData=File);\r
             except:\r
                 EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=File);\r
@@ -286,7 +298,8 @@ class UniFileClassObject(object):
                     self.OrderedStringDict[LangName][Item.StringName] = len(self.OrderedStringList[LangName]) - 1\r
         return True\r
 \r
-    def OpenUniFile(self, FileName):\r
+    @staticmethod\r
+    def OpenUniFile(FileName):\r
         #\r
         # Read file\r
         #\r
@@ -305,14 +318,15 @@ class UniFileClassObject(object):
             FileIn.startswith(codecs.BOM_UTF16_LE)):\r
             Encoding = 'utf-16'\r
 \r
-        self.VerifyUcs2Data(FileIn, FileName, Encoding)\r
+        UniFileClassObject.VerifyUcs2Data(FileIn, FileName, Encoding)\r
 \r
-        UniFile = StringIO.StringIO(FileIn)\r
+        UniFile = BytesIO(FileIn)\r
         Info = codecs.lookup(Encoding)\r
         (Reader, Writer) = (Info.streamreader, Info.streamwriter)\r
         return codecs.StreamReaderWriter(UniFile, Reader, Writer)\r
 \r
-    def VerifyUcs2Data(self, FileIn, FileName, Encoding):\r
+    @staticmethod\r
+    def VerifyUcs2Data(FileIn, FileName, Encoding):\r
         Ucs2Info = codecs.lookup('ucs-2')\r
         #\r
         # Convert to unicode\r
@@ -321,7 +335,7 @@ class UniFileClassObject(object):
             FileDecoded = codecs.decode(FileIn, Encoding)\r
             Ucs2Info.encode(FileDecoded)\r
         except:\r
-            UniFile = StringIO.StringIO(FileIn)\r
+            UniFile = BytesIO(FileIn)\r
             Info = codecs.lookup(Encoding)\r
             (Reader, Writer) = (Info.streamreader, Info.streamwriter)\r
             File = codecs.StreamReaderWriter(UniFile, Reader, Writer)\r
@@ -351,8 +365,8 @@ class UniFileClassObject(object):
         Name = Item.split()[1]\r
         # Check the string name\r
         if Name != '':\r
-            MatchString = re.match('^[a-zA-Z][a-zA-Z0-9_]*$', Name, re.UNICODE)\r
-            if MatchString is None or MatchString.end(0) != len(Name):\r
+            MatchString = gIdentifierPattern.match(Name)\r
+            if MatchString is None:\r
                 EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid character.' % (Name, self.File))\r
         LanguageList = Item.split(u'#language ')\r
         for IndexI in range(len(LanguageList)):\r
@@ -371,20 +385,6 @@ class UniFileClassObject(object):
         FileName = Item[Item.find(u'#include ') + len(u'#include ') :Item.find(u' ', len(u'#include '))][1:-1]\r
         self.LoadUniFile(FileName)\r
 \r
-    def StripComments(self, Line):\r
-        Comment = u'//'\r
-        CommentPos = Line.find(Comment)\r
-        while CommentPos >= 0:\r
-        # if there are non matched quotes before the comment header\r
-        # then we are in the middle of a string\r
-        # but we need to ignore the escaped quotes and backslashes.\r
-            if ((Line.count(u'"', 0, CommentPos) - Line.count(u'\\"', 0, CommentPos)) & 1) == 1:\r
-                CommentPos = Line.find (Comment, CommentPos + 1)\r
-            else:\r
-                return Line[:CommentPos].strip()\r
-        return Line.strip()\r
-                \r
-\r
     #\r
     # Pre-process before parse .uni file\r
     #\r
@@ -393,8 +393,8 @@ class UniFileClassObject(object):
             EdkLogger.error("Unicode File Parser", FILE_NOT_FOUND, ExtraData=File.Path)\r
 \r
         try:\r
-            FileIn = self.OpenUniFile(LongFilePath(File.Path))\r
-        except UnicodeError, X:\r
+            FileIn = UniFileClassObject.OpenUniFile(LongFilePath(File.Path))\r
+        except UnicodeError as X:\r
             EdkLogger.error("build", FILE_READ_FAILURE, "File read failure: %s" % str(X), ExtraData=File.Path);\r
         except:\r
             EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=File.Path);\r
@@ -406,7 +406,7 @@ class UniFileClassObject(object):
         for Line in FileIn:\r
             Line = Line.strip()\r
             Line = Line.replace(u'\\\\', BACK_SLASH_PLACEHOLDER)\r
-            Line = self.StripComments(Line)\r
+            Line = StripComments(Line)\r
 \r
             #\r
             # Ignore empty line\r
@@ -521,8 +521,8 @@ class UniFileClassObject(object):
                 Language = GetLanguageCode(Language, self.IsCompatibleMode, self.File)\r
                 # Check the string name\r
                 if not self.IsCompatibleMode and Name != '':\r
-                    MatchString = re.match('^[a-zA-Z][a-zA-Z0-9_]*$', Name, re.UNICODE)\r
-                    if MatchString is None or MatchString.end(0) != len(Name):\r
+                    MatchString = gIdentifierPattern.match(Name)\r
+                    if MatchString is None:\r
                         EdkLogger.error('Unicode File Parser', FORMAT_INVALID, 'The string token name %s defined in UNI file %s contains the invalid character.' % (Name, self.File))\r
                 self.AddStringToList(Name, Language, Value)\r
                 continue\r
@@ -685,12 +685,12 @@ class UniFileClassObject(object):
     # Show the instance itself\r
     #\r
     def ShowMe(self):\r
-        print self.LanguageDef\r
+        print(self.LanguageDef)\r
         #print self.OrderedStringList\r
         for Item in self.OrderedStringList:\r
-            print Item\r
+            print(Item)\r
             for Member in self.OrderedStringList[Item]:\r
-                print str(Member)\r
+                print(str(Member))\r
 \r
 # This acts like the main() function for the script, unless it is 'import'ed into another\r
 # script.\r