From 69ca817b92d78e91b03ee3e327b9e369c8f26e5a Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Sun, 13 Feb 2011 16:52:19 -0800 Subject: Split out MessageBase class, more code reduction/cleanup --- layman/output.py | 53 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/layman/output.py b/layman/output.py index 5785d58..250ce96 100644 --- a/layman/output.py +++ b/layman/output.py @@ -12,15 +12,14 @@ __version__ = "0.1" import sys, inspect, types -from optparse import OptionGroup +from optparse import OptionGroup -from layman.constants import codes, INFO_LEVEL, WARN_LEVEL, OFF +from layman.constants import codes, INFO_LEVEL, WARN_LEVEL, DEBUG_LEVEL, OFF - -class Message: - #FIXME: Think about some simple doctests before you modify this class the - # next time. +class MessageBase(object): + """Base Message class helper functions and variables + """ def __init__(self, out = sys.stdout, @@ -29,7 +28,6 @@ class Message: warn_level = WARN_LEVEL, col = True ): - # Where should the error output go? This can also be a file self.error_out = err @@ -43,24 +41,27 @@ class Message: self.info_lev = info_level # Should the output be colored? + self.color_func = None self.set_colorize(col) + self.debug_lev = OFF + self.has_error = False - def color (self, col, text): + def _color (self, col, text): return codes[col] + text + codes['reset'] - def no_color (self, col, text): + def _no_color (self, col, text): return text def set_colorize(self, state): if state: - self.color_func = self.color + self.color_func = self._color else: - self.color_func = self.no_color + self.color_func = self._no_color def set_info_level(self, info_level = INFO_LEVEL): @@ -71,10 +72,34 @@ class Message: self.warn_lev = warn_level + def set_debug_level(self, debugging_level = DEBUG_LEVEL): + self.debug_lev = debugging_level + + + +class Message(MessageBase): + """Primary Message output methods + """ + #FIXME: Think about some simple doctests before you modify this class the + # next time. + + def __init__(self, + out = sys.stdout, + err = sys.stderr, + info_level = INFO_LEVEL, + warn_level = WARN_LEVEL, + col = True + ): + + MessageBase.__init__(self) + + ## Output Functions def debug(self, info, level = OFF): - """empty debug function""" + """empty debug function, does nothing, + declared here for compatibility with DebugMessage + """ pass @@ -84,8 +109,6 @@ class Message: def info (self, info, level = INFO_LEVEL): - #print "info =", info - if type(info) not in types.StringTypes: info = str(info) @@ -145,7 +168,7 @@ class Message: for i in error.split('\n'): # NOTE: Forced flushing ensures that stdout and stderr # stay in nice order. This is a workaround for calls like - # "overlord -L |& less". + # "layman -L |& less". sys.stdout.flush() print >> self.error_out, self.color_func('red', '* ') + i self.error_out.flush() -- cgit v1.2.3-1-g7c22