Mercurial > p > roundup > code
comparison roundup/config.py @ 481:a261b0bbba43 config-0-4-0-branch
Fixed a number of minor syntax issues & error handling.
| author | Titus Brown <titus@users.sourceforge.net> |
|---|---|
| date | Thu, 03 Jan 2002 04:28:21 +0000 |
| parents | a5cd27d33516 |
| children | fdee2ff82b40 |
comparison
equal
deleted
inserted
replaced
| 480:a5cd27d33516 | 481:a261b0bbba43 |
|---|---|
| 1 import sys | 1 import sys |
| 2 import os | 2 import os |
| 3 import ConfigParser | 3 import ConfigParser |
| 4 import string | 4 import string |
| 5 | |
| 6 class Error(Exception): | |
| 7 pass | |
| 5 | 8 |
| 6 def debug_mode(): | 9 def debug_mode(): |
| 7 """ | 10 """ |
| 8 Returns the basic debug mode/level. | 11 Returns the basic debug mode/level. |
| 9 """ | 12 """ |
| 29 | 32 |
| 30 filenames_to_check = [] # list of files to check: | 33 filenames_to_check = [] # list of files to check: |
| 31 if os.environ.has_key('ROUNDUP_CONF'): | 34 if os.environ.has_key('ROUNDUP_CONF'): |
| 32 filenames_to_check.append(os.environ['ROUNDUP_CONF']) | 35 filenames_to_check.append(os.environ['ROUNDUP_CONF']) |
| 33 | 36 |
| 34 filenames_to_check.append('%s/share/roundup/roundup.rc' % (sys.prefix,)) | 37 filenames_to_check.append('%s/share/roundup/roundup.rc'%(sys.prefix,)) |
| 35 | 38 |
| 36 found = 0 | |
| 37 for filename in filenames_to_check: | 39 for filename in filenames_to_check: |
| 38 if os.path.exists(filename): | 40 if os.path.exists(filename): |
| 39 found = 1 | |
| 40 c.read(filename) | 41 c.read(filename) |
| 41 break | 42 break |
| 42 | 43 else: |
| 43 if not found: | 44 raise Error("could not find configuration file") |
| 44 assert 0, "could not find config file!" | |
| 45 | 45 |
| 46 if debug_mode(): | 46 if debug_mode(): |
| 47 print 'Loaded configuration from "%s".' % (filename,) | 47 print 'Loaded configuration from "%s".'%(filename,) |
| 48 | 48 |
| 49 # we also want to give a base path for other config file names; | 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. | 50 # for the moment, make it the base path of the filename we chose. |
| 51 base_path = os.path.dirname(filename) | 51 base_path = os.path.dirname(filename) |
| 52 | 52 |
| 53 return RoundupBaseConfig(c, base_path) | 53 return BaseConfig(c, base_path) |
| 54 | 54 |
| 55 class RoundupBaseConfig: | 55 class BaseConfig: |
| 56 """ | 56 """ |
| 57 A container for the installation-wide roundup configuration. | 57 A container for the installation-wide roundup configuration. |
| 58 """ | 58 """ |
| 59 def __init__(self, c, base_path): | 59 def __init__(self, c, base_path): |
| 60 assert isinstance(c, ConfigParser.ConfigParser) | 60 assert isinstance(c, ConfigParser.ConfigParser) |
| 77 defaults_dictionary = { 'roundup_conf_dir' : self.base_path } | 77 defaults_dictionary = { 'roundup_conf_dir' : self.base_path } |
| 78 | 78 |
| 79 c = ConfigParser.ConfigParser(defaults_dictionary) | 79 c = ConfigParser.ConfigParser(defaults_dictionary) |
| 80 c.read(filename) | 80 c.read(filename) |
| 81 | 81 |
| 82 return RoundupInstancesConfig(c, filename) | 82 return InstancesConfig(c, filename) |
| 83 | 83 |
| 84 class RoundupInstancesConfig: | 84 class InstancesConfig: |
| 85 """ | 85 """ |
| 86 A container for the installation-wide list of instances. | 86 A container for the installation-wide list of instances. |
| 87 """ | 87 """ |
| 88 def __init__(self, c, filename=""): | 88 def __init__(self, c, filename=""): |
| 89 assert isinstance(c, ConfigParser.ConfigParser) | 89 assert isinstance(c, ConfigParser.ConfigParser) |
| 95 | 95 |
| 96 for name in c.sections(): | 96 for name in c.sections(): |
| 97 dir = c.get(name, 'homedir') | 97 dir = c.get(name, 'homedir') |
| 98 | 98 |
| 99 if instance_names.has_key(dir) or instance_dirs.has_key(name): | 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,)) | 100 error_text = 'ERROR: dir/name correspondence is not unique (%s)'%(self.filename,) |
| 101 assert 0, "%s:%s" % (dir, name,) | 101 raise ValueError(error_text) |
| 102 | |
| 103 | 102 |
| 104 instance_dirs[name] = dir | 103 instance_dirs[name] = dir |
| 105 instance_names[dir] = name | 104 instance_names[dir] = name |
| 106 | 105 |
| 107 self.instance_dirs = instance_dirs | 106 self.instance_dirs = instance_dirs |
| 156 if __name__ == '__main__': | 155 if __name__ == '__main__': |
| 157 base_config = load_base_config() | 156 base_config = load_base_config() |
| 158 instances_config = base_config.load_instances_config() | 157 instances_config = base_config.load_instances_config() |
| 159 | 158 |
| 160 for k in instances_config.get_instance_names(): | 159 for k in instances_config.get_instance_names(): |
| 161 print "%s:%s" % (k, instances_config.get_instance_dir(k),) | 160 print "%s:%s"%(k, instances_config.get_instance_dir(k),) |
