Mercurial > p > roundup > code
comparison roundup/config.py @ 480:a5cd27d33516 config-0-4-0-branch
Initial ConfigParser implementation.
| author | Titus Brown <titus@users.sourceforge.net> |
|---|---|
| date | Thu, 03 Jan 2002 02:12:05 +0000 |
| parents | |
| children | a261b0bbba43 |
comparison
equal
deleted
inserted
replaced
| 479:a038b872ab8b | 480:a5cd27d33516 |
|---|---|
| 1 import sys | |
| 2 import os | |
| 3 import ConfigParser | |
| 4 import string | |
| 5 | |
| 6 def debug_mode(): | |
| 7 """ | |
| 8 Returns the basic debug mode/level. | |
| 9 """ | |
| 10 return os.environ.get('ROUNDUP_DEBUG', 0) | |
| 11 | |
| 12 def load_base_config(): | |
| 13 """ | |
| 14 Loads the base configuration for Roundup. | |
| 15 """ | |
| 16 | |
| 17 c = ConfigParser.ConfigParser() | |
| 18 | |
| 19 ## | |
| 20 ## CTB: this is where to search for all overrides, including | |
| 21 ## system-specific files, registry settings, etc. | |
| 22 ## | |
| 23 | |
| 24 # For the moment, search for the config file in | |
| 25 # | |
| 26 # %(sys.prefix)s/share/roundup/roundup.rc, | |
| 27 # | |
| 28 # with ROUNDUP_CONF overriding it. | |
| 29 | |
| 30 filenames_to_check = [] # list of files to check: | |
| 31 if os.environ.has_key('ROUNDUP_CONF'): | |
| 32 filenames_to_check.append(os.environ['ROUNDUP_CONF']) | |
| 33 | |
| 34 filenames_to_check.append('%s/share/roundup/roundup.rc' % (sys.prefix,)) | |
| 35 | |
| 36 found = 0 | |
| 37 for filename in filenames_to_check: | |
| 38 if os.path.exists(filename): | |
| 39 found = 1 | |
| 40 c.read(filename) | |
| 41 break | |
| 42 | |
| 43 if not found: | |
| 44 assert 0, "could not find config file!" | |
| 45 | |
| 46 if debug_mode(): | |
| 47 print 'Loaded configuration from "%s".' % (filename,) | |
| 48 | |
| 49 # we also want to give a base path for other config file names; | |
| 50 # for the moment, make it the base path of the filename we chose. | |
| 51 base_path = os.path.dirname(filename) | |
| 52 | |
| 53 return RoundupBaseConfig(c, base_path) | |
| 54 | |
| 55 class RoundupBaseConfig: | |
| 56 """ | |
| 57 A container for the installation-wide roundup configuration. | |
| 58 """ | |
| 59 def __init__(self, c, base_path): | |
| 60 assert isinstance(c, ConfigParser.ConfigParser) | |
| 61 self.conf = c | |
| 62 self.base_path = base_path | |
| 63 | |
| 64 def get(self, group, attr): | |
| 65 return self.conf.get(group, attr) | |
| 66 | |
| 67 def load_instances_config(self): | |
| 68 filename = string.strip(self.conf.get('base', 'instances')) | |
| 69 | |
| 70 # if it looks like an absolute path, leave it alone; otherwise, | |
| 71 # add on the base path. | |
| 72 if filename[0] == '/' or filename[0] == '\\': | |
| 73 pass | |
| 74 else: | |
| 75 filename = os.path.normpath(self.base_path + '/' + filename) | |
| 76 | |
| 77 defaults_dictionary = { 'roundup_conf_dir' : self.base_path } | |
| 78 | |
| 79 c = ConfigParser.ConfigParser(defaults_dictionary) | |
| 80 c.read(filename) | |
| 81 | |
| 82 return RoundupInstancesConfig(c, filename) | |
| 83 | |
| 84 class RoundupInstancesConfig: | |
| 85 """ | |
| 86 A container for the installation-wide list of instances. | |
| 87 """ | |
| 88 def __init__(self, c, filename=""): | |
| 89 assert isinstance(c, ConfigParser.ConfigParser) | |
| 90 self.conf = c | |
| 91 self.filename = filename | |
| 92 | |
| 93 instance_names = {} | |
| 94 instance_dirs = {} | |
| 95 | |
| 96 for name in c.sections(): | |
| 97 dir = c.get(name, 'homedir') | |
| 98 | |
| 99 if instance_names.has_key(dir) or instance_dirs.has_key(name): | |
| 100 sys.stderr.write('ERROR: dir/name correspondence is not unique (%s)' % (self.filename,)) | |
| 101 assert 0, "%s:%s" % (dir, name,) | |
| 102 | |
| 103 | |
| 104 instance_dirs[name] = dir | |
| 105 instance_names[dir] = name | |
| 106 | |
| 107 self.instance_dirs = instance_dirs | |
| 108 self.instance_names = instance_names | |
| 109 | |
| 110 def get_instance_names(self): | |
| 111 return self.instance_dirs.keys() | |
| 112 | |
| 113 def get_instance_name(self, dir): | |
| 114 return self.instance_names[dir] | |
| 115 | |
| 116 def get_instance_dir(self, name): | |
| 117 return self.instance_dirs[name] | |
| 118 | |
| 119 def load_instance_config(self, name): | |
| 120 instance_dir = self.get_instance_dir(name) | |
| 121 | |
| 122 defaults_file = self.conf.get(name, 'defaults') | |
| 123 config_file = self.conf.get(name, 'config') | |
| 124 | |
| 125 defaults_dictionary = { 'homedir' : instance_dir, | |
| 126 'instance_name' : name, | |
| 127 } | |
| 128 | |
| 129 | |
| 130 c = ConfigParser.ConfigParser(defaults_dictionary) | |
| 131 c.read(defaults_file) | |
| 132 c.read(config_file) | |
| 133 | |
| 134 return InstanceConfig(c, name, instance_dir) | |
| 135 | |
| 136 class InstanceConfig: | |
| 137 """ | |
| 138 A container for each per-instance configuration. | |
| 139 """ | |
| 140 | |
| 141 def __init__(self, c, instanceName, instanceDirectory): | |
| 142 assert isinstance(c, ConfigParser.ConfigParser) | |
| 143 self.conf = c | |
| 144 self.name = instanceName | |
| 145 self.directory = instanceDirectory | |
| 146 | |
| 147 def get_name(self): | |
| 148 return self.name | |
| 149 | |
| 150 def get_directory(self): | |
| 151 return self.directory | |
| 152 | |
| 153 def get(self, group, attr): | |
| 154 return self.conf.get(group, attr) | |
| 155 | |
| 156 if __name__ == '__main__': | |
| 157 base_config = load_base_config() | |
| 158 instances_config = base_config.load_instances_config() | |
| 159 | |
| 160 for k in instances_config.get_instance_names(): | |
| 161 print "%s:%s" % (k, instances_config.get_instance_dir(k),) |
