forked from hexparrot/mineos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf_reader.py
217 lines (200 loc) · 8.91 KB
/
conf_reader.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
Subclass of Configparser for sectionless configuration files.
Implements slicing as additional get/set methods.
"""
__author__ = "William Dizon"
__license__ = "GNU GPL v3.0"
__version__ = "0.6.0"
__email__ = "[email protected]"
import ConfigParser
class config_file_sectionless(object):
def __init__(self, filepath):
self.filepath = open(filepath, 'r')
self.fake_section = '[sectionless]\n'
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.filepath.close()
def readline(self):
if self.fake_section:
try:
return self.fake_section
finally:
self.fake_section = None
else:
return self.filepath.readline()
class config_file(ConfigParser.SafeConfigParser):
def __init__(self, filepath=None):
ConfigParser.SafeConfigParser.__init__(self, allow_no_value=True)
self.filepath = filepath
self.use_sections(True)
try:
self.read(self.filepath)
except ConfigParser.MissingSectionHeaderError:
self.use_sections(False)
with config_file_sectionless(self.filepath) as cf:
self.readfp(cf)
except TypeError:
if filepath is not None:
raise
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.commit()
def __getitem__(self, option):
if self._use_sections:
syntax_error = "config_file get syntax: " \
"var[:] or " \
"var['section'] or " \
"var['section':'option'] or " \
"var['section':'option':'defaultval']"
if type(option) is str:
try:
return dict(self.items(option))
except ConfigParser.NoSectionError:
raise KeyError("No section: %s" % option)
elif type(option) is slice:
if type(option.start) is str and type(option.stop) is str:
try:
return self.get(option.start, option.stop)
except ConfigParser.NoSectionError:
raise KeyError(option.start)
except ConfigParser.NoOptionError:
#__getitem__ cannot return None as default argument
#because it cannot distinguish between empty slice arg
if option.step is not None:
return option.step
else:
raise KeyError(option.start)
elif type(option.start) is str and \
option.stop is None and \
option.step is None:
try:
return dict(self.items(option.start))
except ConfigParser.NoSectionError:
raise KeyError("No section: %s" % option.start)
else:
from sys import maxint
if option.start is 0 and option.stop == maxint:
return {sec:dict(self.items(sec)) for sec in self.sections()}
elif type(option.start) is not str:
raise TypeError('First argument must be string not %s' % type(option.start))
else:
raise TypeError('Second argument must be string not %s' % type(option.stop))
else:
raise TypeError(syntax_error)
else:
syntax_error = "config_file get syntax: " \
"var[:] or " \
"var['option'] or " \
"var['option'::'defaultval']"
if type(option) is str:
try:
return self.get('sectionless', option)
except ConfigParser.NoOptionError:
raise KeyError(option)
elif type(option) is slice:
if type(option.start) is str and option.stop is None:
try:
return self.get('sectionless', option.start)
except ConfigParser.NoOptionError:
#__getitem__ cannot return None as default argument
#because it cannot distinguish between empty slice arg
if option.step is not None:
return option.step
else:
raise KeyError(option.start)
else:
raise SyntaxError(syntax_error)
else:
from sys import maxint
if option.start is 0 and option.stop == maxint and option.step is None:
return dict(self.items('sectionless'))
elif option.stop is not None:
raise SyntaxError(syntax_error)
else:
raise TypeError(syntax_error)
else:
raise TypeError(syntax_error)
def __setitem__(self, option, value):
if self._use_sections:
syntax_error = "config_file set syntax: " \
"var['section':'option'] = val"
if type(option) is slice:
if option.step is not None:
raise SyntaxError(syntax_error)
elif type(option.start) is str and type(option.stop) is str:
if option.step:
raise SyntaxError(syntax_error)
else:
if type(value) in (int,str):
try:
self.set(option.start, option.stop, str(value))
except ConfigParser.NoSectionError:
raise KeyError('No section called %s' % option.start)
else:
raise ValueError('Value may only be int or string')
else:
if type(option.start) is not str:
raise TypeError('First argument must be string not %s' % type(option.start))
else:
raise TypeError('Second argument must be string not %s' % type(option.stop))
else:
raise SyntaxError(syntax_error)
else:
syntax_error = "config_file set syntax: " \
"var['option'] = val"
if type(option) is str:
self.set('sectionless', str(option), str(value))
elif type(option) is slice:
raise SyntaxError(syntax_error)
else:
raise TypeError('Inappropriate argument type: %s' % type(option))
def __delitem__(self, option):
if self._use_sections:
syntax_error = "config_file del syntax: " \
"del var['section':'option']"
if type(option) is slice:
if option.step is not None:
raise SyntaxError(syntax_error)
elif option.stop is None:
raise SyntaxError(syntax_error)
elif type(option.start) is str and type(option.stop) is str:
try:
self.remove_option(option.start, option.stop)
except ConfigParser.NoSectionError:
raise KeyError(option.start)
elif type(option.start) is not str:
raise TypeError('Inappropriate argument type: %s' % type(option.start))
else:
raise TypeError('Inappropriate argument type: %s' % type(option.stop))
else:
raise SyntaxError(syntax_error)
else:
syntax_error = "config_file del syntax: " \
"del var['option']"
if type(option) is str:
self.remove_option('sectionless', str(option))
elif type(option) is slice:
raise SyntaxError(syntax_error)
else:
raise TypeError('Inappropriate argument type: %s' % type(option))
def commit(self):
if self._use_sections:
with open(self.filepath, 'wb') as configfile:
self.write(configfile)
else:
with open(self.filepath, "w") as configfile:
for k,v in self.items('sectionless'):
configfile.write("%s=%s\n" % (k.strip(), v.strip()))
def use_sections(self, value):
if value:
self.remove_section('sectionless')
self._use_sections = True
else:
try:
self.add_section('sectionless')
except ConfigParser.DuplicateSectionError:
pass
finally:
self._use_sections = False