-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfman.py
100 lines (81 loc) · 2.97 KB
/
confman.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import json
from conferror import ConfError
class ConfManager:
"""
Singleton class. Opens and parses a JSON-formatted conf file from (generally) the running user's home folder. Looks for .pybotrc.
This allows each thread to know only its own network name, and always get back the information specified for that network from the confman.
"""
def __init__(self, conf=None):
if conf is not None:
try:
self.conf_file = open(os.path.expanduser(conf))
except IOError:
raise ConfError(
"could not open conf file '" +
os.path.expanduser(conf) +
"'")
if conf is None:
if 'HOME' in os.environ:
try:
self.conf_path = os.environ['HOME'] + '/.pybotrc'
self.conf_file = open(self.conf_path)
except IOError:
raise ConfError(
"could not open conf file '" + self.conf_path + "'")
else: # lines of with os.environ.has_key
try:
self.conf_file = open('.pybotrc')
except IOError:
self.conf_path = os.environ['HOME'] + '/.pybotrc'
try:
#self.conf_path = os.environ['HOME'] + conf
self.conf_file = open(self.conf_path)
except IOError:
raise ConfError(
"could not open conf file '" + self.conf_path + "'")
self.parsed = json.load(self.conf_file)
def getDBType(self):
try:
db_type = self.parsed["__pybot_conf"]["db_type"]
except BaseException:
return "mysql"
return db_type
def getOwner(self, net):
return self.parsed[net]["owner"]
def getTimeout(self, net):
try:
return int(self.parsed[net]["timeout"])
except BaseException:
return 120
def getIRCPass(self, net):
return self.parsed[net]["ircpass"]
def getDBPass(self, net):
return self.parsed[net]["dbpass"]
def getDBName(self, net):
return self.parsed[net]["dbname"]
def getDBUsername(self, net):
return self.parsed[net]["dbusername"]
def getNumChannels(self, net):
return len(self.parsed[net]["channels"])
def getNick(self, net):
return self.parsed[net]["nick"]
def getChannels(self, net):
return self.parsed[net]["channels"]
def getNetworks(self):
l = list()
for n in list(self.parsed.keys()):
if n != "__pybot_conf":
l.append(n)
return l
def getNumNets(self):
i = 0
for n in list(self.parsed.keys()):
if n != "__pybot_conf":
i += 1
return i
# deprecated
def getNetwork(self):
pass
def getPort(self, net):
return self.parsed[net]["port"]