From cf26094fc275e187490981ae56b77b13c56ce36f Mon Sep 17 00:00:00 2001 From: Alec Warner Date: Sat, 31 Mar 2007 19:42:35 +0000 Subject: [PATCH] After talking to marienz, decide that 1 class is better than 4, also realize that UserDict has most of the dict methods exposed, so drop them. Also add a LoaderError Exception. Still to do? add a validation callable into the loader to validate data. svn path=/main/trunk/; revision=6328 --- pym/portage/env/config.py | 131 +++++++------------------------------ pym/portage/env/loaders.py | 17 +++++ 2 files changed, 39 insertions(+), 109 deletions(-) diff --git a/pym/portage/env/config.py b/pym/portage/env/config.py index 3bac9a3a5..ca533286f 100644 --- a/pym/portage/env/config.py +++ b/pym/portage/env/config.py @@ -6,109 +6,50 @@ from UserDict import UserDict from portage.env.loaders import KeyListFileLoader, KeyValuePairFileLoader, AtomFileLoader -class PackageKeywords(UserDict): +class UserConfigKlass(UserDict,object): """ - A base class stub for things to inherit from; some people may want a database based package.keywords or something - - Internally dict has pairs of the form - {'cpv':['keyword1','keyword2','keyword3'...] + 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 + the first being a data dict, the second being a dict of errors. + """ self._loader = loader def load(self): + """ + Load the data from the loader. + + @throws LoaderError: + """ + self.data, self.errors = self._loader.load() - def iteritems(self): - return self.data.iteritems() - - def keys(self): - return self.data.keys() - - def __contains__(self, other): - return other in self.data - - def __hash__( self ): - return self.data.__hash__() - -class PackageKeywordsFile(PackageKeywords): +class PackageKeywordsFile(UserConfigKlass): """ - Inherits from PackageKeywords; implements a file-based backend. Doesn't handle recursion yet. + Inherits from UserConfigKlass; implements a file-based backend. """ default_loader = KeyListFileLoader def __init__(self, filename): - PackageKeywords.__init__(self, self.default_loader(filename)) - -class PackageUse(UserDict): - """ - A base class stub for things to inherit from; some people may want a database based package.keywords or something - - Internally dict has pairs of the form - {'cpv':['flag1','flag22','flag3'...] - """ - - data = {} + super(PackageKeywordsFile,self).__init__(self.default_loader(filename)) - def __init__(self, loader): - self._loader = loader - - def load( self): - self.data, self.errors = self._loader.load() - - def iteritems(self): - return self.data.iteritems() - - def __hash__(self): - return hash(self.data) - - def __contains__(self, other): - return other in self.data - - def keys(self): - return self.data.keys() - -class PackageUseFile(PackageUse): +class PackageUseFile(UserConfigKlass): """ Inherits from PackageUse; implements a file-based backend. Doesn't handle recursion yet. """ default_loader = KeyListFileLoader def __init__(self, filename): - PackageUse.__init__(self, self.default_loader(filename)) - -class PackageMask(UserDict): - """ - A base class for Package.mask functionality - """ - data = {} + super(PackageUseFile,self).__init__(self.default_loader(filename)) - def __init__(self, loader): - self._loader = loader - - def load(self): - self.data, self.errors = self._loader.load() - - def iteritems(self): - return self.data.iteritems() - - def __hash__(self): - return hash(self.data) - - def __contains__(self, other): - return other in self.data - - def keys(self): - return self.data.keys() - - def iterkeys(self): - return self.data.iterkeys() - -class PackageMaskFile(PackageMask): +class PackageMaskFile(UserConfigKlass): """ A class that implements a file-based package.mask @@ -123,37 +64,9 @@ class PackageMaskFile(PackageMask): default_loader = AtomFileLoader def __init__(self, filename): - PackageMask.__init__(self, self.default_loader(filename)) - -class PortageModules(UserDict): - """ - Base Class for user level module over-rides - """ - - data = {} - - def __init__(self, loader): - self._loader = loader - - def load(self): - self.data, self.errors = self._loader.load() - - def iteritems(self): - return self.data.iteritems() - - def __hash__(self): - return self.data.__hash__() - - def __contains__(self, key): - return key in self.data - - def keys(self): - return self.data.keys() - - def iterkeys(self): - return self.data.iterkeys() + super(PackageMaskFile,self).__init__(self.default_loader(filename)) -class PortageModulesFile(PortageModules): +class PortageModulesFile(UserConfigKlass): """ File Class for /etc/portage/modules """ @@ -161,4 +74,4 @@ class PortageModulesFile(PortageModules): default_loader = KeyValuePairFileLoader def __init__(self, filename): - PortageModules.__init__(self, self.default_loader(filename)) + super(PortageModulesFile,self).__init__(self.default_loader(filename)) diff --git a/pym/portage/env/loaders.py b/pym/portage/env/loaders.py index 33a8c3d4f..18acd457c 100644 --- a/pym/portage/env/loaders.py +++ b/pym/portage/env/loaders.py @@ -5,6 +5,22 @@ import os +class LoaderError(Exception): + + def __init__(self, resource, error_msg): + """ + @param resource: Resource that failed to load (file/sql/etc) + @type resource: String + @param error_msg: Error from underlying Loader system + @type error_msg: String + """ + + self.resource + + def __str__(self): + return "Failed while loading resource: %s, error was: %s" % ( + resource, error_msg) + def RecursiveFileLoader(filename): """ If filename is of type file, return [filename] @@ -164,3 +180,4 @@ class KeyValuePairFileLoader(DataLoader): else: data[key] = value return (data,errors) + -- 2.26.2