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