diff options
author | Alec Warner <antarus@gentoo.org> | 2007-03-07 06:38:05 +0000 |
---|---|---|
committer | Alec Warner <antarus@gentoo.org> | 2007-03-07 06:38:05 +0000 |
commit | 60cb59d71dd75b9ae476a9c6b6363ca1ff77375e (patch) | |
tree | 3f5ef21ec57865225acf6ab195bed661d7fc88aa /pym/portage/env | |
parent | f4eadc536e93cc9f429d249989a61b019c82d6bb (diff) | |
download | portage-60cb59d71dd75b9ae476a9c6b6363ca1ff77375e.tar.gz portage-60cb59d71dd75b9ae476a9c6b6363ca1ff77375e.tar.bz2 portage-60cb59d71dd75b9ae476a9c6b6363ca1ff77375e.zip |
Part of my attempt now involves cleaning up config; this means for me; removing the file-based stuff from it (config_path) and encapsulating that into classes. This is the first one, a simple PackageKeywords class that does file-based stuff, no recursion yet but soon. Trying to do TDD here as well,so tests first then code.
svn path=/main/trunk/; revision=6190
Diffstat (limited to 'pym/portage/env')
-rw-r--r-- | pym/portage/env/__init__.py | 4 | ||||
-rw-r--r-- | pym/portage/env/config.py | 60 |
2 files changed, 64 insertions, 0 deletions
diff --git a/pym/portage/env/__init__.py b/pym/portage/env/__init__.py new file mode 100644 index 000000000..63f6f9ef2 --- /dev/null +++ b/pym/portage/env/__init__.py @@ -0,0 +1,4 @@ +# Copyright: 2007 Gentoo Foundation +# License: GPL2 +# $Id$ + diff --git a/pym/portage/env/config.py b/pym/portage/env/config.py new file mode 100644 index 000000000..05bb49274 --- /dev/null +++ b/pym/portage/env/config.py @@ -0,0 +1,60 @@ +# config.py -- Portage Config +# Copyright 2007 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id$ + +import os +from UserDict import UserDict + +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'...] + """ + + data = {} + + 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): + """ + Inherits from PackageKeywords; implements a file-based backend. Doesn't handle recursion yet. + """ + def __init__( self, filename ): + self.fname = filename + + def load(self, recursive=False): + """ + Package.keywords files have comments that begin with #. + The entries are of the form: + >>> cpv [-~]keyword1 [-~]keyword2 keyword3 + >>> Exceptions include -*, ~*, and ** for keywords. + """ + + 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','x86','amd64'] becomes {'sys-apps/portage':['x86','amd64']} + 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 |