Change load() to have no default arguments, makes caller specifiy explicitly...I...
authorAlec Warner <antarus@gentoo.org>
Thu, 8 Mar 2007 06:44:06 +0000 (06:44 -0000)
committerAlec Warner <antarus@gentoo.org>
Thu, 8 Mar 2007 06:44:06 +0000 (06:44 -0000)
svn path=/main/trunk/; revision=6192

pym/portage/env/config.py
pym/portage/tests/env/config/test_PackageKeywordsFile.py
pym/portage/tests/env/config/test_PackageUseFile.py [new file with mode: 0644]

index 05bb492748a53efc268ff4fb1fff81f649fff0d0..eccd35e084298f10c498525181142cc70d4021d2 100644 (file)
@@ -11,7 +11,7 @@ class PackageKeywords(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':['key1','key2','key3'...]
+       {'cpv':['keyword1','keyword2','keyword3'...]
        """
        
        data = {}
@@ -35,7 +35,7 @@ class PackageKeywordsFile(PackageKeywords):
        def __init__( self, filename ):
                self.fname = filename
        
-       def load(self, recursive=False):
+       def load(self, recursive):
                """
                Package.keywords files have comments that begin with #.
                The entries are of the form:
@@ -58,3 +58,57 @@ class PackageKeywordsFile(PackageKeywords):
                                                self.data[key].append(items)
                                        else:
                                                self.data[key] = items
+
+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 = {}
+       
+       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):
+       """
+       Inherits from PackageUse; implements a file-based backend.  Doesn't handle recursion yet.
+       """
+       def __init__(self, filename):
+               self.fname = filename
+       
+       def load(self, recursive):
+               """
+               Package.keywords files have comments that begin with #.
+               The entries are of the form:
+               >>> atom useflag1 useflag2 useflag3..
+               useflags may optionally be negative with a minus sign (-)
+               >>> atom useflag1 -useflag2 useflag3
+               """
+               
+               if os.path.exists( self.fname ):
+                       f = open(self.fname, 'rb')
+                       for line in f:
+                               if line.startswith('#'):
+                                       continue
+                               split = line.split()
+                               if len(split):
+                                       # Surprisingly this works for iterables of length 1
+                                       # fex ['sys-apps/portage','foo','bar'] becomes {'sys-apps/portage':['foo','bar']}
+                                       key, items = split[0],split[1:]
+                                       # if they specify the same cpv twice; stack the values (append) instead of overwriting.
+                                       if key in self.data:
+                                               self.data[key].append(items)
+                                       else:
+                                               self.data[key] = items
index f39ef96d78befb27a10ccd24642aca7c7b3a1b83..6c5475881fbdb2a76ff34873c79371da559048db 100644 (file)
@@ -19,7 +19,7 @@ class PackageKeywordsFileTestCase(TestCase):
 
                self.BuildFile()
                f = PackageKeywordsFile(self.fname)
-               f.load()
+               f.load(recursive=False)
                for cpv, keyword in f.iteritems():
                        self.assertEqual( cpv, self.cpv )
                        [k for k in keyword if self.assertTrue(k in self.keywords)]
diff --git a/pym/portage/tests/env/config/test_PackageUseFile.py b/pym/portage/tests/env/config/test_PackageUseFile.py
new file mode 100644 (file)
index 0000000..6dd76ee
--- /dev/null
@@ -0,0 +1,33 @@
+# test_PackageUseFile.py -- Portage Unit Testing Functionality
+# Copyright 2007 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id: test_PackageUseFile.py 6182 2007-03-06 07:35:22Z antarus $
+
+from portage.tests import TestCase
+from portage.env.config import PackageUseFile
+
+class PackageUseFileTestCase(TestCase):
+
+       fname = 'package.use'
+       cpv = 'sys-apps/portage'
+       useflags = ['cdrom', 'far', 'boo', 'flag', 'blat']
+       
+       def testPackageUseLoad(self):
+               """
+               A simple test to ensure the load works properly
+               """
+               self.BuildFile()
+               f = PackageUseFile(self.fname)
+               f.load(recursive=False)
+               for cpv, use in f.iteritems():
+                       self.assertEqual( cpv, self.cpv )
+                       [flag for flag in use if self.assertTrue(flag in self.useflags)]
+               self.NukeFile()
+
+       def BuildFile(self):
+               f = open(self.fname, 'wb')
+               f.write("%s %s" % (self.cpv, ' '.join(self.useflags)))
+       
+       def NukeFile(self):
+               import os
+               os.unlink(self.fname)