r169 - in branches/rewrite: src tests/Config

Nat Budin partial-mirror-devel@lists.alioth.debian.org
Tue, 06 Jul 2004 20:35:03 -0600


Author: natbudin-guest
Date: Tue Jul  6 20:35:02 2004
New Revision: 169

Modified:
   branches/rewrite/src/Config.py
   branches/rewrite/tests/Config/test.py
Log:
Implemented basic testing for filters.  Did a little refactoring, but some of the code is extremely kludgy.  Going to refactor a bit.


Modified: branches/rewrite/src/Config.py
==============================================================================
--- branches/rewrite/src/Config.py	(original)
+++ branches/rewrite/src/Config.py	Tue Jul  6 20:35:02 2004
@@ -114,22 +114,68 @@
         'architectures': 'list',
         'backends': 'list',
         'distributions': 'list',
-        'filter': 'dict',
-        'filter_@BACKEND@': 'dict',
+        'filter': 'filter',
+        'filter_@BACKEND@': 'filter',
         'get_provides': 'boolean',
         'get_recommends': 'boolean',
         'get_suggests': 'boolean',
         'sections': 'list',
         }
 
-    def __castOption(self, option, value):
+    def __getSectionType(self, section):
+        # detect which config type this is
+        if 'backends' in self.options(section):
+            return 'merge'
+        elif 'server' in self.options(section):
+            return 'mirror'
+        else:
+            debug("Unknown section type in section [%s]." % (section))
+            raise InvalidSection(section)
+
+    def __allowedInSection(self, section):
+        ### MAJOR HACK.  Remove this when refactoring.
+        sect_type = self.__getSectionType(section)
+        if sect_type == 'mirror':
+            allowed_in_section = self._allowed_in_mirror_backend
+        elif sect_type == 'merge':
+            allowed_in_section = self._allowed_in_merge_backend
+        return allowed_in_section
+        
+    def __parseVariableOptions(self, section, item):
+        allowed_in_section = self.__allowedInSection(section)
+        for allowed_key in allowed_in_section:
+            if allowed_key.find('@') != -1:
+                left_length = allowed_key.find('@')
+                right_length = allowed_key[left_length+1:].find('@') + left_length + 2
+                if (allowed_key[:left_length] == item[:left_length]
+                    and allowed_key[right_length:] == item[right_length:]):
+                    # found it!
+                    variable = allowed_key[left_length:right_length]
+                    match = item[left_length:len(item)-(len(allowed_key)-right_length)]
+                    return allowed_key, variable, match
+                                
+    def __castOption(self, section, option, value):
+        if option not in self._options_with_type:
+            try:
+                allowed_key, variable, match = self.__parseVariableOptions(section, option)
+                option = allowed_key
+            except TypeError:
+                pass
         if option in self._options_with_type:
             if self._options_with_type[option] == 'list':
                 return value.split()
             elif self._options_with_type[option] == 'boolean':
                 return bool(value)
-#            elif self._options_with_type[option] == 'dict':
-#                return map(value.split())
+            elif self._options_with_type[option] == 'filter':
+                opts = value.split()
+                ret = {}
+                for opt in opts:
+                    key, val = opt.split(':')
+                    if key not in self._allowed_in_filter_field:
+                        debug("[%s] is not a filter field (found in option [%s] in section [%s])." % (key, option, section))
+                        raise InvalidOption(section, option)
+                    ret[key] = val
+                return ret
         else:
             return value
 
@@ -156,38 +202,24 @@
             if section == 'GLOBAL': continue
 	    self.confs[section] = {}
 
-            ###HACK.  Split this out later into subclasses.
-            # detect which config type this is
-            if 'backends' in self.options(section):
-                allowed_in_section = self._allowed_in_merge_backend
-            elif 'server' in self.options(section):
-                allowed_in_section = self._allowed_in_mirror_backend
-            else:
-                debug("Unknown section type in section [%s]." % (section))
-                raise InvalidSection(section)
+            allowed_in_section = self.__allowedInSection(section)
             
 	    for item, value in self.items(section):
-                value = self.__castOption(item, value)
+                value = self.__castOption(section, item, value)
 		if item not in allowed_in_section:
-		    for allowed_key in allowed_in_section:
-			# check the allowed_in_backend keys with
-			# @VARIABLES@
-			if allowed_key.find('@') != -1:
-			    left_length = allowed_key.find('@')
-			    right_length = allowed_key[left_length+1:].find('@') + left_length + 2
-			    if (allowed_key[:left_length] == item[:left_length]
-                                and allowed_key[right_length:] == item[right_length:]):
-				# found it!
-                                variable = allowed_key[left_length:right_length]
-                                match = item[left_length:len(item)-(len(allowed_key)-right_length)]
-                                if variable == '@BACKEND@':
-                                    if match in self.sections():
-                                        break
-                                else:
-                                    print("You found a bug: [%s] matches unknown variable [%s]! "
-                                          "Please report it." % (item, variable))
-                                    exit(1)
-                    else:
+                    try:
+                        allowed_key, variable, match = self.__parseVariableOptions(section, item)
+                        if variable == '@BACKEND@':
+                            if match not in self.sections():
+                                # oh god this is such a hack.  going to
+                                # refactor this soon
+                                raise TypeError
+                        else:
+                            print("You found a bug: [%s] matches unknown variable [%s]! "
+                                  "Please report it." % (item, variable))
+                            exit(1)
+                    except TypeError:
+                        # __parseVariableOptions didn't return a sequence
 			debug("[%s] is not allowed in a backend section (it was found in [%s])."
                               % (item, section))
                         raise InvalidOption(section, item)

Modified: branches/rewrite/tests/Config/test.py
==============================================================================
--- branches/rewrite/tests/Config/test.py	(original)
+++ branches/rewrite/tests/Config/test.py	Tue Jul  6 20:35:02 2004
@@ -56,4 +56,5 @@
 test('Missing Required configuration file', 'missing-required.conf', 'RequiredOptionMissing')
 test('Invalid Option configuration file', 'invalid-option.conf', 'InvalidOption')
 test('Section Types configuration file', 'section-types.conf', 'InvalidOption')
+test('Invalid Filter configuration file', 'invalid-filter.conf', 'InvalidOption')
 test('Invalid Section configuration file', 'invalid-section.conf', 'InvalidSection')