42
44
Raises a ConfigError on error.
44
46
if 'IVLECONF' in os.environ:
45
fname = os.path.join(os.environ['IVLECONF'], 'ivle.conf')
47
fname = os.path.join(os.environ['IVLECONF'])
46
48
if os.path.exists(fname):
48
if os.path.exists('/etc/ivle/ivle.conf'):
49
return '/etc/ivle/ivle.conf'
50
raise ConfigError("Could not find IVLE config file")
50
if os.path.exists('/etc/ivle'):
52
raise ConfigError("Could not find IVLE config directory")
54
def get_plugin(pluginstr):
55
plugin_path, classname = pluginstr.split('#')
56
# Load the plugin module from somewhere in the Python path
57
# (Note that plugin_path is a fully-qualified Python module name).
59
getattr(__import__(plugin_path, fromlist=[classname]), classname))
53
62
class Config(ConfigObj):
58
67
Automatically validates the file against the spec (found in
59
68
./ivle-spec.conf relative to this module).
61
def __init__(self, blank=False, *args, **kwargs):
70
def __init__(self, blank=False, plugins=True, *args, **kwargs):
62
71
"""Initialises a new Config object. Searches for the config file,
63
72
loads it, and validates it.
64
73
@param blank: If blank=True, will create a blank config instead, and
65
74
not search for the config file.
75
@param plugins: If True, will find and index plugins.
66
76
@raise ConfigError: If the config file cannot be found.
68
78
specfile = os.path.join(os.path.dirname(__file__), 'ivle-spec.conf')
70
80
super(Config, self).__init__(configspec=specfile, *args, **kwargs)
72
conffile = search_conffile()
82
confdir = search_confdir()
83
conffile = os.path.join(confdir, 'ivle.conf')
73
84
super(Config, self).__init__(infile=conffile, configspec=specfile,
75
86
# XXX This doesn't raise errors if it doesn't validate
76
87
self.validate(Validator())
92
self.plugin_configs = {}
93
# Go through the plugin config files, looking for plugins.
94
for pconfn in glob.glob(os.path.join(confdir, 'plugins.d/*.conf')):
95
pconf = ConfigObj(pconfn)
96
for plugin_section in pconf:
97
# We have a plugin path. Resolve it into a class...
98
plugin_path, plugin = get_plugin(plugin_section)
99
self.plugins[plugin_path] = plugin
100
# ... and add it to the registry.
101
self.plugin_configs[plugin] = pconf[plugin_section]
103
# Create a registry mapping plugin classes to paths.
104
self.reverse_plugins = dict([(v, k) for (k, v) in
105
self.plugins.items()])
107
# Create an index of plugins by base class.
108
self.plugin_index = {}
109
for plugin in self.plugins.values():
110
# Getmro returns a tuple of all the super-classes of the plugin
111
for base in inspect.getmro(plugin):
112
if base not in self.plugin_index:
113
self.plugin_index[base] = []
114
self.plugin_index[base].append(plugin)
78
116
def set_by_path(self, path, value=_NO_VALUE, comment=None):
79
117
"""Writes a value to an option, given a '/'-separated path.
80
118
@param path: '/'-separated path to configuration option.