]>
Commit | Line | Data |
---|---|---|
1 | ## @file\r | |
2 | # Define a dictionary structure\r | |
3 | #\r | |
4 | # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r | |
5 | # This program and the accompanying materials\r | |
6 | # are licensed and made available under the terms and conditions of the BSD License\r | |
7 | # which accompanies this distribution. The full text of the license may be found at\r | |
8 | # http://opensource.org/licenses/bsd-license.php\r | |
9 | #\r | |
10 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | #\r | |
13 | \r | |
14 | ##\r | |
15 | # Import Modules\r | |
16 | #\r | |
17 | import EdkLogger\r | |
18 | from DataType import *\r | |
19 | from Common.LongFilePathSupport import OpenLongFilePath as open\r | |
20 | \r | |
21 | ## Convert a text file to a dictionary\r | |
22 | #\r | |
23 | # Convert a text file to a dictionary of (name:value) pairs.\r | |
24 | #\r | |
25 | # @retval 0 Convert successful\r | |
26 | # @retval 1 Open file failed\r | |
27 | #\r | |
28 | def ConvertTextFileToDictionary(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):\r | |
29 | try:\r | |
30 | F = open(FileName, 'r')\r | |
31 | Keys = []\r | |
32 | for Line in F:\r | |
33 | if Line.startswith(CommentCharacter):\r | |
34 | continue\r | |
35 | LineList = Line.split(KeySplitCharacter, 1)\r | |
36 | if len(LineList) >= 2:\r | |
37 | Key = LineList[0].split()\r | |
38 | if len(Key) == 1 and Key[0][0] != CommentCharacter and Key[0] not in Keys:\r | |
39 | if ValueSplitFlag:\r | |
40 | Dictionary[Key[0]] = LineList[1].replace('\\', '/').split(ValueSplitCharacter)\r | |
41 | else:\r | |
42 | Dictionary[Key[0]] = LineList[1].strip().replace('\\', '/')\r | |
43 | Keys += [Key[0]]\r | |
44 | F.close()\r | |
45 | return 0\r | |
46 | except:\r | |
47 | EdkLogger.info('Open file failed')\r | |
48 | return 1\r | |
49 | \r | |
50 | ## Print the dictionary\r | |
51 | #\r | |
52 | # Print all items of dictionary one by one\r | |
53 | #\r | |
54 | # @param Dict: The dictionary to be printed\r | |
55 | #\r | |
56 | def printDict(Dict):\r | |
57 | if Dict != None:\r | |
58 | KeyList = Dict.keys()\r | |
59 | for Key in KeyList:\r | |
60 | if Dict[Key] != '':\r | |
61 | print Key + ' = ' + str(Dict[Key])\r | |
62 | \r | |
63 | ## Print the dictionary\r | |
64 | #\r | |
65 | # Print the items of dictionary which matched with input key\r | |
66 | #\r | |
67 | # @param list: The dictionary to be printed\r | |
68 | # @param key: The key of the item to be printed\r | |
69 | #\r | |
70 | def printList(Key, List):\r | |
71 | if type(List) == type([]):\r | |
72 | if len(List) > 0:\r | |
73 | if Key.find(TAB_SPLIT) != -1:\r | |
74 | print "\n" + Key\r | |
75 | for Item in List:\r | |
76 | print Item\r |