summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlec Warner <antarus@gentoo.org>2007-07-23 07:28:58 +0000
committerAlec Warner <antarus@gentoo.org>2007-07-23 07:28:58 +0000
commit6751c610e3c59caea7240d097d658ce16fc32392 (patch)
tree063b1745776885031e4cddfa2a7cf77ccbaa2c48
parentff9402db196bb97d86adf670318aa69967b258eb (diff)
downloadportage-6751c610e3c59caea7240d097d658ce16fc32392.tar.gz
portage-6751c610e3c59caea7240d097d658ce16fc32392.tar.bz2
portage-6751c610e3c59caea7240d097d658ce16fc32392.zip
Validator should be a function, not a class, fix spacing/tab issues. Fix parsing errors for KeyValuePairLoader. Add a GenericFileLoader class that uses more than 1 loader in a vain attempt to figure out what kind of file it is. Fix inheritance by renaming UserConfigKlass
svn path=/main/trunk/; revision=7368
-rw-r--r--pym/portage/env/config.py37
-rw-r--r--pym/portage/env/loaders.py40
-rw-r--r--pym/portage/env/validators.py21
3 files changed, 72 insertions, 26 deletions
diff --git a/pym/portage/env/config.py b/pym/portage/env/config.py
index bb63e35e8..c990d9f0e 100644
--- a/pym/portage/env/config.py
+++ b/pym/portage/env/config.py
@@ -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
"""
diff --git a/pym/portage/env/loaders.py b/pym/portage/env/loaders.py
index e80bce6de..ae9579a86 100644
--- a/pym/portage/env/loaders.py
+++ b/pym/portage/env/loaders.py
@@ -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
index 000000000..fb29b8d29
--- /dev/null
+++ b/pym/portage/env/validators.py
@@ -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