From 6751c610e3c59caea7240d097d658ce16fc32392 Mon Sep 17 00:00:00 2001 From: Alec Warner Date: Mon, 23 Jul 2007 07:28:58 +0000 Subject: 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 --- pym/portage/env/config.py | 37 +++++++++++++++++++++++++++++-------- pym/portage/env/loaders.py | 40 ++++++++++++++++++++++------------------ pym/portage/env/validators.py | 21 +++++++++++++++++++++ 3 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 pym/portage/env/validators.py (limited to 'pym') 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 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 -- cgit v1.2.3-1-g7c22