public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/gentoopm:master commit in: gentoopm/bash/, gentoopm/basepm/
@ 2011-07-11  9:39 Michał Górny
  0 siblings, 0 replies; only message in thread
From: Michał Górny @ 2011-07-11  9:39 UTC (permalink / raw
  To: gentoo-commits

commit:     a2b7c9c28f6a6f7667786b57438cce2d3ce44e3b
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Mon Jul 11 09:13:09 2011 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Mon Jul 11 09:13:09 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/gentoopm.git;a=commit;h=a2b7c9c2

Support grabbing package environment.

---
 gentoopm/basepm/environ.py  |   53 +++++++++++++++++++++++++++++++++++++++++++
 gentoopm/basepm/pkg.py      |    9 +-----
 gentoopm/bash/__init__.py   |   14 +++++++++-
 gentoopm/bash/bashserver.py |    2 +-
 4 files changed, 68 insertions(+), 10 deletions(-)

diff --git a/gentoopm/basepm/environ.py b/gentoopm/basepm/environ.py
new file mode 100644
index 0000000..86ee5f5
--- /dev/null
+++ b/gentoopm/basepm/environ.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+#	vim:fileencoding=utf-8
+# (c) 2011 Michał Górny <mgorny@gentoo.org>
+# Released under the terms of the 2-clause BSD license.
+
+import bz2
+
+from gentoopm.bash import get_any_bashparser
+
+class LazyBashParser(object):
+	"""
+	Lazily-initialized, shared bash parser wrapper.
+	"""
+	_curr_path = None
+	_parser = None
+
+	def set_file(self, path, open_func):
+		if self._curr_path == path:
+			return
+		self._curr_path = path
+		f = open_func(path)
+		if self._parser is None:
+			self._parser = get_any_bashparser()
+		self._parser.load_file(f)
+		f.close()
+
+	def __getitem__(self, k):
+		return self._parser[k]
+
+	def copy(self, *v):
+		return self._parser.copy(*v)
+
+	def __del__(self):
+		del self._parser
+
+_bp = LazyBashParser()
+
+class PMPackageEnvironment(object):
+	"""
+	Package environment accessor class.
+	"""
+
+	def __init__(self, path, bzipped2 = False):
+		self._path = path
+		self._open_func = bz2.BZ2File if bzipped2 else open
+
+	def __getitem__(self, k):
+		_bp.set_file(self._path, self._open_func)
+		return _bp[k]
+
+	def copy(self, *keys):
+		_bp.set_file(self._path, self._open_func)
+		return _bp.copy(*keys)

diff --git a/gentoopm/basepm/pkg.py b/gentoopm/basepm/pkg.py
index 4293f4d..f91c52f 100644
--- a/gentoopm/basepm/pkg.py
+++ b/gentoopm/basepm/pkg.py
@@ -3,10 +3,11 @@
 # (c) 2011 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
-import bz2, os.path
+import os.path
 from abc import abstractmethod, abstractproperty
 
 from gentoopm.basepm.atom import PMAtom
+from gentoopm.basepm.environ import PMPackageEnvironment
 from gentoopm.exceptions import EmptyPackageSetError, AmbiguousPackageSetError
 from gentoopm.util import ABCObject
 
@@ -249,9 +250,3 @@ class PMPackage(ABCObject):
 
 	def __repr__(self):
 		return '%s(%s)' % (self.__class__.__name__, repr(self.id))
-
-class PMPackageEnvironment(object):
-	def __init__(self, f, bzipped2 = False):
-		self._f = f
-		self._open_func = bz2.BZ2File if bzipped2 else open
-		print (self._f, self._open_func)

diff --git a/gentoopm/bash/__init__.py b/gentoopm/bash/__init__.py
index 53b9b88..284a358 100644
--- a/gentoopm/bash/__init__.py
+++ b/gentoopm/bash/__init__.py
@@ -34,10 +34,10 @@ class BashParser(ABCObject):
 		"""
 		pass
 
-	def get_env(self, *varlist):
+	def copy(self, *varlist):
 		"""
 		Get values of multiple environment variables, and return them
-		as a dict.
+		as a dict (a 'local copy' of the environment).
 
 		@param varlist: environment variable names
 		@type varlist: list(string)
@@ -49,3 +49,13 @@ class BashParser(ABCObject):
 		for v in varlist:
 			out[v] = self[v]
 		return out
+
+def get_any_bashparser():
+	"""
+	Get any BashParser implementation (the best one available).
+
+	@return: a BashParser instance
+	@rtype: L{BashParser}
+	"""
+	from gentoopm.bash.bashserver import BashServer
+	return BashServer()

diff --git a/gentoopm/bash/bashserver.py b/gentoopm/bash/bashserver.py
index 8fb8e09..f83c88b 100644
--- a/gentoopm/bash/bashserver.py
+++ b/gentoopm/bash/bashserver.py
@@ -64,7 +64,7 @@ class BashServer(BashParser):
 
 		return self._read1()
 
-	def get_env(self, *varlist):
+	def copy(self, *varlist):
 		q = ' '.join(['"${%s}"' % v for v in varlist])
 		self._bashproc.stdin.write(('%s\n' % q).encode('ASCII'))
 		self._bashproc.stdin.flush()



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2011-07-11  9:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-11  9:39 [gentoo-commits] proj/gentoopm:master commit in: gentoopm/bash/, gentoopm/basepm/ Michał Górny

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox