Fix minor issues with loaders in the static file loader, fix quoting and import style...
authorAlec Warner <antarus@gentoo.org>
Fri, 7 Sep 2007 10:17:26 +0000 (10:17 -0000)
committerAlec Warner <antarus@gentoo.org>
Fri, 7 Sep 2007 10:17:26 +0000 (10:17 -0000)
svn path=/main/trunk/; revision=7753

pym/portage/env/loaders.py
pym/portage/news.py
pym/portage/sets/files.py

index ae9579a86b0d6b4c74694b6bf5e5d3323b68f14a..e7f5289a7054f95a78066d148607de280fbc086f 100644 (file)
@@ -67,6 +67,40 @@ class DataLoader(object):
                """
                raise NotImplementedError("Please override in a subclass")
 
+class EnvLoader(DataLoader):
+       """ Class to access data in the environment """
+       def __init__(self, validator):
+               DataLoader.__init__(self, validator)
+
+       def load(self):
+               return os.environ
+
+class TestTextLoader(DataLoader):
+       """ You give it some data, it 'loads' it for you, no filesystem access
+       """
+       def __init__(self, validator):
+               DataLoader.__init__(self, validator)
+               self.data = {}
+               self.errors = {}
+
+       def setData(self, text):
+               """Explicitly set the data field
+               Args:
+                       text - a dict of data typical of Loaders
+               Returns:
+                       None
+               """
+               if isinstance(text, dict):
+                       self.data = text
+               else:
+                       raise ValueError("setData requires a dict argument")
+
+       def setErrors(self, errors):
+               self.errors = errors
+       
+       def load(self):
+               return (self.data, self.errors)
+
 
 class FileLoader(DataLoader):
        """ Class to access data in files """
@@ -234,4 +268,3 @@ class KeyValuePairFileLoader(FileLoader):
                        data[key].append(value)
                else:
                        data[key] = value
-
index 702a377a54687431fb88077dcec6d8c2e6b165d1..dff66e3da5daafbe842edaf2ce8a5ce44da42ea5 100644 (file)
@@ -3,12 +3,13 @@
 # Distributed under the terms of the GNU General Public License v2
 # $Id$
 
+import os
+import re
 from portage.const import INCREMENTALS, PROFILE_PATH, NEWS_LIB_PATH
-from portage.util import ensure_dirs, apply_permissions
+from portage.util import ensure_dirs, apply_permissions, normalize_path
 from portage.data import portage_gid
 from portage.locks import lockfile, unlockfile, lockdir, unlockdir
 from portage.exception import FileNotFound
-import os, re
 
 class NewsManager(object):
        """
