-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.py
More file actions
57 lines (49 loc) · 1.43 KB
/
Config.py
File metadata and controls
57 lines (49 loc) · 1.43 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
#!/usr/bin/python
#ConfigParser ,maybe we should use this
# by tianming 2013-06-26
class Config:
def __init__(self, file_path):
#if self._conf_file == "" and file_path != "":
self._conf_file = ""
self._conf_items = {}
self.__load_config(file_path)
#private function
def __load_config(self, file_path):
self._conf_file = file_path
try:
f = open(self._conf_file, "rb")
for i in f.readlines():
i1 = i.lstrip().rstrip()
if i1 == "" or i1[0] == '#':
continue
info = i.split("=")
if len(info) == 2:
self._conf_items[info[0].lstrip().rstrip()] = info[1].lstrip().rstrip()
f.close()
except Exception,e:
print Exception,":",e
def get_string(self, key, default_value):
if key in self._conf_items.keys():
return self._conf_items[key]
else:
return default_value
def get_int(self, key, default_value):
if key in self._conf_items.keys():
return int(self._conf_items[key])
else:
return default_value
def get_conf_items_copy(self):
return self._conf_items.copy()
#_conf_file = ""
#_conf_items = {}
#for debug and example
if __name__ == '__main__':
g_config = Config("./server.conf")
test_value1 = g_config.get_string("test_key1", "err")
print "test_value1=%s" %test_value1
test_value2 = g_config.get_int("test_key2", -1)
print "test_value2=%d" %test_value2
for key,value in g_config.get_conf_items_copy().items():
print "%s=%s" %(key,value)
else:
pass