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