Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 76 lines (68 sloc) 2.387 kb
df02500 Richo Healey initial import
richo authored
1 #!/usr/bin/python
2 """A module wrapper around the config files for pyBawt"""
3 __all__ = []
4
5 # This is tremendous hax, just a piece of scaffold to avoid breaking the old
6 # API
7 # Also, bail the fuck out if the config is incomplete
8
9 import logging
10 import os
365e885 Richo Healey Move more exceptions into lib, catch config errors
richo authored
11 from lib import *
df02500 Richo Healey initial import
richo authored
12
13
14
15 CONFIG_FILE = 'pyBawt.conf'
16 logging.info("Commencing config read from %s" % (os.path.join(os.getcwd(), CONFIG_FILE)))
17 config = {}
18 boolvalues = { 'true' : True,
19 '1' : True,
20 'yes' : True,
21 'false': False,
22 '0' : False,
23 'no' : False }
24 def to_bool(val):
25 return boolvalues[val.lower()]
26 def to_list(val):
27 return list(map(lambda x: x.strip(), val.split(',')))
28 def passthru(val):
29 return val
30 # A tuple of tuples with a key and an transformer
d952c33 Richo Healey Added more notes
richo authored
31 # Should modules be able to inject their own keys? Or should we just accept whatever and deal with it later?
df02500 Richo Healey initial import
richo authored
32 keys = (('host', passthru),
33 ('ssl' , to_bool),
34 ('nick', passthru),
35 ('port', int),
36 ('auth_host', passthru),
37 ('auth_hash', passthru),
38 ('channels' , to_list),
39 ('nickserv_nick', passthru),
40 ('nickserv_pass', passthru)
41 )
42
5021eae Richo Healey Catch lack of config, bail appropriately
richo authored
43 try:
96cadca Richo Healey Dump lineno when invalid key
richo authored
44 lineno = 0
5021eae Richo Healey Catch lack of config, bail appropriately
richo authored
45 with open(CONFIG_FILE) as fh:
46 for line in fh:
96cadca Richo Healey Dump lineno when invalid key
richo authored
47 lineno += 1
5021eae Richo Healey Catch lack of config, bail appropriately
richo authored
48 line = line.strip()
49 if line.startswith("#"):
50 # Ignore comments
51 continue
52 if not line:
53 continue
54 try:
55 key, value = map(lambda x: x.strip(),line.split('=', 1))
f32b2e5 Richo Healey Spit out useful error message when we have to bail on config
richo authored
56 config[key] = (value, lineno)
5021eae Richo Healey Catch lack of config, bail appropriately
richo authored
57 except ValueError:
96cadca Richo Healey Dump lineno when invalid key
richo authored
58 raise InvalidConfig, "Invalid key value pair at line %i" % (lineno)
5021eae Richo Healey Catch lack of config, bail appropriately
richo authored
59 except IOError:
60 logging.fatal("Couldn't open %s" % (CONFIG_FILE))
df02500 Richo Healey initial import
richo authored
61 if not config:
62 logging.error("Couldn't open config file")
63 raise NoConfigFile
64 # FIXME distinguish between not there and inaccessible
65 for name, transformer in keys:
66 __all__.append(name)
67 try:
f32b2e5 Richo Healey Spit out useful error message when we have to bail on config
richo authored
68 val, line = config[name]
69 exec('%s = %s' % (name, repr(transformer(val))))
df02500 Richo Healey initial import
richo authored
70
71 except ValueError: #Probably port didn't translate
4f2f801 Richo Healey Final todo
richo authored
72 # Pull lineno, spit out useful error
f32b2e5 Richo Healey Spit out useful error message when we have to bail on config
richo authored
73 raise InvalidConfig, "Counldn't parse %s on line %i" % (val, line)
51f732e Richo Healey Barf when missing config
richo authored
74 except KeyError: #Didn't have a required key
75 raise InvalidConfig, "Missing required key: %s" % (name)
Something went wrong with that request. Please try again.