* improve error handling a bit
* put a simple get_option interface to context
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user