* improve error handling a bit

* put a simple get_option interface to context
This commit is contained in:
Eray Özkural
2005-10-14 16:33:50 +00:00
parent e4c8caf16a
commit 7d40634bcb
4 changed files with 19 additions and 11 deletions
+6 -4
View File
@@ -12,8 +12,9 @@
import sys import sys
import gettext import locale
import gettext
__trans = gettext.translation('pisi', fallback=True) __trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext _ = __trans.ugettext
@@ -30,14 +31,14 @@ def handle_exception(exception, value, tb):
ui = pisi.cli.CLI() # make a temporary UI ui = pisi.cli.CLI() # make a temporary UI
show_traceback = False show_traceback = False
if exception == exceptions.KeyboardInterrupt: if exception == exceptions.KeyboardInterrupt:
ui.error(_("KeyboardInterrupt: Exiting...\n")) ui.error(_("Keyboard Interrupt: Exiting..."))
sys.exit(1) sys.exit(1)
elif exception == XmlError: elif exception == XmlError:
ui.error(unicode(value)) ui.error(unicode(value))
sys.exit(1) sys.exit(1)
elif isinstance(value, pisi.Error): elif isinstance(value, pisi.Error):
ui.error(_("Fatal Error. Program Terminated.")) 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): elif isinstance(value, pisi.Exception):
show_traceback = True show_traceback = True
ui.error(_(""" ui.error(_("""
@@ -46,7 +47,7 @@ Please file a bug report. (http://bugs.uludag.org.tr)"""))
else: else:
# For any other exception (possibly Python exceptions) show # For any other exception (possibly Python exceptions) show
# the traceback! # 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(_("System Error. Program Terminated."))
ui.error("%s: %s\n" % (exception, value)) ui.error("%s: %s\n" % (exception, value))
@@ -63,5 +64,6 @@ if __name__ == "__main__":
sys.excepthook = handle_exception sys.excepthook = handle_exception
locale.setlocale(locale.LC_ALL, '')
cli = PisiCLI() cli = PisiCLI()
cli.run_command() cli.run_command()
+9 -6
View File
@@ -36,18 +36,21 @@ class Exception(pisi.Exception):
pass pass
def printu(obj): def printu(obj, err = False):
if not isinstance(obj, unicode): if not isinstance(obj, unicode):
obj = unicode(obj) 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): class CLI(UI):
"Command Line Interface" "Command Line Interface"
def __init__(self, show_debug = False, show_verbose = False): def __init__(self, show_debug = False, show_verbose = False):
super(CLI, self).__init__(show_debug, show_verbose) super(CLI, self).__init__(show_debug, show_verbose)
locale.setlocale(locale.LC_ALL, '')
def output(self, msg, err = False): def output(self, msg, err = False):
if type(msg)==type(unicode()): if type(msg)==type(unicode()):
@@ -72,13 +75,13 @@ class CLI(UI):
self.output(msg + msgend) self.output(msg + msgend)
def warning(self,msg): def warning(self,msg):
if ctx.config.get_option('no_color'): if ctx.get_option('no_color'):
self.output(_('Warning: ') + msg + '\n', err=True) self.output(_('Warning: ') + msg + '\n', err=True)
else: else:
self.output(colorize(msg + '\n', 'purple'), err=True) self.output(colorize(msg + '\n', 'purple'), err=True)
def error(self,msg): def error(self,msg):
if ctx.config.get_option('no_color'): if ctx.get_option('no_color'):
self.output(_('Error: ') + msg + '\n', err=True) self.output(_('Error: ') + msg + '\n', err=True)
else: else:
self.output(colorize(msg + '\n', 'red'), err=True) self.output(colorize(msg + '\n', 'red'), err=True)
+1 -1
View File
@@ -58,7 +58,7 @@ colors = {'black' : "\033[30m",
def colorize(msg, color): def colorize(msg, color):
"""Colorize the given message for console output""" """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'] return colors[color] + msg + colors['default']
else: else:
return msg return msg
+3
View File
@@ -23,6 +23,9 @@ const = pisi.constants.Constants()
config = None config = None
def get_option(opt):
return config and config.get_option(opt)
# default UI is CLI # default UI is CLI
ui = None # not now ui = None # not now