# HG changeset patch # User Alexander Smishlajev # Date 1089713880 0 # Node ID 7500a9bb8bf9b3ec800a9ac7f37738a2bbe0ed39 # Parent 420d5c2a49d98c1c273e312b7f03e05bc8c04bce message translator is passed by the client in function arguments; more messages marked for translation, especially in pt_html(); fix vim modeline diff -r 420d5c2a49d9 -r 7500a9bb8bf9 roundup/cgi/cgitb.py --- a/roundup/cgi/cgitb.py Tue Jul 13 09:41:15 2004 +0000 +++ b/roundup/cgi/cgitb.py Tue Jul 13 10:18:00 2004 +0000 @@ -1,7 +1,7 @@ # # This module was written by Ka-Ping Yee, . # -# $Id: cgitb.py,v 1.11 2004-06-09 09:20:01 a1s Exp $ +# $Id: cgitb.py,v 1.12 2004-07-13 10:18:00 a1s Exp $ """Extended CGI traceback handler by Ka-Ping Yee, . """ @@ -10,10 +10,25 @@ import sys, os, types, string, keyword, linecache, tokenize, inspect, cgi import pydoc, traceback -from roundup.cgi import templating +from roundup.cgi import templating, TranslationService + +def get_translator(i18n=None): + """Return message translation function (gettext) + + Parameters: + i18n - translation service, such as roundup.i18n module + or TranslationService object. -def _(msgid): - return templating.translationService.gettext(msgid) + Return ``gettext`` attribute of the ``i18n`` object, if available + (must be a message translation function with one argument). + If ``gettext`` cannot be obtained from ``i18n``, take default + TranslationService. + + """ + try: + return i18n.gettext + except: + return TranslationService.get_translation().gettext def breaker(): return ('' + @@ -27,12 +42,14 @@ cgi.escape(repr(v)))) return '\n'.join(l) -def pt_html(context=5): +def pt_html(context=5, i18n=None): + _ = get_translator(i18n) esc = cgi.escape - l = ['

Templating Error

', - '

%s: %s

'%(esc(str(sys.exc_type)), - esc(str(sys.exc_value))), - '

Debugging information follows

', + exc_info = [esc(str(value)) for value in sys.exc_info()[:2]] + l = [_('

Templating Error

\n' + '

%(exc_type)s: %(exc_value)s

\n' + '

Debugging information follows

' + ) % {'exc_type': exc_info[0], 'exc_value': exc_info[1]}, '
    ',] from roundup.cgi.PageTemplates.Expressions import TraversalError t = inspect.trace(context) @@ -44,50 +61,60 @@ if isinstance(ti, TraversalError): s = [] for name, info in ti.path: - s.append('
  1. "%s" (%s)
  2. '%(name, esc(repr(info)))) + s.append(_('
  3. "%(name)s" (%(info)s)
  4. ') + % {'name': name, 'info': esc(repr(info))}) s = '\n'.join(s) - l.append('
  5. Looking for "%s", current path:
      %s
  6. '%( - ti.name, s)) + l.append(_('
  7. Looking for "%(name)s", ' + 'current path:
      %(path)s
  8. ' + ) % {'name': ti.name, 'path': s}) else: - l.append('
  9. In %s
  10. '%esc(str(ti))) + l.append(_('
  11. In %s
  12. ') % esc(str(ti))) if locals.has_key('__traceback_supplement__'): ts = locals['__traceback_supplement__'] if len(ts) == 2: supp, context = ts - s = 'A problem occurred in your template "%s".'%str(context.id) + s = _('A problem occurred in your template "%s".') \ + % str(context.id) if context._v_errors: s = s + '
    ' + '
    '.join( [esc(x) for x in context._v_errors]) l.append('
  13. %s
  14. '%s) elif len(ts) == 3: supp, context, info = ts - l.append(''' -
  15. While evaluating the %r expression on line %d + l.append(_(''' +
  16. While evaluating the %(info)r expression on line %(line)d - %s - %s + %(globals)s + %(locals)s
    Current variables:
  17. -'''%(info, context.position[0], niceDict(' ', context.global_vars), - niceDict(' ', context.local_vars))) +''') % { + 'info': info, + 'line': context.position[0], + 'globals': niceDict(' ', context.global_vars), + 'locals': niceDict(' ', context.local_vars) +}) l.append('''
- + -
Full traceback:
%s
%s
'''%cgi.escape(''.join(traceback.format_exception(sys.exc_type, - sys.exc_value, sys.exc_traceback)))) +''' % (_('Full traceback:'), cgi.escape(''.join( + traceback.format_exception(*sys.exc_info()) + )))) l.append('

 

') return '\n'.join(l) -def html(context=5): +def html(context=5, i18n=None): + _ = get_translator(i18n) etype, evalue = sys.exc_type, sys.exc_value if type(etype) is types.ClassType: etype = etype.__name__ pyver = 'Python ' + string.split(sys.version)[0] + '
' + sys.executable head = pydoc.html.heading( - '%s: %s'%(etype, evalue), + _('%(exc_type)s: %(exc_value)s') + % {'exc_type': etype, 'exc_value': evalue}, '#ffffff', '#777777', pyver) head = head + (_('

A problem occurred while running a Python script. ' @@ -99,8 +126,8 @@ traceback = [] for frame, file, lnum, func, lines, index in inspect.trace(context): if file is None: - link = '''<file is None - probably inside eval or - exec>''' + link = _("<file is None - probably inside eval " + "or exec>") else: file = os.path.abspath(file) link = '%s' % (file, pydoc.html.escape(file)) @@ -108,7 +135,7 @@ if func == '?': call = '' else: - call = 'in %s' % func + inspect.formatargvalues( + call = _('in %s') % func + inspect.formatargvalues( args, varargs, varkw, locals, formatvalue=lambda value: '=' + pydoc.html.repr(value)) @@ -189,4 +216,4 @@ print breaker() print html() -# vim: set filetype=python ts=4 sw=4 et si +# vim: set filetype=python ts=4 sw=4 et si :