From 7d40634bcb42197c97611f18ef996aa203b7a929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Fri, 14 Oct 2005 16:33:50 +0000 Subject: [PATCH] * improve error handling a bit * put a simple get_option interface to context --- pisi-cli | 10 ++++++---- pisi/cli/__init__.py | 15 +++++++++------ pisi/cli/colors.py | 2 +- pisi/context.py | 3 +++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/pisi-cli b/pisi-cli index 7896259a..fdc615a5 100755 --- a/pisi-cli +++ b/pisi-cli @@ -12,8 +12,9 @@ import sys -import gettext +import locale +import gettext __trans = gettext.translation('pisi', fallback=True) _ = __trans.ugettext @@ -30,14 +31,14 @@ def handle_exception(exception, value, tb): ui = pisi.cli.CLI() # make a temporary UI show_traceback = False if exception == exceptions.KeyboardInterrupt: - ui.error(_("KeyboardInterrupt: Exiting...\n")) + ui.error(_("Keyboard Interrupt: Exiting...")) sys.exit(1) elif exception == XmlError: ui.error(unicode(value)) sys.exit(1) elif isinstance(value, pisi.Error): ui.error(_("Fatal Error. Program Terminated.")) - show_traceback = ctx.config.get_option('debug') + show_traceback = ctx.config and ctx.config.options.debug elif isinstance(value, pisi.Exception): show_traceback = True ui.error(_(""" @@ -46,7 +47,7 @@ Please file a bug report. (http://bugs.uludag.org.tr)""")) else: # For any other exception (possibly Python exceptions) show # the traceback! - show_traceback = ctx.config.get_option('debug') + show_traceback = ctx.config and ctx.config.options.debug ui.error(_("System Error. Program Terminated.")) ui.error("%s: %s\n" % (exception, value)) @@ -63,5 +64,6 @@ if __name__ == "__main__": sys.excepthook = handle_exception + locale.setlocale(locale.LC_ALL, '') cli = PisiCLI() cli.run_command() diff --git a/pisi/cli/__init__.py b/pisi/cli/__init__.py index 16bbc7e4..a1dc7620 100644 --- a/pisi/cli/__init__.py +++ b/pisi/cli/__init__.py @@ -36,18 +36,21 @@ class Exception(pisi.Exception): pass -def printu(obj): +def printu(obj, err = False): if not isinstance(obj, unicode): obj = unicode(obj) - print obj.encode('utf-8') - + if err: + out = sys.stderr + else: + out = sys.stdout + out.write(obj.encode('utf-8')) + out.flush() class CLI(UI): "Command Line Interface" def __init__(self, show_debug = False, show_verbose = False): super(CLI, self).__init__(show_debug, show_verbose) - locale.setlocale(locale.LC_ALL, '') def output(self, msg, err = False): if type(msg)==type(unicode()): @@ -72,13 +75,13 @@ class CLI(UI): self.output(msg + msgend) def warning(self,msg): - if ctx.config.get_option('no_color'): + if ctx.get_option('no_color'): self.output(_('Warning: ') + msg + '\n', err=True) else: self.output(colorize(msg + '\n', 'purple'), err=True) def error(self,msg): - if ctx.config.get_option('no_color'): + if ctx.get_option('no_color'): self.output(_('Error: ') + msg + '\n', err=True) else: self.output(colorize(msg + '\n', 'red'), err=True) diff --git a/pisi/cli/colors.py b/pisi/cli/colors.py index 74479c94..12fd4911 100644 --- a/pisi/cli/colors.py +++ b/pisi/cli/colors.py @@ -58,7 +58,7 @@ colors = {'black' : "\033[30m", def colorize(msg, color): """Colorize the given message for console output""" - if colors.has_key(color) and not ctx.config.get_option('no_color'): + if colors.has_key(color) and not ctx.get_option('no_color'): return colors[color] + msg + colors['default'] else: return msg diff --git a/pisi/context.py b/pisi/context.py index 0a2f1b9f..408fc78e 100644 --- a/pisi/context.py +++ b/pisi/context.py @@ -23,6 +23,9 @@ const = pisi.constants.Constants() config = None +def get_option(opt): + return config and config.get_option(opt) + # default UI is CLI ui = None # not now