From 89455e5de82d644e5b034642bacaac8338e8987a Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Mon, 21 Feb 2011 03:38:41 -0800 Subject: add an error callback function ability to pass errors to. --- layman/output.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/layman/output.py b/layman/output.py index 05260ce..469f673 100644 --- a/layman/output.py +++ b/layman/output.py @@ -24,7 +24,8 @@ class MessageBase(object): err = sys.stderr, info_level = INFO_LEVEL, warn_level = WARN_LEVEL, - col = True + col = True, + error_callback=None ): # Where should the error output go? This can also be a file self.error_out = err @@ -44,7 +45,9 @@ class MessageBase(object): self.debug_lev = OFF - self.has_error = False + # callback function that gets passed any error messages + # that have shown up. + self.error_callback = error_callback def _color (self, col, text): @@ -73,6 +76,12 @@ class MessageBase(object): def set_debug_level(self, debugging_level = DEBUG_LEVEL): self.debug_lev = debugging_level + def do_error_callback(self, error): + """runs the error_callback function with the error + that occurred + """ + if self.error_callback: + self.error_callback(error) class Message(MessageBase): @@ -86,8 +95,9 @@ class Message(MessageBase): err = sys.stderr, info_level = INFO_LEVEL, warn_level = WARN_LEVEL, - col = True - ): + col = True, + error_callback = None + ): MessageBase.__init__(self) @@ -170,7 +180,7 @@ class Message(MessageBase): sys.stdout.flush() print >> self.error_out, self.color_func('red', '* ') + i self.error_out.flush() - self.has_error = True + self.do_error_callback(error) def die (self, error): -- cgit v1.2.3-1-g7c22