~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/config/__init__.py

setup: Removed 'config' mode. It's now standalone.
setup.configure: Renamed to setup/ivle-config.
    Made executable and standalone Python script (with a Main function).

You now run ./setup/ivle-config as a standalone script, not 'setup.py config'.
This will eventually get moved out but can't right now due to having to
generate the Trampoline conf.h file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
"""
23
23
 
24
24
import os
25
 
import glob
26
 
import inspect
27
25
 
28
26
from configobj import ConfigObj
29
27
from validate import Validator
36
34
    """
37
35
    pass
38
36
 
39
 
def search_confdir():
 
37
def search_conffile():
40
38
    """
41
39
    Search for the config file, and return it as a filename.
42
 
    1. Environment var IVLECONF (path to directory)
43
 
    2. /etc/ivle/ivle.conf
 
40
    1. Environment var IVLECONF (full filename).
 
41
    2. ./etc/ivle.conf
 
42
    3. /etc/ivle/ivle.conf
44
43
    Raises a ConfigError on error.
45
44
    """
46
45
    if 'IVLECONF' in os.environ:
47
 
        fname = os.path.join(os.environ['IVLECONF'])
 
46
        fname = os.environ['IVLECONF']
48
47
        if os.path.exists(fname):
49
48
            return fname
50
 
    if os.path.exists('/etc/ivle'):
51
 
        return '/etc/ivle'
52
 
    raise ConfigError("Could not find IVLE config directory")
53
 
 
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).
58
 
    return (plugin_path,
59
 
            getattr(__import__(plugin_path, fromlist=[classname]), classname))
 
49
    if os.path.exists('./etc/ivle.conf'):
 
50
        return './etc/ivle.conf'
 
51
    if os.path.exists('/etc/ivle/ivle.conf'):
 
52
        return '/etc/ivle/ivle.conf'
 
53
    raise ConfigError("Could not find IVLE config file")
60
54
 
61
55
_NO_VALUE = []
62
56
class Config(ConfigObj):
67
61
    Automatically validates the file against the spec (found in
68
62
    ./ivle-spec.conf relative to this module).
69
63
    """
70
 
    def __init__(self, blank=False, plugins=True, *args, **kwargs):
 
64
    def __init__(self, blank=False, *args, **kwargs):
71
65
        """Initialises a new Config object. Searches for the config file,
72
66
        loads it, and validates it.
73
67
        @param blank: If blank=True, will create a blank config instead, and
74
68
        not search for the config file.
75
 
        @param plugins: If True, will find and index plugins.
76
69
        @raise ConfigError: If the config file cannot be found.
77
70
        """
78
71
        specfile = os.path.join(os.path.dirname(__file__), 'ivle-spec.conf')
79
72
        if blank:
80
73
            super(Config, self).__init__(configspec=specfile, *args, **kwargs)
81
74
        else:
82
 
            confdir = search_confdir()
83
 
            conffile = os.path.join(confdir, 'ivle.conf')
 
75
            conffile = search_conffile()
84
76
            super(Config, self).__init__(infile=conffile, configspec=specfile,
85
77
                                         *args, **kwargs)
86
78
            # XXX This doesn't raise errors if it doesn't validate
87
79
            self.validate(Validator())
88
80
 
89
 
            if not plugins:
90
 
                return
91
 
            self.plugins = {}
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]
102
 
 
103
 
            # Create a registry mapping plugin classes to paths.
104
 
            self.reverse_plugins = dict([(v, k) for (k, v) in
105
 
                                         self.plugins.items()])
106
 
 
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)
115
 
 
116
81
    def set_by_path(self, path, value=_NO_VALUE, comment=None):
117
82
        """Writes a value to an option, given a '/'-separated path.
118
83
        @param path: '/'-separated path to configuration option.