Validator should be a function, not a class, fix spacing/tab issues. Fix parsing...
authorAlec Warner <antarus@gentoo.org>
Mon, 23 Jul 2007 07:28:58 +0000 (07:28 -0000)
committerAlec Warner <antarus@gentoo.org>
Mon, 23 Jul 2007 07:28:58 +0000 (07:28 -0000)
svn path=/main/trunk/; revision=7368

pym/portage/env/config.py
pym/portage/env/loaders.py
pym/portage/env/validators.py [new file with mode: 0644]

index bb63e35e8d1d45a643c30ff9d145298ffab29690..c990d9f0effe3b80661ac6ecc7bd909415e48495 100644 (file)
@@ -6,14 +6,12 @@
 from UserDict import UserDict
 from portage.env.loaders import KeyListFileLoader, KeyValuePairFileLoader, ItemFileLoader
 
-class UserConfigKlass(UserDict, object):
+class ConfigLoaderKlass(UserDict, object):
        """
        A base class stub for things to inherit from.
        Users may want a non-file backend.
        """
        
-       data = {}
-       
        def __init__(self, loader):
                """
                @param loader: A class that has a load() that returns two dicts
@@ -34,9 +32,32 @@ class UserConfigKlass(UserDict, object):
        def __iter__(self):
                return iter(self.data)
 
-class PackageKeywordsFile(UserConfigKlass):
+class GenericFile(UserDict):
+       """
+       Inherits from ConfigLoaderKlass, attempts to use all known loaders
+       until it gets <something> in data.  This is probably really slow but is
+       helpful when you really have no idea what you are loading (hint hint the file
+       should perhaps declare  what type it is? ;)
+       """
+       
+       loaders = [KeyListFileLoader, KeyValuePairFileLoader, ItemFileLoader]
+       
+       def __init__(self, filename):
+               UserDict.__init__(self)
+               self.filename = filename
+       
+       def load(self):
+               for loader in self.loaders:
+                       l = loader(self.filename, None)
+                       data, errors = l.load()
+                       if len(data) and not len(errors):
+                               (self.data, self.errors) = (data, errors)
+                               return
+
+
+class PackageKeywordsFile(ConfigLoaderKlass):
        """
-       Inherits from UserConfigKlass; implements a file-based backend.
+       Inherits from ConfigLoaderKlass; implements a file-based backend.
        """
 
        default_loader = KeyListFileLoader
@@ -45,7 +66,7 @@ class PackageKeywordsFile(UserConfigKlass):
                super(PackageKeywordsFile, self).__init__(
                        self.default_loader(filename, validator=None))
        
-class PackageUseFile(UserConfigKlass):
+class PackageUseFile(ConfigLoaderKlass):
        """
        Inherits from PackageUse; implements a file-based backend.  Doesn't handle recursion yet.
        """
@@ -55,7 +76,7 @@ class PackageUseFile(UserConfigKlass):
                super(PackageUseFile, self).__init__(
                        self.default_loader(filename, validator=None))
        
-class PackageMaskFile(UserConfigKlass):
+class PackageMaskFile(ConfigLoaderKlass):
        """
        A class that implements a file-based package.mask
        
@@ -73,7 +94,7 @@ class PackageMaskFile(UserConfigKlass):
                super(PackageMaskFile, self).__init__(
                        self.default_loader(filename, validator=None))
 
-class PortageModulesFile(UserConfigKlass):
+class PortageModulesFile(ConfigLoaderKlass):
        """
        File Class for /etc/portage/modules
        """
index e80bce6ded4fc9f5aed2b82011010daf4cf7c5d0..ae9579a86b0d6b4c74694b6bf5e5d3323b68f14a 100644 (file)
@@ -56,11 +56,10 @@ class DataLoader(object):
                if f is None:
                        # if they pass in no validator, just make a fake one
                        # that always returns true
-                       class AlwaysTrue(object):
-                               def validate(self, key):
-                                       return True
-                       f = AlwaysTrue()
-               self._validator = f
+                       def validate(key):
+                               return True
+                       f = validate
+               self._validate = f
 
        def load(self):
                """
@@ -147,10 +146,10 @@ class ItemFileLoader(FileLoader):
                                % (line_num + 1, line))
                        return
                key = split[0]
-               if not self._validator.validate(key):
+               if not self._validate(key):
                        errors.setdefault(self.fname, []).append(
-                       "Validation failed at line: %s, data %s"
-                       % (line_num + 1, key))
+                               "Validation failed at line: %s, data %s"
+                               % (line_num + 1, key))
                        return
                data[key] = None
 
@@ -176,15 +175,15 @@ class KeyListFileLoader(FileLoader):
                split = line.split()
                if len(split) < 2:
                        errors.setdefault(self.fname, []).append(
-                       "Malformed data at line: %s, data: %s"
-                       % (line_num + 1, line))
+                               "Malformed data at line: %s, data: %s"
+                               % (line_num + 1, line))
                        return
                key = split[0]
                value = split[1:]
-               if not self._validator.validate(key):
+               if not self._validate(key):
                        errors.setdefault(self.fname, []).append(
-                       "Validation failed at line: %s, data %s"
-                       % (line_num + 1, key))
+                               "Validation failed at line: %s, data %s"
+                               % (line_num + 1, key))
                        return
                if key in data:
                        data[key].append(value)
@@ -216,15 +215,20 @@ class KeyValuePairFileLoader(FileLoader):
                split = line.split('=')
                if len(split) < 2:
                        errors.setdefault(self.fname, []).append(
-                       "Malformed data at line: %s, data %s"
-                       % (line_num + 1, line))
+                               "Malformed data at line: %s, data %s"
+                               % (line_num + 1, line))
                        return
                key = split[0]
                value = split[1:]
-               if not self._validator.validate(key):
+               if not key:
+                       errors.setdefault(self.fname, []).append(
+                               "Malformed key at line: %s, key %s"
+                               % (line_num + 1, key))
+                       return
+               if not self._validate(key):
                        errors.setdefault(self.fname, []).append(
-                       "Validation failed at line: %s, data %s"
-                       % (line_num + 1, key))
+                               "Validation failed at line: %s, data %s"
+                               % (line_num + 1, key))
                        return
                if key in data:
                        data[key].append(value)
diff --git a/pym/portage/env/validators.py b/pym/portage/env/validators.py
new file mode 100644 (file)
index 0000000..fb29b8d
--- /dev/null
@@ -0,0 +1,21 @@
+# validators.py Portage File Loader Code
+# Copyright 2007 Gentoo Foundation
+# $Id$
+
+from portage.dep import isvalidatom
+
+ValidAtomValidator = isvalidatom
+
+def PackagesFileValidator(atom):
+       """ This function mutates atoms that begin with - or *
+           It then checks to see if that atom is valid, and if
+           so returns True, else it returns False.
+           
+           Args:
+               atom: a string representing an atom such as sys-apps/portage-2.1
+       """
+       if atom.startswith("*") or atom.startswith("-"):
+               atom = atom[1:]
+       if not isvalidatom(atom):
+               return False
+       return True