1 # Erebus IRC bot - Author: John Runyon
2 # "Config" class (reading/providing access to bot.config)
4 from __future__
import print_function
7 if sys
.version_info
.major
< 3:
10 import configparser
as ConfigParser
13 def __init__(self
, filename
, writeout
=True):
14 self
.__dict
__['config'] = ConfigParser
.RawConfigParser()
15 self
.__dict
__['filename'] = filename
16 self
.__dict
__['writeout'] = writeout
17 self
.config
.read(filename
)
19 def __getattr__(self
, key
):
20 return self
.config
.get('erebus', key
)
22 def __setattr__(self
, key
, value
):
23 self
.config
.set('erebus', key
, value
)
25 def __getitem__(self
, section
): #!! READ-ONLY !!
26 return {item: self.config.get(section, item) for item in self.config.options(section)}
29 return self
.config
.get('levels', cmd
)
31 def setlevel(self
, cmd
, level
):
32 self
.config
.set('levels', cmd
, level
)
34 def items(self
, section
='erebus'):
35 return self
.config
.items(section
)
37 def get(self
, section
, key
, default
=None):
39 return self
.config
.get(section
, key
)
42 def getboolean(self
, section
, key
):
43 val
= self
.get(section
, key
, False)
44 if val
== False or val
== "0" or val
.lower() == "false":
49 def set(self
, section
, key
, value
):
50 self
.config
.set(section
, key
, value
)
51 if self
.writeout
: self
.write()
54 with open(self
.filename
, 'wb') as configfile
:
55 self
.config
.write(configfile
)
58 if self
.writeout
: self
.write()
60 if __name__
== '__main__':
63 cfg
= Config(sys
.argv
[1], False)
65 cfg
= Config('bot.config', False)
67 for s
in cfg
.config
.sections():
68 for k
, v
in cfg
.items(s
):
69 print("[%r][%r] = %r" % (s
, k
, v
))