Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4f2f801591
Fetching contributors…

Cannot retrieve contributors at this time

80 lines (72 sloc) 2.456 kb
#!/usr/bin/python
"""A module wrapper around the config files for pyBawt"""
__all__ = []
# This is tremendous hax, just a piece of scaffold to avoid breaking the old
# API
# Also, bail the fuck out if the config is incomplete
import logging
import os
class InvalidConfig(Exception):
pass
class NoConfigFile(Exception):
pass
CONFIG_FILE = 'pyBawt.conf'
logging.info("Commencing config read from %s" % (os.path.join(os.getcwd(), CONFIG_FILE)))
config = {}
boolvalues = { 'true' : True,
'1' : True,
'yes' : True,
'false': False,
'0' : False,
'no' : False }
def to_bool(val):
return boolvalues[val.lower()]
def to_list(val):
return list(map(lambda x: x.strip(), val.split(',')))
def passthru(val):
return val
# A tuple of tuples with a key and an transformer
# Should modules be able to inject their own keys? Or should we just accept whatever and deal with it later?
keys = (('host', passthru),
('ssl' , to_bool),
('nick', passthru),
('port', int),
('auth_host', passthru),
('auth_hash', passthru),
('channels' , to_list),
('nickserv_nick', passthru),
('nickserv_pass', passthru)
)
try:
lineno = 0
with open(CONFIG_FILE) as fh:
for line in fh:
lineno += 1
line = line.strip()
if line.startswith("#"):
# Ignore comments
continue
if not line:
continue
try:
key, value = map(lambda x: x.strip(),line.split('=', 1))
config[key] = value
except ValueError:
raise InvalidConfig, "Invalid key value pair at line %i" % (lineno)
except IOError:
logging.fatal("Couldn't open %s" % (CONFIG_FILE))
if not config:
logging.error("Couldn't open config file")
raise NoConfigFile
# FIXME distinguish between not there and inaccessible
for name, transformer in keys:
__all__.append(name)
try:
# This is fucked XXX FIXME
#__dict__[name] = transformer(config[name])
exec('%s = %s' % (name, repr(transformer(config[name]))))
except ValueError: #Probably port didn't translate
# Pull lineno, spit out useful error
raise InvalidConfig
except KeyError: #Didn't have a required key
raise InvalidConfig, "Missing required key: %s" % (name)
Jump to Line
Something went wrong with that request. Please try again.