forked from markmbadham/pythoncourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config01.py
47 lines (40 loc) · 1.3 KB
/
config01.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
FILENAME = 'config.text'
import re
class Config(object):
def __init__(self,filename=FILENAME):
self.config = {}
self.filename = filename
self.get_config()
def get_config(self):
config = {}
for line in open(self.filename):
match = re.search(r'^\s*(\w+)\s*=\s*(\w+)\s*$',line)
if match:
key, val = match.groups()
config[key] = val
self.config = config
def __getattr__(self,attr):
if attr != 'config' and attr in self.config:
return self.config.get(attr)
else:
raise AttributeError,\
"%s has no attribute %s" %(self,attr)
def __setattr__(self,attr,val):
if attr != 'config' and attr in self.config:
self.config[attr] = val
else:
super(Config, self).__setattr__(attr, val)
def write_config(self, filename=None):
filename = filename if filename else self.filename
f = open(filename, 'w')
for item in self.config.items():
f.write('%s = %s\n' % item)
f.close()
if __name__ == '__main__':
config = Config()
print config.config
print config.port
config.port = 10001
print config.port
config.write_config()
print open(FILENAME).read()