-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackPSUconfig.py
More file actions
132 lines (109 loc) · 3.59 KB
/
HackPSUconfig.py
File metadata and controls
132 lines (109 loc) · 3.59 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
This module provides an interface for reading and writing to a HackPSU RaspberryPi Scanner config file
Methods:
getProperty(configFile, prop)
Get a property from a config file by reading the config file until the desired property is found
setProperty(configFile, prop, value)
Set a property by updating the config file (requries a total rewrite of the config file)
getProperties(configFile)
Read all properties into a dictionary, which is returned to the user
setProperties(configFile, dict)
Overwrite the configFile with a new configFile generated from the dictionary provided
"""
def getProperties(configFile):
"""
dictionary getProperties(str)
This funciton reads the entire config file and builds a dictionary from the config file
Args:
configFile: The configuration file to read from
Returns:
dictionary: A list of key value pairs from the config file
"""
dict = {}
#For each line in the file
with open(configFile) as file:
for line in file:
#Remove leading and trailing whitespace
line = line.strip()
#If the line is a comment, skip
if line.startswith('#'):
continue
#Find the equals sign, if not present, skip the line
loc = line.find('=')
if loc == -1:
continue
#parse out the key and value
key = line[:loc]
value = line[loc+1:]
dict[key] = value
return dict
def setProperties(configFile, dict):
"""
void setProperties (str, dictionary)
This function iterates over the entire dictionary and saves each dictionary entry to the specified config file
Args:
configFile: The file to overwrite with the new configuration
dict: The dictionary to write
"""
#Overwrite the file
#Foreach key in dictionary write a new line
with open(configFile, 'w') as file:
for key in dict:
file.write(key + '=' + dict[key] + '\n')
def getProperty(configFile, prop):
"""
str getProperty(str, str)
This function searches a configFile for a specific property and returns its value
Args:
configFile: The configuration file to open
prop: The property to search for
Returns:
string: The property value if found or None for no value found
"""
retVal = None
#Foreach line in the file
with open(configFile) as file:
for line in file:
#Remove leading and trailing whitespace
line = line.strip()
#Ignore comment lines
if line.startswith('#'):
continue
#If the line is the desired property, parse and return
if line.startswith(prop):
retVal = line.replace(prop, '')
retVal = retVal.strip()
retVal = retVal[1:]
retVal = retVal.lstrip()
break
return retVal
def setProperty(configFile, prop, value):
"""
void setProperty(str, str, str)
This function searches a config file for the specified propery and updates its value if found.
If the specified property is not found, then a new line for the property will be created
Args:
configFile: The configuration file to open and update
prop: The property key to update
value: The new value for the property
"""
written = False
with open(configFile) as inFile:
#Create a temp file to copy into
tmpHandle, outPath = mkstemp()
with fdopen(tmpHandle, 'w') as outFile:
#Foreach line in the original file
for line in inFile:
#If it's the prop line, rewrite the prop line
if line.startswith(prop):
outFile.write(prop + '=' + value + '\n')
written = True
#Otherwise keep the line as is
else:
outFile.write(line)
#If no update was performed, then add a new line for the prop
if not written:
outFile.write(prop + ':' + value + '\n')
#Move from tmp to actual file
remove(configFile)
move(outPath, configFile)