-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigobject.py
33 lines (28 loc) · 962 Bytes
/
configobject.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
### Another config file handling. the file name could be arbitrary.
class ConfigObject(object):
"""usage: conf = ConfigObject('alarm.conf')
conf.oss_trap_ip
"""
def __init__(self,cfile=''):
if cfile:
self.read(cfile)
def read(self,confile):
try:
execfile(confile,globals(),self.__dict__)
except IOError,e:
print "Config File not found:%s" % e
def getall(self):
"""return a dict include all configuration"""
return self.__dict__
def items(self):
return self.__dict__.items()
def get(self,key,notfound=None):
"""return the value for 'key' or notfound if key not exist"""
return self.__dict__.get(key,notfound)
def __repr__(self):
import pprint
return pprint.pformat(self.__dict__)
#### or
#conf = {} or class conf:pass
#execfile('file.conf',globals(),conf)
#conf[xxx]