11#
22# This module was written by Ka-Ping Yee, <[email protected] >. 33#
4- # $Id: cgitb.py,v 1.11 2004-06-09 09:20:01 a1s Exp $
4+ # $Id: cgitb.py,v 1.12 2004-07-13 10:18:00 a1s Exp $
55
66"""Extended CGI traceback handler by Ka-Ping Yee, <[email protected] >. 77"""
1010import sys , os , types , string , keyword , linecache , tokenize , inspect , cgi
1111import pydoc , traceback
1212
13- from roundup .cgi import templating
13+ from roundup .cgi import templating , TranslationService
1414
15- def _ (msgid ):
16- return templating .translationService .gettext (msgid )
15+ def get_translator (i18n = None ):
16+ """Return message translation function (gettext)
17+
18+ Parameters:
19+ i18n - translation service, such as roundup.i18n module
20+ or TranslationService object.
21+
22+ Return ``gettext`` attribute of the ``i18n`` object, if available
23+ (must be a message translation function with one argument).
24+ If ``gettext`` cannot be obtained from ``i18n``, take default
25+ TranslationService.
26+
27+ """
28+ try :
29+ return i18n .gettext
30+ except :
31+ return TranslationService .get_translation ().gettext
1732
1833def breaker ():
1934 return ('<body bgcolor="white">' +
@@ -27,12 +42,14 @@ def niceDict(indent, dict):
2742 cgi .escape (repr (v ))))
2843 return '\n ' .join (l )
2944
30- def pt_html (context = 5 ):
45+ def pt_html (context = 5 , i18n = None ):
46+ _ = get_translator (i18n )
3147 esc = cgi .escape
32- l = ['<h1>Templating Error</h1>' ,
33- '<p><b>%s</b>: %s</p>' % (esc (str (sys .exc_type )),
34- esc (str (sys .exc_value ))),
35- '<p class="help">Debugging information follows</p>' ,
48+ exc_info = [esc (str (value )) for value in sys .exc_info ()[:2 ]]
49+ l = [_ ('<h1>Templating Error</h1>\n '
50+ '<p><b>%(exc_type)s</b>: %(exc_value)s</p>\n '
51+ '<p class="help">Debugging information follows</p>'
52+ ) % {'exc_type' : exc_info [0 ], 'exc_value' : exc_info [1 ]},
3653 '<ol>' ,]
3754 from roundup .cgi .PageTemplates .Expressions import TraversalError
3855 t = inspect .trace (context )
@@ -44,50 +61,60 @@ def pt_html(context=5):
4461 if isinstance (ti , TraversalError ):
4562 s = []
4663 for name , info in ti .path :
47- s .append ('<li>"%s" (%s)</li>' % (name , esc (repr (info ))))
64+ s .append (_ ('<li>"%(name)s" (%(info)s)</li>' )
65+ % {'name' : name , 'info' : esc (repr (info ))})
4866 s = '\n ' .join (s )
49- l .append ('<li>Looking for "%s", current path:<ol>%s</ol></li>' % (
50- ti .name , s ))
67+ l .append (_ ('<li>Looking for "%(name)s", '
68+ 'current path:<ol>%(path)s</ol></li>'
69+ ) % {'name' : ti .name , 'path' : s })
5170 else :
52- l .append ('<li>In %s</li>' % esc (str (ti )))
71+ l .append (_ ( '<li>In %s</li>' ) % esc (str (ti )))
5372 if locals .has_key ('__traceback_supplement__' ):
5473 ts = locals ['__traceback_supplement__' ]
5574 if len (ts ) == 2 :
5675 supp , context = ts
57- s = 'A problem occurred in your template "%s".' % str (context .id )
76+ s = _ ('A problem occurred in your template "%s".' ) \
77+ % str (context .id )
5878 if context ._v_errors :
5979 s = s + '<br>' + '<br>' .join (
6080 [esc (x ) for x in context ._v_errors ])
6181 l .append ('<li>%s</li>' % s )
6282 elif len (ts ) == 3 :
6383 supp , context , info = ts
64- l .append ('''
65- <li>While evaluating the %r expression on line %d
84+ l .append (_ ( '''
85+ <li>While evaluating the %(info) r expression on line %(line) d
6686<table class="otherinfo" style="font-size: 90%%">
6787 <tr><th colspan="2" class="header">Current variables:</th></tr>
68- %s
69- %s
88+ %(globals) s
89+ %(locals) s
7090</table></li>
71- ''' % (info , context .position [0 ], niceDict (' ' , context .global_vars ),
72- niceDict (' ' , context .local_vars )))
91+ ''' ) % {
92+ 'info' : info ,
93+ 'line' : context .position [0 ],
94+ 'globals' : niceDict (' ' , context .global_vars ),
95+ 'locals' : niceDict (' ' , context .local_vars )
96+ })
7397
7498 l .append ('''
7599</ol>
76100<table style="font-size: 80%%; color: gray">
77- <tr><th class="header" align="left">Full traceback: </th></tr>
101+ <tr><th class="header" align="left">%s </th></tr>
78102 <tr><td><pre>%s</pre></td></tr>
79- </table>''' % cgi .escape ('' .join (traceback .format_exception (sys .exc_type ,
80- sys .exc_value , sys .exc_traceback ))))
103+ </table>''' % (_ ('Full traceback:' ), cgi .escape ('' .join (
104+ traceback .format_exception (* sys .exc_info ())
105+ ))))
81106 l .append ('<p> </p>' )
82107 return '\n ' .join (l )
83108
84- def html (context = 5 ):
109+ def html (context = 5 , i18n = None ):
110+ _ = get_translator (i18n )
85111 etype , evalue = sys .exc_type , sys .exc_value
86112 if type (etype ) is types .ClassType :
87113 etype = etype .__name__
88114 pyver = 'Python ' + string .split (sys .version )[0 ] + '<br>' + sys .executable
89115 head = pydoc .html .heading (
90- '<font size=+1><strong>%s</strong>: %s</font>' % (etype , evalue ),
116+ _ ('<font size=+1><strong>%(exc_type)s</strong>: %(exc_value)s</font>' )
117+ % {'exc_type' : etype , 'exc_value' : evalue },
91118 '#ffffff' , '#777777' , pyver )
92119
93120 head = head + (_ ('<p>A problem occurred while running a Python script. '
@@ -99,16 +126,16 @@ def html(context=5):
99126 traceback = []
100127 for frame , file , lnum , func , lines , index in inspect .trace (context ):
101128 if file is None :
102- link = ''' <file is None - probably inside <tt>eval</tt> or
103- <tt>exec</tt>>'''
129+ link = _ ( " <file is None - probably inside <tt>eval</tt> "
130+ "or <tt>exec</tt>>" )
104131 else :
105132 file = os .path .abspath (file )
106133 link = '<a href="file:%s">%s</a>' % (file , pydoc .html .escape (file ))
107134 args , varargs , varkw , locals = inspect .getargvalues (frame )
108135 if func == '?' :
109136 call = ''
110137 else :
111- call = 'in <strong>%s</strong>' % func + inspect .formatargvalues (
138+ call = _ ( 'in <strong>%s</strong>' ) % func + inspect .formatargvalues (
112139 args , varargs , varkw , locals ,
113140 formatvalue = lambda value : '=' + pydoc .html .repr (value ))
114141
@@ -189,4 +216,4 @@ def handler():
189216 print breaker ()
190217 print html ()
191218
192- # vim: set filetype=python ts=4 sw=4 et si
219+ # vim: set filetype=python ts=4 sw=4 et si :
0 commit comments