1
|
|
|
import os |
2
|
|
|
import json |
3
|
|
|
from conferror import ConfError |
4
|
|
|
class ConfManager: |
5
|
|
|
""" |
6
|
|
|
Singleton class. Opens and parses a JSON-formatted conf file from (generally) the running user's home folder. Looks for .pybotrc. |
7
|
|
|
This allows each thread to know only its own network name, and always get back the information specified for that network from the confman. |
8
|
|
|
""" |
9
|
|
|
|
10
|
|
|
def __init__(self,conf=None): |
11
|
|
|
if conf is not None: |
12
|
|
|
try: |
13
|
|
|
self.conf_file = open(os.path.expanduser(conf)) |
14
|
|
|
except IOError: |
15
|
|
|
raise ConfError("could not open conf file '"+os.path.expanduser(conf)+"'") |
16
|
|
|
if conf is None: |
17
|
|
|
if os.environ.has_key('HOME'): |
18
|
|
|
try: |
19
|
|
|
self.conf_path = os.environ['HOME'] + '/.pybotrc' |
20
|
|
|
self.conf_file = open(self.conf_path) |
21
|
|
|
except IOError: |
22
|
|
|
raise ConfError("could not open conf file '"+self.conf_path+"'") |
23
|
|
|
else: # lines of with os.environ.has_key |
24
|
|
|
try: |
25
|
|
|
self.conf_file = open('.pybotrc') |
26
|
|
|
except IOError: |
27
|
|
|
self.conf_path = os.environ['HOME'] + '/.pybotrc' |
28
|
|
|
try: |
29
|
|
|
#self.conf_path = os.environ['HOME'] + conf |
30
|
|
|
self.conf_file = open(self.conf_path) |
31
|
|
|
except IOError: |
32
|
|
|
raise ConfError("could not open conf file '"+self.conf_path+"'") |
33
|
|
|
|
34
|
|
|
self.parsed = json.load(self.conf_file) |
35
|
|
|
|
36
|
|
|
def getDBType(self): |
37
|
|
|
try: |
38
|
|
|
db_type = self.parsed["__pybot_conf"]["db_type"] |
39
|
|
|
except: |
40
|
|
|
return "mysql" |
41
|
|
|
|
42
|
|
|
return db_type |
43
|
|
|
|
44
|
|
|
def getOwner(self, net): |
45
|
|
|
return self.parsed[net]["owner"] |
46
|
|
|
|
47
|
|
|
def getTimeout(self, net): |
48
|
|
|
try: |
49
|
|
|
return int(self.parsed[net]["timeout"]) |
50
|
|
|
except: |
51
|
|
|
return 120 |
52
|
|
|
|
53
|
|
|
def getIRCPass(self, net): |
54
|
|
|
return self.parsed[net]["ircpass"] |
55
|
|
|
|
56
|
|
|
def getDBPass(self, net): |
57
|
|
|
return self.parsed[net]["dbpass"] |
58
|
|
|
|
59
|
|
|
def getDBName(self, net): |
60
|
|
|
return self.parsed[net]["dbname"] |
61
|
|
|
|
62
|
|
|
def getDBUsername(self, net): |
63
|
|
|
return self.parsed[net]["dbusername"] |
64
|
|
|
|
65
|
|
|
def getNumChannels(self, net): |
66
|
|
|
return len(self.parsed[net]["channels"]) |
67
|
|
|
|
68
|
|
|
def getNick(self, net): |
69
|
|
|
return self.parsed[net]["nick"] |
70
|
|
|
|
71
|
|
|
def getChannels(self, net): |
72
|
|
|
return self.parsed[net]["channels"] |
73
|
|
|
|
74
|
|
|
def getNetworks(self): |
75
|
|
|
l = list() |
76
|
|
|
for n in self.parsed.iterkeys(): |
77
|
|
|
if n != "__pybot_conf": |
78
|
|
|
l.append(n) |
79
|
|
|
return l |
80
|
|
|
|
81
|
|
|
def getNumNets(self): |
82
|
|
|
i = 0 |
83
|
|
|
for n in self.parsed.iterkeys(): |
84
|
|
|
if n != "__pybot_conf": |
85
|
|
|
i += 1 |
86
|
|
|
return i |
87
|
|
|
|
88
|
|
|
# deprecated |
89
|
|
|
def getNetwork(self): |
90
|
|
|
pass |
91
|
|
|
|
92
|
|
|
def getPort(self,net): |
93
|
|
|
return self.parsed[net]["port"] |
94
|
|
|
|