-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
78 lines (68 loc) · 1.94 KB
/
config.py
File metadata and controls
78 lines (68 loc) · 1.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/urs/bin/env python
from ConfigParser import SafeConfigParser
from optparse import OptionParser
def _loadIni( path ):
config = SafeConfigParser()
config.read( path )
result = dict()
result[ 'host' ] = config.get( 'server', 'host' )
result[ 'port' ] = config.getint( 'server', 'port' )
result[ 'limit' ] = config.getint( 'server', 'limit' )
result[ 'welcome' ] = config.get( 'text', 'welcome' )
return result
def _loadOptions():
parser = OptionParser("%prog [-s HOST] [-p PORT] [-l LIMIT] [-w WELCOME]") # 1st argument is usage, %prog is replaced with sys.argv[0]
parser.add_option(
"-s", "--server",
dest = "host",
type = "string",
action = "store",
help = "IRC server address",
)
parser.add_option(
"-p", "--port",
dest = "port",
type = "int",
action = "store",
help = "IRC server port",
)
parser.add_option(
"-l", "--limit",
dest = "limit",
type = "int",
action = "store",
help = "maximum allowed connected users",
)
parser.add_option(
"-w", "--welcome",
dest = "welcome",
type = "string",
action = "store",
help = "welcome message",
)
options, _ = parser.parse_args()
result = dict()
for k, v in vars( options ).items():
if v is not None:
result[ k ] = v
return result
class Config( object ):
host = 'localhost'
port = 8080
limit = 5
welcome = 'Hello incoming user!'
def merge( self, d ):
if 'host' in d:
self.host = d[ 'host' ]
if 'port' in d:
self.port = d[ 'port' ]
if 'limit' in d:
self.limit = d[ 'limit' ]
if 'welcome' in d:
self.welcome = d[ 'welcome' ]
def loadConfig( path ):
d = _loadIni( path )
d.update( _loadOptions() )
config = Config()
config.merge( d )
return config