-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfigReader.py
101 lines (76 loc) · 3.69 KB
/
configReader.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
101
import re
import ConfigParser
class invalidConfig(Exception):
def __init__(self, key, line):
self.line = line+1
self.key = key
def __str__(self):
return "Invalid data on line {0}: {1}".format(self.line, self.key)
def CreateDefaultConfig():
Parser = ConfigParser.SafeConfigParser(allow_no_value = True)
Parser.add_section("Connection Info")
Parser.set("Connection Info", "nickname", "MyIRCBot")
Parser.set("Connection Info", "password", "foobar")
Parser.set("Connection Info", "ident", "MyIRCBot")
Parser.set("Connection Info", "realname", "RenolIRCBot")
Parser.set("Connection Info", "server", "")
Parser.set("Connection Info", "port", "6667")
Parser.add_section("Administration")
Parser.set("Administration", "bot operators", "")
Parser.set("Administration", "channels", "")
Parser.set("Administration", "command prefix", "=")
Parser.set("Administration", "logging level", "INFO")
Parser.add_section("Networking")
Parser.set("Networking", "force IPv6", "False")
Parser.set("Networking", "bind address", "")
return Parser
class Configuration():
def __init__(self):
self.config = None
self.configname = "config.cfg"
self.mandatoryVariables = {"Connection Info" : {"nickname" : True, "password" : True,
"ident" : True, "realname" : True, "server" : True,
"port" : True},
"Administration" : {"bot operators" : False, "channels" : False,
"command prefix" : True, "logging level" : True},
"Networking" : {"force ipv6" : True, "bind address" : False}
}
self.found = []
def loadConfig(self):
try:
conFile = open(self.configname, "r")
except IOError as error:
config = CreateDefaultConfig()
configFile = open(self.configname, "w")
config.write(configFile)
configFile.close()
raise RuntimeError("The '{0}' file was missing. A new config file has been created. "
"Please fill in the information.".format(self.configname))
else:
self.config = ConfigParser.SafeConfigParser()
self.config.readfp(conFile)
def check_options(self):
for section in self.mandatoryVariables:
options = self.mandatoryVariables[section]
for option in options:
val = self.config.get(section, option)
if options[option] == True and val == "":
raise RuntimeError("Option '{0}' in section '{1}' has no value, but is required to have one. "
"Please fill in the missing information.".format(option, section))
def getChannels(self):
chans = self.config.get("Administration", "channels").split(",")
newchans = []
for chan in chans:
chan = chan.strip()
if chans[0].isalnum():
newchans.append("#"+chan)
else:
newchans.append(chan)
return newchans
def getAdmins(self):
admins = self.config.get("Administration", "bot operators").split(",")
newadmins = []
for admin in admins:
admin = admin.strip()
newadmins.append(admin)
return newadmins