source: TI00-utils/trunk/ndg/utils/config.py @ 1828

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI00-utils/trunk/ndg/utils/config.py
Revision 1828, 2.5 KB checked in by spascoe, 13 years ago (diff)

Changed config reading rules. You can now set NDG_HOME to override $HOME/.ndg.
This will be useful for making test fixtures, among other things.

Line 
1"""
2Routines for reading the NDG configuration file(s).
3
4All packages can import this module to access the system's NDG
5configation.  Conguration files are read as follows
6
7 1. SafeConfigParser is subclassed so that any call to the read() or
8    readfp() methods examines the section 'ndg.utils.config' for
9    'include' definitions.  Each whitespace-separated string defined
10    as include is treated as a path to another configuration file
11    which will be passed to read().  Paths are interpreted relative to
12    the config file currently being parsed.
13 2. /etc/ndg.conf is read if available
14 3. $NDG_HOME/ndg.conf is read if available.
15 4. NDG_HOME defaults to $HOME/.ndg if unset.
16 
17For example:
18
19  /etc/ndg.conf:
20  [ndg.utils.config]
21  include = /etc/ndg/myconfig.conf
22    ndg_extra.conf
23
24would parse /etc/ndg/myconfig.conf and /etc/ndg_extra.conf.
25
26
27@author: Stephen Pascoe
28"""
29
30from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
31import os
32
33
34GLOBAL_CONFIG_FILE = '/etc/ndg.conf'
35CONFIG_SECTION = 'ndg.utils.config'
36
37class NdgConfigParser(SafeConfigParser):
38   
39    def __init__(self, configSection, defaults=None):
40        """
41        @param configSection: The section name to read 'include' definitions from.
42        """
43        self._included = []
44        self.configSection = configSection
45        SafeConfigParser.__init__(self, defaults)
46
47    def read(self, filenames, include=True):
48        if isinstance(filenames, basestring):
49            filenames = [filenames]
50        for filename in filenames:
51            SafeConfigParser.read(self, [filename])
52            if include:
53                self.include(os.path.abspath(os.path.dirname(filename)))
54
55    def readfp(self, fp):
56        SafeConfigParser.readfp(self, fp)
57        self.include()
58
59    def include(self, relativeDir=''):
60        """
61        @param relativeDir: include definitions are resolved relative to this directory.
62        """
63
64        try:
65            paths = self.get(self.configSection, 'include').split()
66        except NoSectionError, NoOptionError:
67            return
68
69        for path in paths:
70            if not os.path.isabs(path):
71                path = os.path.join(relativeDir, path)
72            if path in self._included:
73                continue
74            self.read([path], include=False)
75            self._included.append(path)     
76
77config = NdgConfigParser(CONFIG_SECTION)
78NDG_HOME = os.getenv('NDG_HOME', os.path.join(os.getenv('HOME'), '.ndg'))
79config.read([GLOBAL_CONFIG_FILE, os.path.join(NDG_HOME, 'ndg.conf')])
80
Note: See TracBrowser for help on using the repository browser.