use a more logical approach for editable sets
authorMarius Mauch <genone@gentoo.org>
Mon, 16 Jul 2007 07:36:09 +0000 (07:36 -0000)
committerMarius Mauch <genone@gentoo.org>
Mon, 16 Jul 2007 07:36:09 +0000 (07:36 -0000)
svn path=/main/trunk/; revision=7273

pym/portage/sets/__init__.py
pym/portage/sets/files.py

index 51db6e2f0150a2d877b22a22f102e1b0877aca9d..2b48ca6cb56150761110d3caee5fb93d75a196b6 100644 (file)
@@ -8,7 +8,7 @@ from portage.const import PRIVATE_PATH, USER_CONFIG_PATH
 from portage.exception import InvalidAtom
 from portage.dep import isvalidatom, match_from_list, dep_getkey
 
-OPERATIONS = ["merge", "unmerge", "edit"]
+OPERATIONS = ["merge", "unmerge"]
 DEFAULT_SETS = ["world", "system", "everything", "security"] \
        +["package_"+x for x in ["mask", "unmask", "use", "keywords"]]
 
@@ -22,7 +22,7 @@ class PackageSet(object):
        def __init__(self, name):
                self._name = name
                self._atoms = set()
-               self._mtime = None
+               self._loaded = False
        
        def supportsOperation(self, op):
                if not op in OPERATIONS:
@@ -30,7 +30,10 @@ class PackageSet(object):
                return op in self._operations
        
        def getAtoms(self):
-               if not self._mtime or self.supportsOperation("edit"):
+               if not self._loaded:
+                       # Order is important for the next two statements as load() may change
+                       # _loaded to a more specific value for editable subclasses
+                       self._loaded = True
                        self.load()
                return self._atoms
 
@@ -42,55 +45,51 @@ class PackageSet(object):
                        elif not isvalidatom(a):
                                raise InvalidAtom(a)
                self._atoms = atoms
-               if not self.supportsOperation("edit"):
-                       self._mtime = True
        
        def getName(self):
                return self._name
        
+       def load(self):
+               # This method must be overwritten by subclasses
+               # Editable sets should use the value of self._mtime to determine if they
+               # need to reload themselves
+               raise NotImplementedError()
+
+       def containsCPV(self, cpv):
+               for a in self.getAtoms():
+                       if match_from_list(a, cpv):
+                               return True
+               return False
+       
+
+class EditablePackageSet(PackageSet):
        def updateAtoms(self, atoms):
-               if self.supportsOperation("edit"):
-                       self.load()
-                       self._atoms.update(atoms)
-                       self.write()
-               else:
-                       raise NotImplementedError()
+               self.load()
+               self._atoms.update(atoms)
+               self.write()
        
        def addAtom(self, atom):
                self.updateAtoms([atom])
 
        def removeAtom(self, atom):
-               if self.supportsOperation("edit"):
-                       self.load()
-                       self._atoms.discard(atom)
-                       self.write()
-               else:
-                       raise NotImplementedError()
+               self.load()
+               self._atoms.discard(atom)
+               self.write()
 
        def removePackageAtoms(self, cp):
+               self.load()
                for a in self.getAtoms():
                        if dep_getkey(a) == cp:
-                               self.removeAtom(a)
+                               self._atoms.discard(a)
+               self.write()
 
        def write(self):
                # This method must be overwritten in subclasses that should be editable
                raise NotImplementedError()
 
-       def load(self):
-               # This method must be overwritten by subclasses
-               # Editable sets should use the value of self._mtime to determine if they
-               # need to reload themselves
-               raise NotImplementedError()
-
-       def containsCPV(self, cpv):
-               for a in self.getAtoms():
-                       if match_from_list(a, cpv):
-                               return True
-               return False
-       
 
-class InternalPackageSet(PackageSet):
-       _operations = ["merge", "unmerge", "edit"]
+class InternalPackageSet(EditablePackageSet):
+       _operations = ["merge", "unmerge"]
        
        def load(self):
                pass
index c4b3f73f6d0d186d34fa9e93e131b67d9343fd5a..d79571c57db00d758adbc95c9962587f79e2b585 100644 (file)
@@ -5,33 +5,33 @@
 from portage.util import grabfile_package, grabdict_package, write_atomic
 import os
 
-from portage.sets import PackageSet
+from portage.sets import PackageSet, EditablePackageSet
 
-class StaticFileSet(PackageSet):
-       _operations = ["merge", "unmerge", "edit"]
+class StaticFileSet(EditablePackageSet):
+       _operations = ["merge", "unmerge"]
        
        def __init__(self, name, filename):
                super(StaticFileSet, self).__init__(name)
                self._filename = filename
+               self._mtime = None
        
        def write(self):
                write_atomic(self._filename, "\n".join(self._atoms)+"\n")
        
        def load(self):
-               mtime = os.stat(self._filename).st_mtime
-               if not self._mtime or self._mtime != mtime:
-                       self._load()
+               try:
+                       mtime = os.stat(self._filename).st_mtime
+               except (OSError, IOError):
+                       mtime = None
+               if not self._loaded or self._mtime != mtime:
+                       self._setAtoms(grabfile_package(self._filename, recursive=True))
                        self._mtime = mtime
        
-       def _load(self):
-               self._setAtoms(grabfile_package(self._filename, recursive=True))
-       
-class ConfigFileSet(StaticFileSet):
-       _operations = ["merge", "unmerge"]
+class ConfigFileSet(PackageSet):
+       def __init__(self, name, filename):
+               super(ConfigFileSet, self).__init__(name)
+               self._filename = filename
 
-       def write(self):
-               raise NotImplementedError()
-       
-       def _load(self):
+       def load(self):
                self._setAtoms(grabdict_package(self._filename, recursive=True).keys())