Added from python source root
[people/mcb30/basetools.git] / Source / Python / Common / DecClassObject.py
1 # Copyright (c) 2007, Intel Corporation\r
2 # All rights reserved. This program and the accompanying materials\r
3 # are licensed and made available under the terms and conditions of the BSD License\r
4 # which accompanies this distribution.    The full text of the license may be found at\r
5 # http://opensource.org/licenses/bsd-license.php\r
6 #\r
7 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
8 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
9 \r
10 #\r
11 #This file is used to define each component of DEC file\r
12 #\r
13 \r
14 import os\r
15 from String import *\r
16 from DataType import *\r
17 from Identification import *\r
18 from Dictionary import *\r
19 \r
20 class DecObject(object):\r
21     def __init__(self):\r
22         object.__init__()\r
23 \r
24 class DecDefines(DecObject):\r
25     def __init__(self):\r
26         self.DefinesDictionary = {\r
27             #Req\r
28             TAB_DEC_DEFINES_DEC_SPECIFICATION           : [''],\r
29             TAB_DEC_DEFINES_PACKAGE_NAME                : [''],\r
30             TAB_DEC_DEFINES_PACKAGE_GUID                : [''],\r
31             TAB_DEC_DEFINES_PACKAGE_VERSION             : ['']\r
32         }\r
33         \r
34 class DecContents(DecObject):\r
35     def __init__(self):\r
36         self.Includes = []\r
37         self.Guids = []\r
38         self.Protocols = []\r
39         self.Ppis = []\r
40         self.LibraryClasses = []\r
41         self.PcdsFixedAtBuild = []\r
42         self.PcdsPatchableInModule = []\r
43         self.PcdsFeatureFlag = []\r
44         self.PcdsDynamic = []\r
45         self.PcdsDynamicEx = []\r
46 \r
47 class Dec(DecObject):\r
48     def __init__(self, filename = None, isMergeAllArches = False):\r
49         self.identification = Identification()\r
50         self.Defines = DecDefines()\r
51         \r
52         self.Contents = {}\r
53         for key in DataType.ARCH_LIST_FULL:\r
54             self.Contents[key] = DecContents()\r
55         \r
56         self.KeyList = [\r
57             TAB_INCLUDES, TAB_GUIDS, TAB_PROTOCOLS, TAB_PPIS, TAB_LIBRARY_CLASSES, \\r
58             TAB_PCDS_FIXED_AT_BUILD_NULL, TAB_PCDS_PATCHABLE_IN_MODULE_NULL, TAB_PCDS_FEATURE_FLAG_NULL, \\r
59             TAB_PCDS_DYNAMIC_NULL, TAB_PCDS_DYNAMIC_EX_NULL\r
60         ]\r
61     \r
62         if filename != None:\r
63             self.LoadDecFile(filename)\r
64             \r
65         if isMergeAllArches:\r
66             self.MergeAllArches()            \r
67     \r
68     def ParseDec(self, Lines, Key, KeyField):\r
69         GetMultipleValuesOfKeyFromLines(Lines, Key, KeyField, TAB_COMMENT_SPLIT)\r
70             \r
71     def MergeAllArches(self):\r
72         for key in self.KeyList:\r
73             for arch in DataType.ARCH_LIST:\r
74                 Command = "self.Contents[arch]." + key + ".extend(" + "self.Contents['" + DataType.TAB_ARCH_COMMON + "']." + key + ")"\r
75                 eval(Command)\r
76 \r
77     def LoadDecFile(self, filename):\r
78         f = open(filename, 'r').read()\r
79         sects = f.split('[')\r
80         for sect in sects:\r
81             tab = (sect.split(TAB_SECTION_END, 1)[0]).upper()\r
82             if tab == TAB_INF_DEFINES.upper():\r
83                 GetSingleValueOfKeyFromLines(sect, self.Defines.DefinesDictionary, TAB_COMMENT_SPLIT, TAB_EQUAL_SPLIT, True, TAB_VALUE_SPLIT)\r
84                 continue\r
85             for arch in DataType.ARCH_LIST_FULL + [DataType.TAB_ARCH_NULL]:\r
86                 for key in self.KeyList:\r
87                     if arch != DataType.TAB_ARCH_NULL:\r
88                         target = (key + DataType.TAB_SPLIT + arch).upper()\r
89                     else:\r
90                         target = key.upper()\r
91                     if tab == target:\r
92                         if arch != DataType.TAB_ARCH_NULL:\r
93                             Command = 'self.ParseDec(sect, tab, self.Contents[arch].' + key + ')'\r
94                             eval(Command)\r
95                             continue\r
96                         else:\r
97                             Command = "self.ParseDec(sect, tab, self.Contents['" + DataType.TAB_ARCH_COMMON + "']." + key + ')'\r
98                             eval(Command)\r
99                             continue\r
100         #EndFor\r
101 \r
102     def showDec(self):\r
103         print TAB_SECTION_START + TAB_INF_DEFINES + TAB_SECTION_END\r
104         printDict(self.Defines.DefinesDictionary)\r
105 \r
106         for key in self.KeyList:\r
107             for arch in DataType.ARCH_LIST_FULL:\r
108                 Command = "printList(TAB_SECTION_START + '" + \\r
109                                     key + DataType.TAB_SPLIT + arch + \\r
110                                     "' + TAB_SECTION_END, self.Contents[arch]." + key + ')'\r
111                 eval(Command)\r
112 \r
113 if __name__ == '__main__':\r
114     p = Dec()\r
115     directory = 'C:\Documents and Settings\\hchen30\\Desktop\\prototype\\dec'\r
116     fileList = []\r
117     for f in os.listdir(directory):\r
118         if os.path.splitext(os.path.normcase(f))[1] == '.dec':\r
119             fileList.append(os.path.join(directory, os.path.normcase(f)))\r
120             \r
121             \r
122     for f in fileList:\r
123         p.LoadDecFile(f)\r
124         p.showDec()\r