From 429281392d97880cb075456d85e878dc077e7d51 Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Sun, 6 Feb 2011 15:40:15 -0800 Subject: use the new message class and fix the error recording and output --- layman/api.py | 43 ++++++------ layman/cli.py | 3 +- layman/config.py | 5 +- layman/output.py | 187 +++++++++++++++++++++++++++++++++++++++++++++++++++++ overlord/output.py | 187 ----------------------------------------------------- 5 files changed, 214 insertions(+), 211 deletions(-) create mode 100644 layman/output.py delete mode 100644 overlord/output.py diff --git a/layman/api.py b/layman/api.py index 77e95d8..f3d68b6 100644 --- a/layman/api.py +++ b/layman/api.py @@ -19,10 +19,11 @@ import os from layman.config import BareConfig #from layman.action import Sync -from layman.dbbase import UnknownOverlayException +from layman.dbbase import UnknownOverlayException, UnknownOverlayMessage from layman.db import DB, RemoteDB #from layman.utils import path, delete_empty_directory -from layman.debug import OUT +#from layman.debug import OUT +from layman.output import OUT # give them some values for now, these are from the packagekit backend # TODO establish some proper errors for the api. @@ -114,11 +115,7 @@ class LaymanAPI(object): for ovl in repos: if not self.is_installed(ovl): results.append(True) - break - if not self.is_repo(ovl): - self._error(1, UNKNOWN_REPO_ID %ovl) - results.append(False) - break + continue try: self._get_installed_db().delete(self._get_installed_db().select(ovl)) results.append(True) @@ -145,11 +142,11 @@ class LaymanAPI(object): for ovl in repos: if self.is_installed(ovl): results.append(True) - break + continue if not self.is_repo(ovl): - self._error(1, UNKNOWN_REPO_ID %ovl) + self._error(UnknownOverlayMessage(ovl)) results.append(False) - break + continue try: self._get_installed_db().add(self._get_remote_db().select(ovl), quiet=True) results.append(True) @@ -199,12 +196,13 @@ class LaymanAPI(object): for ovl in repos: if not self.is_repo(ovl): - self._error(1, UNKNOWN_REPO_ID %ovl) + self._error(UnknownOverlayMessage(ovl)) result[ovl] = ('', False, False) + continue try: overlay = db.select(ovl) except UnknownOverlayException, error: - self._error(2, "Error: %s" %str(error)) + self._error(error) result[ovl] = ('', False, False) else: result[ovl] = { @@ -247,20 +245,21 @@ class LaymanAPI(object): for ovl in repos: if not self.is_repo(ovl): - self._error(1, UNKNOWN_REPO_ID % ovl) + self._error(UnknownOverlayMessage(ovl)) result[ovl] = ('', False, False) + continue try: overlay = db.select(ovl) #print "overlay = ", ovl - #print overlay + #print "!!!", overlay except UnknownOverlayException, error: #print "ERRORS", str(error) - self._error(2, "Error: %s" %str(error)) + self._error(error) result[ovl] = ('', False, False) else: # Is the overlay supported? if verbose: - info = overlay.__str__() + info = overlay.get_infostr() else: info = overlay.short_list(width) official = overlay.is_official() @@ -271,7 +270,7 @@ class LaymanAPI(object): def get_info_list(self, local=True, verbose=False, width=0): """retrieves the string representation of the recorded information - about the repo(s) specified by ovl + about the repo(s) @param local: bool (defaults to True) @param verbose: bool(defaults to False) @@ -303,7 +302,7 @@ class LaymanAPI(object): try: odb = db.select(ovl) except UnknownOverlayException, error: - self._error(1,"Sync(), failed to select %s overlay. Original error was: %s" %(ovl, str(error))) + self._error(UnknownOverlayException(error)) continue try: @@ -376,7 +375,7 @@ class LaymanAPI(object): try: self._get_remote_db().cache() except Exception, error: - self._error(-1,'Failed to fetch overlay list!\n Original Error was: ' + self._error('Failed to fetch overlay list!\n Original Error was: ' + str(error)) return False self.get_available(reload=True) @@ -417,13 +416,13 @@ class LaymanAPI(object): result = self.get_installed(reload=True) - def _error(self, num, message): + def _error(self, message): """outputs the error to the pre-determined output defaults to stderr. This method may be removed, is here for now due to code taken from the packagekit backend. """ - msg = "Error: %d," % num, message - self._error_messages.append(msg) + #msg = "Error: %d," % num, message + self._error_messages.append(message) if self.report_errors: print >>stderr, msg diff --git a/layman/cli.py b/layman/cli.py index dca6bb6..3277c1b 100644 --- a/layman/cli.py +++ b/layman/cli.py @@ -171,7 +171,8 @@ class Main(object): try: result += getattr(self, action[1])() except Exception, error: - self.output.error(self.api.get_errors()) + for _error in self.api.get_errors(): + self.output.error(_error) result = -1 # So it cannot remain 0, i.e. success break diff --git a/layman/config.py b/layman/config.py index 33951e7..e515155 100644 --- a/layman/config.py +++ b/layman/config.py @@ -31,7 +31,10 @@ import sys, ConfigParser import os from optparse import OptionParser, OptionGroup -from layman.debug import OUT + +#from layman.debug import OUT +from layman.output import OUT + from layman.version import VERSION #=============================================================================== diff --git a/layman/output.py b/layman/output.py new file mode 100644 index 0000000..49ebb34 --- /dev/null +++ b/layman/output.py @@ -0,0 +1,187 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +""" Copyright 2005 - 2008 Gunnar Wrobel + 2011 - Brian Dolbec + Distributed under the terms of the GNU General Public License v2 +""" + + +__version__ = "0.1" + + +import sys, inspect, types + +from optparse import OptionGroup + +from overlord.constants import codes + + + +class Message: + #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 = 4, + warn_level = 4, + col = True + ): + + # Where should the error output go? This can also be a file + self.error_out = err + + # Where should the normal output go? This can also be a file + self.std_out = out + + # The higher the level the more information you will get + self.warn_lev = warn_level + + # The higher the level the more information you will get + self.info_lev = info_level + + # Should the output be colored? + self.set_colorize(col) + + self.has_error = False + + + def color (self, col, text): + return codes[col] + text + codes['reset'] + + + def no_color (self, col, text): + return text + + + def set_colorize(self, state): + if state: + self.color_func = self.color + else: + self.color_func = self.no_color + + + def set_info_level(self, info_level = 4): + self.info_lev = info_level + + + def info_off(self): + self.set_info_level(0) + + + def info_on(self, info_level = 4): + self.set_info_level(info_level) + + + def set_warn_level(self, warn_level = 4): + self.warn_lev = warn_level + + + def warn_off(self): + self.set_warn_level(0) + + + def warn_on(self, warn_level = 4): + self.set_warn_level(warn_level) + + + + + ## Output Functions + + def debug(self, info, level=0): + """empty debug function""" + pass + + def notice (self, note): + print >> self.std_out, note + + + def info (self, info, level = 4): + + #print "info =", info + + if type(info) not in types.StringTypes: + info = str(info) + + if level > self.info_lev: + return + + for i in info.split('\n'): + print >> self.std_out, self.color_func('green', '* ') + i + + + def status (self, message, status, info = 'ignored'): + + if type(message) not in types.StringTypes: + message = str(message) + + lines = message.split('\n') + + if not lines: + return + + for i in lines[0:-1]: + print >> self.std_out, self.color_func('green', '* ') + i + + i = lines[-1] + + if len(i) > 58: + i = i[0:57] + + if status == 1: + result = '[' + self.color_func('green', 'ok') + ']' + elif status == 0: + result = '[' + self.color_func('red', 'failed') + ']' + else: + result = '[' + self.color_func('yellow', info) + ']' + + print >> self.std_out, self.color_func('green', '* ') + i + ' ' + \ + '.' * (58 - len(i)) + ' ' + result + + + def warn (self, warn, level = 4): + + #print "DEBUG.warn()" + + if type(warn) not in types.StringTypes: + warn = str(warn) + + if level > self.warn_lev: + return + + for i in warn.split('\n'): + print >> self.std_out, self.color_func('yellow', '* ') + i + + + def error (self, error): + + if type(error) not in types.StringTypes: + error = str(error) + + 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". + sys.stdout.flush() + print >> self.error_out, self.color_func('red', '* ') + i + self.error_out.flush() + self.has_error = True + + + def die (self, error): + + if type(error) not in types.StringTypes: + error = str(error) + + for i in error.split('\n'): + self.error(self.color_func('red', 'Fatal error: ') + i) + self.error(self.color_func('red', 'Fatal error(s) - aborting')) + sys.exit(1) + + + +## gloabal message handler +OUT = Message() diff --git a/overlord/output.py b/overlord/output.py deleted file mode 100644 index 49ebb34..0000000 --- a/overlord/output.py +++ /dev/null @@ -1,187 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -""" Copyright 2005 - 2008 Gunnar Wrobel - 2011 - Brian Dolbec - Distributed under the terms of the GNU General Public License v2 -""" - - -__version__ = "0.1" - - -import sys, inspect, types - -from optparse import OptionGroup - -from overlord.constants import codes - - - -class Message: - #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 = 4, - warn_level = 4, - col = True - ): - - # Where should the error output go? This can also be a file - self.error_out = err - - # Where should the normal output go? This can also be a file - self.std_out = out - - # The higher the level the more information you will get - self.warn_lev = warn_level - - # The higher the level the more information you will get - self.info_lev = info_level - - # Should the output be colored? - self.set_colorize(col) - - self.has_error = False - - - def color (self, col, text): - return codes[col] + text + codes['reset'] - - - def no_color (self, col, text): - return text - - - def set_colorize(self, state): - if state: - self.color_func = self.color - else: - self.color_func = self.no_color - - - def set_info_level(self, info_level = 4): - self.info_lev = info_level - - - def info_off(self): - self.set_info_level(0) - - - def info_on(self, info_level = 4): - self.set_info_level(info_level) - - - def set_warn_level(self, warn_level = 4): - self.warn_lev = warn_level - - - def warn_off(self): - self.set_warn_level(0) - - - def warn_on(self, warn_level = 4): - self.set_warn_level(warn_level) - - - - - ## Output Functions - - def debug(self, info, level=0): - """empty debug function""" - pass - - def notice (self, note): - print >> self.std_out, note - - - def info (self, info, level = 4): - - #print "info =", info - - if type(info) not in types.StringTypes: - info = str(info) - - if level > self.info_lev: - return - - for i in info.split('\n'): - print >> self.std_out, self.color_func('green', '* ') + i - - - def status (self, message, status, info = 'ignored'): - - if type(message) not in types.StringTypes: - message = str(message) - - lines = message.split('\n') - - if not lines: - return - - for i in lines[0:-1]: - print >> self.std_out, self.color_func('green', '* ') + i - - i = lines[-1] - - if len(i) > 58: - i = i[0:57] - - if status == 1: - result = '[' + self.color_func('green', 'ok') + ']' - elif status == 0: - result = '[' + self.color_func('red', 'failed') + ']' - else: - result = '[' + self.color_func('yellow', info) + ']' - - print >> self.std_out, self.color_func('green', '* ') + i + ' ' + \ - '.' * (58 - len(i)) + ' ' + result - - - def warn (self, warn, level = 4): - - #print "DEBUG.warn()" - - if type(warn) not in types.StringTypes: - warn = str(warn) - - if level > self.warn_lev: - return - - for i in warn.split('\n'): - print >> self.std_out, self.color_func('yellow', '* ') + i - - - def error (self, error): - - if type(error) not in types.StringTypes: - error = str(error) - - 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". - sys.stdout.flush() - print >> self.error_out, self.color_func('red', '* ') + i - self.error_out.flush() - self.has_error = True - - - def die (self, error): - - if type(error) not in types.StringTypes: - error = str(error) - - for i in error.split('\n'): - self.error(self.color_func('red', 'Fatal error: ') + i) - self.error(self.color_func('red', 'Fatal error(s) - aborting')) - sys.exit(1) - - - -## gloabal message handler -OUT = Message() -- cgit v1.2.3-1-g7c22