summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2007-07-04 12:27:44 +0000
committerZac Medico <zmedico@gentoo.org>2007-07-04 12:27:44 +0000
commit5283c251bc28c14e56288e2d2b76d05661e2b7f9 (patch)
tree6cc25f42e74e86fe1e02e37cc9655efc25e184f6
parent60a4ff083107c12bafbe9ce7e48ce3070e5f8d7d (diff)
downloadportage-5283c251bc28c14e56288e2d2b76d05661e2b7f9.tar.gz
portage-5283c251bc28c14e56288e2d2b76d05661e2b7f9.tar.bz2
portage-5283c251bc28c14e56288e2d2b76d05661e2b7f9.zip
Add a "onerror" callback to parse_color_map() for handling each ParseError that would otherwise be raised.
svn path=/main/trunk/; revision=7147
-rw-r--r--pym/portage/output.py38
1 files changed, 33 insertions, 5 deletions
diff --git a/pym/portage/output.py b/pym/portage/output.py
index fabfb4bdb..e0f675c8d 100644
--- a/pym/portage/output.py
+++ b/pym/portage/output.py
@@ -154,7 +154,16 @@ codes["PKG_NOMERGE_WORLD"] = codes["blue"]
codes["PKG_NOMERGE_ARG_SYSTEM"] = codes["blue"]
codes["PKG_NOMERGE_ARG_WORLD"] = codes["blue"]
-def parse_color_map():
+def parse_color_map(onerror=None):
+ """
+ Parse /etc/portage/color.map and return a dict of error codes.
+
+ @param onerror: an optional callback to handle any ParseError that would
+ otherwise be raised
+ @type onerror: callable
+ @rtype: dict
+ @return: a dictionary mapping color classes to color codes
+ """
myfile = COLOR_MAP_FILE
ansi_code_pattern = re.compile("^[0-9;]*m$")
def strip_quotes(token, quotes):
@@ -170,11 +179,24 @@ def parse_color_map():
if k is s.eof:
break
if o != "=":
- raise ParseError("%s%s'%s'" % (s.error_leader(myfile, s.lineno), "expected '=' operator: ", o))
+ e = ParseError("%s%s'%s'" % (
+ s.error_leader(myfile, s.lineno),
+ "expected '=' operator: ", o))
+ if onerror:
+ onerror(e)
+ else:
+ raise e
+ continue
k = strip_quotes(k, s.quotes)
v = strip_quotes(v, s.quotes)
if not k in codes:
- print ParseError("%s%s'%s'" % (s.error_leader(myfile, s.lineno), "Unknown variable: ", k))
+ e = ParseError("%s%s'%s'" % (
+ s.error_leader(myfile, s.lineno),
+ "Unknown variable: ", k))
+ if onerror:
+ onerror(e)
+ else:
+ raise e
continue
if ansi_code_pattern.match(v):
codes[k] = esc_seq + v
@@ -183,7 +205,13 @@ def parse_color_map():
if x in codes:
codes[k] = codes[k] + codes[x]
else:
- print ParseError("%s%s'%s'" % (s.error_leader(myfile, s.lineno), "Undefined: ", x))
+ e = ParseError("%s%s'%s'" % (
+ s.error_leader(myfile, s.lineno),
+ "Undefined: ", x))
+ if onerror:
+ onerror(e)
+ else:
+ raise e
except (IOError, OSError), e:
if e.errno == errno.ENOENT:
raise FileNotFound(myfile)
@@ -192,7 +220,7 @@ def parse_color_map():
raise
try:
- parse_color_map()
+ parse_color_map(onerror=lambda e: writemsg("%s\n" % str(e), noiselevel=-1))
except FileNotFound, e:
pass
except PortageException, e: