Simplified applets
[gregoa/zavai.git] / zavai / conf.py
1 import os
2 import ConfigParser, StringIO
3
4 def read_config(rootDir = None, defaults = None, nick="octofuss"):
5     """
6     Read octofuss configuration, returning a ConfigParser object
7     """
8     if rootDir == None:
9         rootDir = os.environ.get(nick.upper() + "_CONFDIR", "/etc/" + nick)
10     files = []
11     def trytouse(path):
12         if os.path.exists(path):
13             files.append(path)
14
15     # Start with the main config file
16     trytouse(os.path.join(rootDir, nick + ".conf"))
17
18     # Add snippets found in rc.d style directory
19     subdir = os.path.join(rootDir, nick + ".conf.d")
20     if os.path.isdir(subdir):
21         for file in sorted(os.listdir(subdir)):
22             if file.startswith('#'): continue
23             if file.startswith('.'): continue
24             if file.endswith('~'): continue
25             if file.endswith('.bak'): continue
26             trytouse(os.path.join(subdir, file))
27
28     config = ConfigParser.ConfigParser()
29     if defaults != None:
30         infile = StringIO.StringIO(defaults)
31         config.readfp(infile, "defaults")
32     config.read(files)
33     return config