]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Common/Dictionary.py
BaseTools: Add new RegExp for future use
[mirror_edk2.git] / BaseTools / Source / Python / Common / Dictionary.py
CommitLineData
30fdf114
LG
1## @file\r
2# Define a dictionary structure\r
3#\r
1be2ed90 4# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
40d841f6 5# This program and the accompanying materials\r
30fdf114
LG
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
17import EdkLogger\r
18from DataType import *\r
1be2ed90 19from Common.LongFilePathSupport import OpenLongFilePath as open\r
30fdf114
LG
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
28def ConvertTextFileToDictionary(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):\r
08dd311f 29 try:\r
47fea6af 30 F = open(FileName, 'r')\r
08dd311f
LG
31 Keys = []\r
32 for Line in F:\r
33 if Line.startswith(CommentCharacter):\r
34 continue\r
47fea6af 35 LineList = Line.split(KeySplitCharacter, 1)\r
08dd311f
LG
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
47fea6af 40 Dictionary[Key[0]] = LineList[1].replace('\\', '/').split(ValueSplitCharacter)\r
08dd311f 41 else:\r
47fea6af 42 Dictionary[Key[0]] = LineList[1].strip().replace('\\', '/')\r
08dd311f
LG
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
30fdf114
LG
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
56def printDict(Dict):\r
08dd311f
LG
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
30fdf114
LG
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
70def printList(Key, List):\r
08dd311f
LG
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