-
Notifications
You must be signed in to change notification settings - Fork 0
/
configfile.py
110 lines (96 loc) · 3.75 KB
/
configfile.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
#!/usr/bin/env python
# Get and set parameters in a configuration file (e.g. /etc/hostapd.conf)
import re
import os
# Assumptions (all derived from looking at a sample hostapd.conf file):
#
# A valid parameter name is [a-zA-Z0-9_]+
#
# A valid parameter value is everything after the '=' to the end of the line,
# whitespace trimmed on the ends. Can contain whitespace, ., :, etc. Can't
# handle a comment at the end of a setting; it will be taken as part of the
# setting.
#
# A "parameter setting" is:
# - optional [\t ]*
# - valid parameter name
# - optional [\t ]*
# - '='
# - valid parameter value
#
# A "commented out parameter setting" is:
# - optional [\t ]*
# - '#'
# - "parameter setting"
def paramGet(configFileName, paramName):
'''
Read parameter setting from config file
'''
configFile = open(configFileName) # IOError
# process line-by-line
for line in configFile:
# re.match only works at beginning of line (we want that)
m = re.match('[\t ]*' + paramName + '[\t ]*=[\t ]*(.*?)[\t ]*\n', line)
if m:
return m.group(1)
# not found
return None
def paramSet(configFileName, paramName, paramValue):
'''
Set parameter in config file
'''
configFileNameNew = configFileName + '.new'
# We generally make two passes through the file:
#
# Pass 1:
# If we find the parameter getting set (uncommented), we delete that
# line and replace it with a new line with the new setting. Copy the
# rest of the file then return (do not do pass 2).
#
# Pass 2:
# Otherwise, find the first line with a setting for the parameter
# commented out. Insert a new line with the parameter setting, but
# leave the commented out setting. Copy the rest of the file then
# return. If the parameter is not found (commented out), append the
# setting at the end of the file.
paramSet = False
# pass 1: process line-by-line
configFile = open(configFileName) # IOError
configFileNew = open(configFileNameNew, 'w') # IOError
for line in configFile:
if not paramSet:
# .match only works at beginning of line (we want that)
# optional tabs/spaces, 'param=', setting
m = re.match('[\t ]*' + paramName + '[\t ]*=[\t ]*(.*?)[\t ]*\n', line)
if m:
# found an uncommented setting for the parameter
# replace with the new setting
line = paramName + '=' + str(paramValue) + '\n'
paramSet = True
configFileNew.write(line)
configFileNew.close()
configFile.close()
# pass 1 done: if we set the parameter, then we are done.
# If we did not set the parameter, do pass #2.
if not paramSet:
configFile = open(configFileName) # IOError
configFileNew = open(configFileNameNew, 'w') # IOError
for line in configFile:
if not paramSet:
# .match only works at beginning of line (we want that)
# '#', optional tabs/spaces, 'param=', setting
m = re.match('[\t ]*#[\t ]*' + paramName + '[\t ]*=[\t ]*(.*?)[\t ]*\n', line)
if m:
# found a commented setting for the parameter
# insert a line with the new setting
configFileNew.write(paramName + '=' + str(paramValue) + '\n')
paramSet = True
configFileNew.write(line)
# append setting to end of file if not yet set
if not paramSet:
configFileNew.write(paramName + '=' + str(paramValue) + '\n')
paramSet = True
configFileNew.close()
configFile.close()
# replace old file with updated one
os.rename(configFileNameNew, configFileName)