r124 - branches/rewrite/src

Otavio Salvador partial-mirror-devel@lists.alioth.debian.org
Thu, 01 Jul 2004 21:21:02 -0600


Author: otavio
Date: Thu Jul  1 21:21:02 2004
New Revision: 124

Modified:
   branches/rewrite/src/Config.py
Log:
Added an Exception and did some code cleanup.

Modified: branches/rewrite/src/Config.py
==============================================================================
--- branches/rewrite/src/Config.py	(original)
+++ branches/rewrite/src/Config.py	Thu Jul  1 21:21:02 2004
@@ -17,9 +17,22 @@
 # $Id$
 
 from logging import *
-from ConfigParser import ConfigParser, DEFAULTSECT
+from ConfigParser import ConfigParser
 from sys import exit
 
+class InvalidOption(Exception):
+    """
+    Exception called when a invalid option is found in configuration
+    file.
+
+    Attributes:
+        section -- Where the invalid option was set;
+        option -- The name of invalid option.
+    """
+    def __init__(self, section, option):
+        self.section = section
+        self.option = option
+
 class Config(dict):
     """
     Store the configurations used by our system.
@@ -58,24 +71,21 @@
 	
 	# Read the default section.
         self['DEFAULT'] = {}
-        for item, value in conf.items(DEFAULTSECT):
+        for item, value in conf.items('DEFAULT'):
             if item not in Config.required_in_default:
-                error("[%s] is not allowed in default section of configuration file." % (item))
-                exit(1)
+                debug("[%s] is not allowed in default section." % (item))
+                raise InvalidOption('DEFAULT', item)
             self['DEFAULT'][item] = value
 	    
 	for section in conf.sections():
+            if section == 'DEFAULT': continue # already parsed
 	    self[section] = {}
 	    for item, value in conf.items(section):
-		# is this item from the default section?  if so, go on to the
-		# next item
-		if self['DEFAULT'].has_key(item):
-		    if self['DEFAULT'][item] == value:
-			continue
 		if item not in Config.allowed_in_backend:
 		    actually_its_ok = False
 		    for allowed_key in Config.allowed_in_backend:
-			# check the allowed_in_backend keys with @VARIABLES@
+			# check the allowed_in_backend keys with
+			# @VARIABLES@
 			if allowed_key.find('@') != -1:
 			    nonvar_length = allowed_key.find('@')
 			    if allowed_key[:nonvar_length] == item[:nonvar_length]:
@@ -83,14 +93,14 @@
 				actually_its_ok = True
 				break
 		    if not actually_its_ok:
-			error("[%s] is not allowed in a backend section of a config file (it was found in [%s])."
+			debug("[%s] is not allowed in a backend section (it was found in [%s])."
 			    % (item, section))
-			exit(1)
+                        raise InvalidOption(section, item)
 		self[section][item] = value
 		    
     def getValue(self, key, section='DEFAULT'):
-	# get a config value for a certain section.  if it's not specified,
-	# fall back to DEFAULT
+	# get a config value for a certain section.  if it's not
+	# specified, fall back to DEFAULT
 	if not self.has_key(section):
 	    error("no config section called [%s]." % (section))
 	    exit(1)