git-svn-id: https://buildtools.tianocore.org/svn/buildtools/trunk/BaseTools@172 7335b...
authorhche10x <hche10x@7335b38e-4728-0410-8992-fb3ffe349368>
Fri, 22 Jun 2007 03:24:58 +0000 (03:24 +0000)
committerhche10x <hche10x@7335b38e-4728-0410-8992-fb3ffe349368>
Fri, 22 Jun 2007 03:24:58 +0000 (03:24 +0000)
Source/Python/StrGather.py [new file with mode: 0644]
Source/Python/String.py
Source/Python/UniClassObject.py [new file with mode: 0644]

diff --git a/Source/Python/StrGather.py b/Source/Python/StrGather.py
new file mode 100644 (file)
index 0000000..6a6f665
--- /dev/null
@@ -0,0 +1,82 @@
+# Copyright (c) 2007, Intel Corporation\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution.  The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+#\r
+#This file is used to parse a strings file and create or add to a string database file.\r
+#\r
+\r
+import EdkLogger\r
+from UniClassObject import *\r
+\r
+H_FILE_HEADER = ['//', '//  DO NOT EDIT -- auto-generated file', '//', \\r
+                 '//  This file is generated by the string gather utility', '//']\r
+\r
+def CreateBaseNameDefinition(BaseName):\r
+    Str = WriteLine('', '#ifndef _' + BaseName.upper() + '_STRINGS_DEFINE_H_')\r
+    Str = Write(Str, '#define _' + BaseName.upper() + '_STRINGS_DEFINE_H_')\r
+    return Str\r
+\r
+def CreateHFile(BaseName):\r
+    HFile = ''\r
+    for Item in H_FILE_HEADER:\r
+        HFile = WriteLine(HFile, Item)\r
+    HFile = WriteLine(HFile, CreateBaseNameDefinition(BaseName))\r
+    return HFile\r
+\r
+def CreateCFile(BaseName):\r
+    CFile = ''\r
+    return CFile\r
+\r
+def GetFileList(IncludeList, SkipList):\r
+    if IncludeList == None:\r
+        return None\r
+    \r
+    FileList = []\r
+    if SkipList == None:\r
+        SkipList = []\r
+        \r
+    for Dir in IncludeList:\r
+        for File in os.listdir(Dir):\r
+            #\r
+            # Ignore file listed in skip list\r
+            #\r
+            for Skip in SkipList:\r
+                if os.path.splitext(os.path.normcase(File))[1] == Skip:\r
+                    break\r
+            \r
+            FileList.append(os.path.join(Dir, os.path.normcase(File)))\r
+    \r
+    return FileList\r
+\r
+def GetStringFiles(UniFilList, IncludeList, SkipList, BaseName):\r
+    if len(UniFilList) > 0:\r
+        Uni = UniFileClassObject(UniFilList)\r
+    \r
+    GetFileList(IncludeList, SkipList)\r
+    \r
+    if len(Uni.StringList) > 0:\r
+        pass\r
+    \r
+    HFile = CreateHFile(BaseName)\r
+    CFile = CreateCFile(BaseName)\r
+\r
+    return HFile, CFile\r
+\r
+def Write(Target, Item):\r
+    return Target + Item\r
+\r
+def WriteLine(Target, Item):\r
+    return Target + Item + '\n'\r
+    \r
+# This acts like the main() function for the script, unless it is 'import'ed into another\r
+# script.\r
+if __name__ == '__main__':\r
+    (h, c) = GetStringFiles(['C:\\Documents and Settings\\hchen30\\Desktop\\inventorystrings.uni'], None, None, 'VVV' )\r
+    print h\r
+    print c\r
index cbdc316..b82ce4c 100644 (file)
@@ -179,8 +179,8 @@ def CleanString(Line, CommentCharacter = DataType.TAB_COMMENT_SPLIT):
     Line = Line.replace(DataType.TAB_WORKSPACE1, '')\r
     Line = Line.replace(DataType.TAB_WORKSPACE2, '')\r
     #remove '/' at the beginning\r
-    if Line.startswith('/'):\r
-        Line = Line.replace('/', '')\r
+#    if Line.startswith('/'):\r
+#        Line = Line.replace('/', '')\r
     #Change path\r
     #Line = os.path.normpath(Line)\r
     \r
