From: Alec Warner Date: Thu, 8 Mar 2007 06:44:06 +0000 (-0000) Subject: Change load() to have no default arguments, makes caller specifiy explicitly...I... X-Git-Tag: v2.2_pre1~1597 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=934a71d5ddeb42dba7dd35ab129760a3482c416f;p=portage.git Change load() to have no default arguments, makes caller specifiy explicitly...I think assuming a default arg is bad here. Also add PackageUse and PackageUseFile, change up the comments a bit. I've started to notice code re-use here; these are basically the same code. I think I will write up the rest of the package* classes and then perform code merges to save LOC and memory and whatnot svn path=/main/trunk/; revision=6192 --- diff --git a/pym/portage/env/config.py b/pym/portage/env/config.py index 05bb49274..eccd35e08 100644 --- a/pym/portage/env/config.py +++ b/pym/portage/env/config.py @@ -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 diff --git a/pym/portage/tests/env/config/test_PackageKeywordsFile.py b/pym/portage/tests/env/config/test_PackageKeywordsFile.py index f39ef96d7..6c5475881 100644 --- a/pym/portage/tests/env/config/test_PackageKeywordsFile.py +++ b/pym/portage/tests/env/config/test_PackageKeywordsFile.py @@ -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 index 000000000..6dd76eeca --- /dev/null +++ b/pym/portage/tests/env/config/test_PackageUseFile.py @@ -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)