]>
Commit | Line | Data |
---|---|---|
931c88a4 | 1 | # Erebus IRC bot - Author: John Runyon |
2 | # "Config" class (reading/providing access to bot.config) | |
3 | ||
b25d4368 | 4 | import ConfigParser |
5 | ||
6 | class Config(object): | |
b25d4368 | 7 | def __init__(self, filename, writeout=True): |
f0118d05 | 8 | self.__dict__['config'] = ConfigParser.RawConfigParser() |
b25d4368 | 9 | self.__dict__['filename'] = filename |
10 | self.__dict__['writeout'] = writeout | |
11 | self.config.read(filename) | |
12 | ||
13 | def __getattr__(self, key): | |
14 | return self.config.get('erebus', key) | |
15 | ||
16 | def __setattr__(self, key, value): | |
17 | self.config.set('erebus', key, value) | |
18 | ||
93a06339 | 19 | def __getitem__(self, section): #!! READ-ONLY !! |
20 | return {item: self.config.get(section, item) for item in self.config.options(section)} | |
21 | ||
b2a896c8 | 22 | def level(self, cmd): |
23 | return self.config.get('levels', cmd) | |
24 | ||
25 | def setlevel(self, cmd, level): | |
26 | self.config.set('levels', cmd, level) | |
27 | ||
db50981b | 28 | def items(self, section='erebus'): |
29 | return self.config.items(section) | |
b25d4368 | 30 | |
c715235b | 31 | def get(self, section, key, default=None): |
32 | try: | |
33 | return self.config.get(section, key) | |
34 | except: | |
35 | return default | |
93a06339 | 36 | def getboolean(self, section, key): |
37 | val = self.get(section, key, False) | |
38 | if val == False or val == "0" or val.lower() == "false" or val.strip() == "": | |
39 | return False | |
40 | else: | |
41 | return True | |
c0eee1b4 | 42 | |
43 | def set(self, section, key, value): | |
44 | self.config.set(section, key, value) | |
45 | ||
b25d4368 | 46 | def write(self): |
a12f7519 | 47 | with open(self.filename, 'wb') as configfile: |
b25d4368 | 48 | self.config.write(configfile) |
a4eacae2 | 49 | |
b25d4368 | 50 | def __del__(self): |
51 | if self.writeout: self.write() | |
52 | ||
93a06339 | 53 | def setup(fn='bot.config', writeout=True): |
54 | return Config(fn, writeout) | |
b25d4368 | 55 | |
56 | if __name__ == '__main__': | |
57 | import sys | |
58 | cfg = Config(sys.argv[1], False) | |
59 | ||
0f8352dd | 60 | for s in cfg.config.sections(): |
61 | for k, v in cfg.items(s): | |
93a06339 | 62 | print "[%r][%r] = %r" % (s, k, v) |
0f8352dd | 63 | # for k, v in cfg.items(): |
64 | # print 'erebus.'+k, '=', v |