-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_parser.py
More file actions
41 lines (35 loc) · 1.14 KB
/
Copy pathconfig_parser.py
File metadata and controls
41 lines (35 loc) · 1.14 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
#!/usr/bin/env python
class ConfigError(Exception):
pass
class Config:
def __init__(self, config_path):
self.pool = {}
try:
for line in open(config_path, 'r'):
line = line.strip()
if line.startswith('#'):
continue
tmp = line.split('=')
if len(tmp) < 2:
continue
self.pool[tmp[0].strip()] = '='.join(tmp[1:]).strip()
except IOError:
raise ConfigError('can\'t open %s' % config_path)
def get_bool(self, key, default=False):
if not self.pool.get(key):
return default
return self.pool.get(key).lower() == 'on'
def get_str(self, key, default=None):
return self.pool.get(key) or default
def get_int(self, key, default=None):
v = self.pool.get(key)
if v and v.isdigit():
return int(v)
else:
return default
def get_list(self, key, default=None):
v = self.pool.get(key)
if v:
return [i.strip() for i in v.split(',')]
else:
return default