@@ -23,7 +24,7 @@ class NewsManager(object):
        
        """
 
-       TIMESTAMP_FILE = "news-timestamp"
+       TIMESTAMP_FILE = 'news-timestamp'
 
        def __init__(self, portdb, vardb, NEWS_PATH, UNREAD_PATH, LANGUAGE_ID='en'):
                self.NEWS_PATH = NEWS_PATH
@@ -37,8 +38,7 @@ class NewsManager(object):
                self.portdb = portdb
 
                portdir = portdb.porttree_root
-               profiles_base = os.path.join(portdir, "profiles") + os.path.sep
-               from portage.util import normalize_path
+               profiles_base = os.path.join(portdir, 'profiles') + os.path.sep
                profile_path = normalize_path(
                        os.path.realpath(portdb.mysettings.profile_path))
                if profile_path.startswith(profiles_base):
@@ -89,7 +89,7 @@ class NewsManager(object):
                                updates.append(item)
                del path
                
-               path = os.path.join(self.UNREAD_PATH, "news-"+repoid+".unread")
+               path = os.path.join(self.UNREAD_PATH, 'news-' + repoid + '.unread')
                try:
                        unread_lock = lockfile(path)
                        if not os.path.exists(path):
@@ -97,9 +97,9 @@ class NewsManager(object):
                                open(path, "w")
                        # Ensure correct perms on the unread file.
                        apply_permissions( filename=path,
-                               uid=int(self.config["PORTAGE_INST_UID"]), gid=portage_gid, mode=0664)
+                               uid=int(self.config['PORTAGE_INST_UID']), gid=portage_gid, mode=0664)
                        # Make sure we have the correct permissions when created
-                       unread_file = open(path, "a")
+                       unread_file = open(path, 'a')
 
                        for item in updates:
                                unread_file.write(item.name + "\n")
@@ -108,7 +108,7 @@ class NewsManager(object):
                        unlockfile(unread_lock)
                
                # Touch the timestamp file
-               f = open(timestamp_file, "w")
+               f = open(timestamp_file, 'w')
                f.close()
 
        def getUnreadItems(self, repoid, update=False):
@@ -122,13 +122,13 @@ class NewsManager(object):
                if update:
                        self.updateItems(repoid)
                
-               unreadfile = os.path.join(self.UNREAD_PATH, "news-"+repoid+".unread")
+               unreadfile = os.path.join(self.UNREAD_PATH, 'news-' + repoid + '.unread')
                try:
                        try:
                                unread_lock = lockfile(unreadfile)
                                # Set correct permissions on the news-repoid.unread file
                                apply_permissions(filename=unreadfile,
-                                       uid=int(self.config["PORTAGE_INST_UID"]), gid=portage_gid, mode=0664)
+                                       uid=int(self.config['PORTAGE_INST_UID']), gid=portage_gid, mode=0664)
                                        
                                if os.path.exists(unreadfile):
                                        unread = open(unreadfile).readlines()
@@ -181,9 +181,10 @@ class NewsItem(object):
                if not len(self.restrictions):
                        return True # no restrictions to match means everyone should see it
                
-               kwargs = { 'vardb' : vardb,
-                          'config' : config,
-                          'profile' : profile }
+               kwargs = \
+                       { 'vardb' : vardb,
+                               'config' : config,
+                               'profile' : profile }
 
                for restriction in self.restrictions:
                        if restriction.checkRestriction(**kwargs):
@@ -197,7 +198,7 @@ class NewsItem(object):
                for line in lines:
                        #Optimization to ignore regex matchines on lines that
                        #will never match
-                       if not line.startswith("D"):
+                       if not line.startswith('D'):
                                continue
                        restricts = {  _installedRE : DisplayInstalledRestriction,
                                        _profileRE : DisplayProfileRestriction,
@@ -224,7 +225,7 @@ class DisplayRestriction(object):
        """
 
        def checkRestriction(self, **kwargs):
-               raise NotImplementedError("Derived class should over-ride this method")
+               raise NotImplementedError('Derived class should over-ride this method')
 
 class DisplayProfileRestriction(DisplayRestriction):
        """
@@ -250,7 +251,7 @@ class DisplayKeywordRestriction(DisplayRestriction):
                self.keyword = keyword
 
        def checkRestriction(self, **kwargs):
-               if kwargs['config']["ARCH"] == self.keyword:
+               if kwargs['config']['ARCH'] == self.keyword:
                        return True
                return False
 
index a3875ada872e30bb35819422b04f60e03d71ffe9..05a44e7bdf6b5618223d58b4599003843e9e76c6 100644 (file)
@@ -17,13 +17,14 @@ from portage.env.validators import PackagesFileValidator, ValidAtomValidator
 class StaticFileSet(EditablePackageSet):
        _operations = ["merge", "unmerge"]
        
-       def __init__(self, name, filename, loader=None):
+       def __init__(self, name, filename, data = None):
                super(StaticFileSet, self).__init__(name)
                self._filename = filename
                self._mtime = None
                self.description = "Package set loaded from file %s" % self._filename
-               if loader is None:
-                       self.loader = ConfigLoaderKlass(ItemFileLoader(filename=self._filename,
+               self.data = data
+               if data is None:
+                       self.data = ConfigLoaderKlass(ItemFileLoader(filename=self._filename,
                                validator=PackagesFileValidator))
                metadata = grabfile(self._filename + ".metadata")
                key = None
@@ -54,12 +55,12 @@ class StaticFileSet(EditablePackageSet):
                        mtime = None
                if (not self._loaded or self._mtime != mtime):
                        try:
-                               self.loader.load()
+                               self.data.load()
                        except EnvironmentError, e:
                                if e.errno != errno.ENOENT:
                                        raise
                                del e
-                       self._setAtoms(self.loader.keys())
+                       self._setAtoms(self.data.keys())
                        self._mtime = mtime
        
 class ConfigFileSet(PackageSet):