-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparseconfig.py
More file actions
executable file
·59 lines (50 loc) · 1.78 KB
/
parseconfig.py
File metadata and controls
executable file
·59 lines (50 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
import sys, getopt, warnings, os, re
def getConfigMasterKeyValue (sMasterKey, dictConfigKeyValues) :
dictKeyValueSet = {}
for currentKey, currentValue in dictConfigKeyValues.items() :
if( currentKey.startswith(sMasterKey+"{") and currentKey.endswith("}") and ((len(sMasterKey) + 2) < len(currentKey))) :
coreKey = currentKey[len(sMasterKey)+1: len(currentKey)-1]
dictKeyValueSet [coreKey] = currentValue
return dictKeyValueSet
def parseConfigLine (sLine, sSectionName) :
# sSectionName is a list, but only the first value is used
currentKey = ""
currentValue = ""
if ((sLine[0] == "[") and (sLine[len(sLine)-1] == "]" )) :
sSectionName[0] = sLine
elif (sSectionName[0] == "") :
print("no section name")
else :
twoParts = sLine.split("=")
if (len(twoParts) != 2) :
print("wrong line: "+sLine)
else :
currentKey = sSectionName[0] + twoParts[0].rstrip()
currentValue = twoParts[1].lstrip()
#print currentKey+"=>"+currentValue
return [currentKey, currentValue]
def parseConfigKeyValues (filepath) :
configFile = open(filepath)
sSectionName = [""]
dictConfigKeyValues = {}
for sLine in configFile.readlines() :
poundPos = sLine.find("#")
if (poundPos > -1) :
sLine = sLine[0:poundPos]
sLine = sLine.strip()
if (sLine == "") :
continue
else :
#print "!!!"+sLine+"!!!"
currentKey, currentValue = parseConfigLine (sLine, sSectionName)
if (currentKey != "") and (currentValue != "" ):
# print currentKey+"=>"+currentValue
if (dictConfigKeyValues.get(currentKey) == None ):
dictConfigKeyValues[currentKey] = currentValue
else :
print(currentKey + " has existed")
configFile.close()
# for currentKey, currentValue in dictConfigKeyValues.items() :
# print currentKey, currentValue
return dictConfigKeyValues