1 # synarere -- a highly modular and stable IRC bot.
2 # Copyright (C) 2010 Michael Rodriguez.
3 # Rights to this code are documented in docs/LICENSE.
5 '''Configuration parser.'''
7 # Import required Python module.
10 # Import required source modules.
11 import logger
, irc
, var
13 class Exception(Exception):
16 class ConfigBlock(object):
17 def __init__ (self
, label
, values
={}):
21 # Copy over any values given during initialization
23 self
.vars[key
] = values
[key
]
25 def add (self
, name
, value
):
26 self
.vars[name
] = value
28 def get (self
, name
, defval
=None):
29 return self
.vars.get(name
, defval
)
32 def __init__(self
, file):
36 def rehash(self
, on_sighup
):
37 '''Rehash configuration and change synarere to fit the new conditions.'''
39 logger
.info('Rehashing configuration %s' % ('due to SIGHUP.' if on_sighup
else ''))
43 '''Parse our file, and put the data into a dictionary.'''
47 # Attempt to open the file.
49 fh
= open(self
.file, 'r')
51 raise Exception(os
.strerror(e
.args
[0]))
56 for line
in fh
.xreadlines():
57 for cno
, c
in enumerate(line
):
59 # Increment the line number.
62 if c
== '#': # Comment until EOL.
64 if c
== ':': # Block label.
65 label
= line
[: cno
].strip()
66 self
.blocks
.append(ConfigBlock(label
))
67 if c
== '=': # Variable.
68 if not self
.blocks
: # Skip this line, as no block label was given yet.
70 varname
= line
[:cno
].strip()
71 varval
= line
[cno
+ 1:].strip()
72 self
.blocks
[-1].add(varname
, varval
)
75 # Close the file handle
78 def xget(self
, block
, variable
=None):
80 Return whatever is in block:variable. If variable is None,
81 we will iterate over mutiple blocks, thus allowing us to
82 return multiple values from multiple blocks.
85 if block
not in set(b
.label
for b
in self
.blocks
):
86 raise Exception('Block %s not found.' % block
)
90 if variable
is None: # Just get blocks by this name
92 else: # Get a member of blocks by this name.
95 def get(self
, block
, variable
=None):
97 Call our iterating generator (xget) and just store all its
98 results into a list to return all at once.
101 return [b
for b
in self
.xget(block
, variable
)]