From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QgCxn-0005bx-Dj for garchives@archives.gentoo.org; Mon, 11 Jul 2011 09:39:39 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id A6AC821C0C5; Mon, 11 Jul 2011 09:39:31 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 659EB21C0C5 for ; Mon, 11 Jul 2011 09:39:31 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id D59BC1B4075 for ; Mon, 11 Jul 2011 09:39:30 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id BD38A80042 for ; Mon, 11 Jul 2011 09:39:29 +0000 (UTC) From: "Michał Górny" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Michał Górny" Message-ID: Subject: [gentoo-commits] proj/gentoopm:master commit in: gentoopm/basepm/ X-VCS-Repository: proj/gentoopm X-VCS-Files: gentoopm/basepm/environ.py X-VCS-Directories: gentoopm/basepm/ X-VCS-Committer: mgorny X-VCS-Committer-Name: Michał Górny X-VCS-Revision: a15d5b2ad120d17a92df2da61300f1da6faa7e6b Date: Mon, 11 Jul 2011 09:39:29 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 157d46cd00c278060c2edd169fbf4ce3 commit: a15d5b2ad120d17a92df2da61300f1da6faa7e6b Author: Micha=C5=82 G=C3=B3rny gentoo org> AuthorDate: Mon Jul 11 09:40:06 2011 +0000 Commit: Micha=C5=82 G=C3=B3rny gentoo org> CommitDate: Mon Jul 11 09:40:06 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/gentoopm.git;= a=3Dcommit;h=3Da15d5b2a Support forking a BashParser out of PMPackageEnvironment. --- gentoopm/basepm/environ.py | 64 ++++++++++++++++++++++++++++++++++++++= +++-- 1 files changed, 61 insertions(+), 3 deletions(-) diff --git a/gentoopm/basepm/environ.py b/gentoopm/basepm/environ.py index 86ee5f5..8e1c469 100644 --- a/gentoopm/basepm/environ.py +++ b/gentoopm/basepm/environ.py @@ -7,6 +7,21 @@ import bz2 =20 from gentoopm.bash import get_any_bashparser =20 +def _load_bp(bp, path, open_func): + """ + Load a file onto a bash parser. + + @param bp: the bash parser instance + @type bp: L{BashParser} + @param path: path to the environment file + @type path: string + @param open_func: open function for the file + @type open_func: func(path, mode) + """ + f =3D open_func(path) + bp.load_file(f) + f.close() + class LazyBashParser(object): """ Lazily-initialized, shared bash parser wrapper. @@ -15,14 +30,21 @@ class LazyBashParser(object): _parser =3D None =20 def set_file(self, path, open_func): + """ + Switch the currently used environment file, if necessary. + + @param path: path to the new environment file + @type path: string + @param open_func: open functions for the new file + @type open_func: func(path, mode) + """ + if self._curr_path =3D=3D path: return self._curr_path =3D path - f =3D open_func(path) if self._parser is None: self._parser =3D get_any_bashparser() - self._parser.load_file(f) - f.close() + _load_bp(self._parser, path, open_func) =20 def __getitem__(self, k): return self._parser[k] @@ -41,13 +63,49 @@ class PMPackageEnvironment(object): """ =20 def __init__(self, path, bzipped2 =3D False): + """ + Instantiate L{PMPackageEnvironment} accessor. + + @param path: path to the environment file + @type path: string + @param bzipped2: whether to bunzip2 the file + @type bzipped2: bool + """ self._path =3D path self._open_func =3D bz2.BZ2File if bzipped2 else open =20 def __getitem__(self, k): + """ + Get the value of an environment key by name. + + @param k: the key to access + @type k: string + @return: the environment variable value + @rtype: string + """ _bp.set_file(self._path, self._open_func) return _bp[k] =20 def copy(self, *keys): + """ + Get a number of environment keys as a dict. + + @param keys: keys to access + @type keys: strings + @return: a dict of copied environment keys + @rtype: dict(string -> string) + """ _bp.set_file(self._path, self._open_func) return _bp.copy(*keys) + + def fork(self): + """ + Fork the bash parser. In other words, return a completely separate + instance with the environment file loaded. + + @return: forked L{BashParser} instance + @rtype: L{BashParser} + """ + bp =3D get_any_bashparser() + _load_bp(bp, self._path, self._open_func) + return bp