]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Common/Dictionary.py
Sync EDKII BaseTools to BaseTools project r1971
[mirror_edk2.git] / BaseTools / Source / Python / Common / Dictionary.py
CommitLineData
30fdf114
LG
1## @file\r
2# Define a dictionary structure\r
3#\r
40d841f6
LG
4# Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>\r
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
19\r
20## Convert a text file to a dictionary\r
21#\r
22# Convert a text file to a dictionary of (name:value) pairs.\r
23#\r
24# @retval 0 Convert successful\r
25# @retval 1 Open file failed\r
26#\r
27def ConvertTextFileToDictionary(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):\r
28 try:\r
29 F = open(FileName,'r')\r
30 Keys = []\r
31 for Line in F:\r
32 if Line.startswith(CommentCharacter):\r
33 continue\r
34 LineList = Line.split(KeySplitCharacter,1)\r
35 if len(LineList) >= 2:\r
36 Key = LineList[0].split()\r
37 if len(Key) == 1 and Key[0][0] != CommentCharacter and Key[0] not in Keys:\r
38 if ValueSplitFlag:\r
39 Dictionary[Key[0]] = LineList[1].replace('\\','/').split(ValueSplitCharacter)\r
40 else:\r
41 Dictionary[Key[0]] = LineList[1].strip().replace('\\','/')\r
42 Keys += [Key[0]]\r
43 F.close()\r
44 return 0\r
45 except:\r
46 EdkLogger.info('Open file failed')\r
47 return 1\r
48\r
49## Print the dictionary\r
50#\r
51# Print all items of dictionary one by one\r
52#\r
53# @param Dict: The dictionary to be printed\r
54#\r
55def printDict(Dict):\r
56 if Dict != None:\r
57 KeyList = Dict.keys()\r
58 for Key in KeyList:\r
59 if Dict[Key] != '':\r
60 print Key + ' = ' + str(Dict[Key])\r
61\r
62## Print the dictionary\r
63#\r
64# Print the items of dictionary which matched with input key\r
65#\r
66# @param list: The dictionary to be printed\r
67# @param key: The key of the item to be printed\r
68#\r
69def printList(Key, List):\r
70 if type(List) == type([]):\r
71 if len(List) > 0:\r
72 if key.find(TAB_SPLIT) != -1:\r
73 print "\n" + Key\r
74 for Item in List:\r
75 print Item\r