http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11421
Revision: 11421 Author: drtrigon Date: 2013-04-21 18:41:07 +0000 (Sun, 21 Apr 2013) Log Message: ----------- improvement; allow logging to capture warnings (from trunk) new feature; exception logging/output function (from trunk)
Modified Paths: -------------- branches/rewrite/pywikibot/bot.py
Modified: branches/rewrite/pywikibot/bot.py =================================================================== --- branches/rewrite/pywikibot/bot.py 2013-04-21 18:29:38 UTC (rev 11420) +++ branches/rewrite/pywikibot/bot.py 2013-04-21 18:41:07 UTC (rev 11421) @@ -191,6 +191,8 @@
root_logger = logging.getLogger("pywiki") root_logger.setLevel(DEBUG+1) # all records except DEBUG go to logger + if hasattr(root_logger, 'captureWarnings'): + root_logger.captureWarnings(True) # introduced in Python >= 2.7 root_logger.handlers = [] # remove any old handlers
# configure handler(s) for display to user interface @@ -392,7 +394,30 @@ """Output a debug record to the log file.""" logoutput(text, decoder, newline, DEBUG, layer, **kwargs)
+def exception(msg=None, decoder=None, newline=True, full=False, **kwargs): + """Output an error traceback to the user via the userinterface.
+ Use directly after an 'except' statement: + ... + except: + pywikibot.exception() + ... + or alternatively: + ... + except Exception, e: + pywikibot.exception(e) + ... + """ + if isinstance(msg, BaseException): + exc_info = 1 + else: + exc_info = sys.exc_info() + msg = unicode(exc_info[1]).strip() + if full: + kwargs['exc_info'] = exc_info + logoutput(msg, decoder, newline, ERROR, **kwargs) + + # User input functions
def input(question, password=False):
pywikipedia-svn@lists.wikimedia.org