@@ -225,4 +225,4 @@ def GetSingleValueOfKeyFromLines(Lines, Dictionary, CommentCharacter, KeySplitCh
 if __name__ == '__main__':\r
     print SplitModuleType('LibraryClasses.common.DXE_RUNTIME_DRIVER')\r
     print SplitModuleType('Library.common')\r
-    print SplitModuleType('Librarsdsfwe')
\ No newline at end of file
+    print SplitModuleType('Librarsdsfwe')\r
diff --git a/Source/Python/UniClassObject.py b/Source/Python/UniClassObject.py
new file mode 100644 (file)
index 0000000..7be579d
--- /dev/null
@@ -0,0 +1,221 @@
+# Copyright (c) 2007, Intel Corporation\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution.  The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+#\r
+#This file is used to collect all defined strings in multiple uni files \r
+#\r
+\r
+import codecs\r
+import binascii\r
+import os\r
+import EdkLogger\r
+\r
+def StrToUnix(Str):\r
+    return unicode(Str)\r
+\r
+def HexListToUni(List):\r
+    pass\r
+\r
+def UniToStr(Uni):\r
+    return repr(Uni)[2:-1]\r
+\r
+def UniToHexList(Uni):\r
+    List = []\r
+    HexNullStr = '0x00'\r
+    for Item in Uni:\r
+        DecNum = ord(Item)\r
+        if DecNum < 16:\r
+            HexStr = '0x0' + hex(DecNum)[2:].upper()\r
+            List.append(HexStr)\r
+            List.append(HexNullStr)\r
+        elif DecNum < 256:\r
+            HexStr = hex(DecNum).upper().replace('X' ,'x')\r
+            List.append(HexStr)\r
+            List.append(HexNullStr)\r
+        elif DecNum < 4096:\r
+            HexStr = hex(ord(Item))[2:].upper()\r
+            List.append('0x'.join(HexStr[1:]))\r
+            List.append('0x0'.join(HexStr[0:1]))\r
+        else:\r
+            HexStr = hex(DecNum).upper().replace('X' ,'x')\r
+            List.append(HexStr)\r
+    return List\r
+\r
+class StringDefinitionClassObject(object):\r
+    def __init__(self, Name = None, Language = None, Value = None):\r
+        self.StringName = ''\r
+        self.StringNameByteList = []\r
+        self.StringValue = {}                        #{ u'Language' : u'Value' }\r
+        self.Referenced = False\r
+        self.Offset = ''\r
+        self.Token = ''\r
+        \r
+        if Name != None:\r
+            self.StringName = Name\r
+            self.StringNameByteList = UniToHexList(Name)\r
+        if Value != None and Language != None:\r
+            self.StringValue[Language] = UniToHexList(Value)\r
+        \r
+    def Update(self, Name = None, Language = None, Value = None):\r
+        if Name != None:\r
+            self.StringName = Name\r
+            self.StringNameByteList = UniToHexList(Name)\r
+            \r
+        if Value != None and Language != None:\r
+            self.StringValue[Language] = UniToHexList(Value)\r
+        \r
+    def __str__(self):\r
+        return repr(self.StringName) + ' ' + repr(self.StringValue)\r
+\r
+class UniFileClassObject(object):\r
+    def __init__(self, FileList = []):\r
+        self.FileList = FileList\r
+        self.LanguageDef = {}                   #{ 'LanguageIdentifier' : [PrintableName] }\r
+        self.StringList = {}                    #{ 'StringName' : StringDefinitionClassObject }\r
+        \r
+        if len(self.FileList) > 0:\r
+            self.LoadUniFiles(FileList)\r
+    \r
+    def GetLangDef(self, Line):\r
+        LangName = Line[Line.find(u'#langdef ') + len(u'#langdef ') : Line.find(u' ', len(u'#langdef '))]\r
+        LangPrintName = Line[Line.find(u'\"') + len(u'\"') : Line.rfind(u'\"')]\r
+        self.LanguageDef[LangName] = UniToHexList(LangPrintName)\r
+\r
+        return True\r
+        \r
+    def GetStringObject(self, Item):\r
+        Name = ''\r
+        Language = ''\r
+        Value = ''\r
+        \r
+        Name = Item[Item.find(u'#string ') + len(u'#string ') :Item.find(u' ', len(u'#string '))]\r
+        LanguageList = Item.split(u'#language ')\r
+        for IndexI in range(len(LanguageList)):\r
+            if IndexI == 0:\r
+                continue\r
+            else:\r
+                Language = LanguageList[IndexI][ : LanguageList[IndexI].find(u' ')]\r
+                Value = LanguageList[IndexI][LanguageList[IndexI].find(u'\"') + len(u'\"') : LanguageList[IndexI].rfind(u'\"')].replace(u'\r\n', u'')\r
+                self.AddStringToList(Name, Language, Value)\r
+    \r
+    def LoadUniFile(self, File = None):\r
+        if File != None:\r
+            if os.path.exists(File) and os.path.isfile(File):\r
+                FileIn = codecs.open(File, mode='rb', encoding='utf-16').readlines()             \r
+                Lines = []\r
+                #\r
+                # Use unique identifier\r
+                #\r
+                for Index in range(len(FileIn)):\r
+                    if FileIn[Index].startswith(u'//') or FileIn[Index] == u'\r\n':\r
+                        continue\r
+                    FileIn[Index] = FileIn[Index].replace(u'/langdef', u'#langdef')\r
+                    FileIn[Index] = FileIn[Index].replace(u'/string', u'#string')\r
+                    FileIn[Index] = FileIn[Index].replace(u'/language', u'#language')\r
+                    FileIn[Index] = FileIn[Index].replace(u'\t', u' ')\r
+                    Lines.append(FileIn[Index])\r
+                \r
+                #\r
+                # Get Unicode Information\r
+                #\r
+                for IndexI in range(len(Lines)):\r
+                    Line = Lines[IndexI]\r
+                    #\r
+                    # Ignore comment line and empty line\r
+                    #\r
+                    if Line.startswith(u'//') or Line.strip() == u'\r\n':\r
+                        continue\r
+                    \r
+                    if (IndexI + 1) < len(Lines):\r
+                        SecondLine = Lines[IndexI + 1]\r
+                    if (IndexI + 2) < len(Lines):\r
+                        ThirdLine = Lines[IndexI + 2]\r
+                                            \r
+                    #\r
+                    # Get Language def information\r
+                    # \r
+                    if Line.find(u'#langdef ') >= 0:\r
+                        self.GetLangDef(Line)\r
+                        continue\r
+                    \r
+                    Name = ''\r
+                    Language = ''\r
+                    Value = ''\r
+                    \r
+                    #\r
+                    # Get string def information format 1 as below\r
+                    #\r
+                    #     #string MY_STRING_1\r
+                    #     #language eng\r
+                    #     My first English string line 1\r
+                    #     My first English string line 2\r
+                    #     #string MY_STRING_1\r
+                    #     #language spa\r
+                    #     Mi segunda secuencia 1\r
+                    #     Mi segunda secuencia 2\r
+                    #\r
+                    if Line.find(u'#string ') >= 0 and Line.find(u'#language ') < 0 and \\r
+                        SecondLine.find(u'#string ') < 0 and SecondLine.find(u'#language ') >= 0 and \\r
+                        ThirdLine.find(u'#string ') < 0 and ThirdLine.find(u'#language ') < 0:\r
+                        Name = Line[Line.find(u'#string ') + len(u'#string ') : ].strip()\r
+                        Language = SecondLine[SecondLine.find(u'#language ') + len(u'#language ') : ].strip()\r
+                        for IndexJ in range(IndexI + 2, len(Lines)):\r
+                            if Lines[IndexJ].find(u'#string ') < 0 and Lines[IndexJ].find(u'#language ') < 0:\r
+                                Value = Value + Lines[IndexJ]\r
+                            else:\r
+                                IndexI = IndexJ\r
+                                break\r
+                        Value = Value.replace(u'\r\n', u'')\r
+                        self.AddStringToList(Name, Language, Value)\r
+                        continue\r
+                    \r
+                    #\r
+                    # Get string def information format 2 as below\r
+                    #\r
+                    #     #string MY_STRING_1     #language eng     "My first English string line 1"\r
+                    #                                               "My first English string line 2"\r
+                    #                             #language spa     "Mi segunda secuencia 1"\r
+                    #                                               "Mi segunda secuencia 2"\r
+                    #     #string MY_STRING_2     #language eng     "My first English string line 1"\r
+                    #                                               "My first English string line 2"\r
+                    #     #string MY_STRING_2     #language spa     "Mi segunda secuencia 1"\r
+                    #                                               "Mi segunda secuencia 2"\r
+                    #\r
+                    if Line.find(u'#string ') >= 0 and Line.find(u'#language ') >= 0:\r
+                        StringItem = Line\r
+                        for IndexJ in range(IndexI + 1, len(Lines)):\r
+                            if Lines[IndexJ].find(u'#string ') >= 0 and Lines[IndexJ].find(u'#language ') >= 0:\r
+                                IndexI = IndexJ\r
+                                break\r
+                            elif Lines[IndexJ].find(u'#string ') < 0 and Lines[IndexJ].find(u'#language ') >= 0:\r
+                                StringItem = StringItem + Lines[IndexJ]\r
+                            elif Lines[IndexJ].find(u'\"') >= 2:\r
+                                StringItem = StringItem[ : StringItem.rfind(u'\"')] + Lines[IndexJ][Lines[IndexJ].find(u'\"') + len(u'\"') : ]\r
+                        self.GetStringObject(StringItem)\r
+\r
+    def LoadUniFiles(self, FileList = []):\r
+        if len(FileList) > 0:\r
+            for File in FileList:\r
+                self.LoadUniFile(File)\r
+                \r
+    def AddStringToList(self, Name, Language, Value):\r
+        if Name in self.StringList.keys():\r
+            self.StringList[UniToStr(Name)].Update(Name, Language, Value)\r
+        else:\r
+            self.StringList[UniToStr(Name)] = StringDefinitionClassObject(Name, Language, Value)\r
+\r
+# This acts like the main() function for the script, unless it is 'import'ed into another\r
+# script.\r
+if __name__ == '__main__':\r
+    a = UniFileClassObject(['C:\\Documents and Settings\\hchen30\\Desktop\\inventorystrings.uni'])\r
+    for i in a.LanguageDef:\r
+        print i, a.LanguageDef[i]\r
+    for i in a.StringList:\r
+        print i, str(a.StringList[i])\r
+    
\ No newline at end of file