-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortConfigLog.py
executable file
·114 lines (86 loc) · 2.5 KB
/
sortConfigLog.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# @brief Sort configuration log removing duplicates
import sys
import os
#Python procedures to sort config log and remove duplicates
def read_config(filename):
'''
Read config log and returns dictionary { vriable : value }
@param filename configuration file in the format variable = value
'''
f = open(filename)
config_dict = {}
for lines in f:
if not lines.startswith("#"):
items = lines.strip().split('= ', 1)
if items == ['']:
continue
config_dict[items[0]] = (items[1])
return config_dict
def print_sorted_dict( d ):
'''
Print sorted dict
@param d dictionary
'''
for key in sorted(d.iterkeys()):
print("%s: %s" % (key, d[key]))
def save_sorted_dict_in_file( d, filename ):
'''
Save sorted dict d into file
@param d dictionary to be sorted and saved
@param filename file to save dictionary
'''
file_ = open(filename, 'w')
tmp = ""
for key in sorted(d.iterkeys()):
tmp = key + " = " + d[key] + '\n'
file_.write( tmp )
file_.close()
def save_dict_in_file( d, filename ):
'''
Save dict d into file
@param d dictionary to be saved
@param filename file to save dictionary
'''
file_ = open(filename, 'w')
tmp = ""
for key in d.iterkeys():
tmp = key + " = " + d[key] + '\n'
file_.write( tmp )
file_.close()
def read_config_file_option(filename, option):
'''
Read config file of the type
option = value
and extract value for option
@param filename configuration file
@param option option to be extracted
'''
f = open(filename, 'r')
config_dict = {}
for lines in f:
if not lines.startswith("#"):
items = lines.strip().split('= ', 1)
if items == ['']:
continue
config_dict[items[0].strip()] = (items[1].split('#', 1)[0].strip())
value = config_dict[option]
return value
def main(argv):
'''
Main method
'''
#read Generator.dat and extract name of config log
logname = read_config_file_option('Generator.dat', 'ConfigLogFile' )
#sortedLogname = "sorted" + logname
log = read_config(logname)
#print_sorted_dict( log )
#save_sorted_dict_in_file( log, sortedLogname )
#save_dict_in_file( log, sortedLogname )
#Remove oryginal config log and save the new one
os.remove(logname)
save_sorted_dict_in_file( log, logname )
#save_dict_in_file( log, logname )
#print( "Read: " + logname + " and write result in: " + sortedLogname)
if __name__ == "__main__":
main(sys.argv)