Back to index

moin  1.9.0~rc2
render.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     werkzeug.debug.render
00004     ~~~~~~~~~~~~~~~~~~~~~
00005 
00006     Render the traceback debugging page.
00007 
00008     :copyright: (c) 2009 by the Werkzeug Team, see AUTHORS for more details.
00009     :license: BSD, see LICENSE for more details.
00010 """
00011 import pprint
00012 from os.path import dirname, join
00013 
00014 from werkzeug.templates import Template
00015 
00016 
00017 def get_template(name):
00018     return Template.from_file(join(dirname(__file__), 'shared', name),
00019                               unicode_mode=False, errors='ignore')
00020 
00021 
00022 def load_resource(res):
00023     try:
00024         f = file(join(dirname(__file__), 'shared', res))
00025     except IOError:
00026         return ''
00027     try:
00028         return f.read()
00029     finally:
00030         f.close()
00031 
00032 
00033 t_body = get_template('body.tmpl')
00034 t_codetable = get_template('codetable.tmpl')
00035 t_vartable = get_template('vartable.tmpl')
00036 
00037 
00038 def code_table(frame):
00039     from werkzeug.debug.util import Namespace
00040     lines = []
00041     lineno = frame['context_lineno']
00042     if lineno is not None:
00043         lineno += 1
00044         for l in frame['pre_context']:
00045             lines.append(Namespace(mode='pre', lineno=lineno, code=l))
00046             lineno += 1
00047         lines.append(Namespace(mode='cur', lineno=lineno,
00048                                code=frame['context_line']))
00049         lineno += 1
00050         for l in frame['post_context']:
00051             lines.append(Namespace(mode='post', lineno=lineno, code=l))
00052             lineno += 1
00053     else:
00054         lines.append(Namespace(mode='cur', lineno=1,
00055                                code='Sourcecode not available'))
00056 
00057     return t_codetable.render(lines=lines)
00058 
00059 
00060 def var_table(var):
00061     def safe_pformat(x):
00062         try:
00063             lines = pprint.pformat(x).splitlines()
00064         except:
00065             return '?'
00066         tmp = []
00067         for line in lines:
00068             if len(line) > 79:
00069                 line = line[:79] + '...'
00070             tmp.append(line)
00071         return '\n'.join(tmp)
00072 
00073     # dicts
00074     if isinstance(var, dict) or hasattr(var, 'items'):
00075         value = var.items()
00076         if not value:
00077             typ = 'empty'
00078         else:
00079             typ = 'dict'
00080             value.sort()
00081             value = [(repr(key), safe_pformat(val)) for key, val in value]
00082 
00083     # lists
00084     elif isinstance(var, list):
00085         if not var:
00086             typ = 'empty'
00087         else:
00088             typ = 'list'
00089         value = [safe_pformat(item) for item in var]
00090 
00091     # others
00092     else:
00093         typ = 'simple'
00094         value = repr(var)
00095 
00096     return t_vartable.render(type=typ, value=value)
00097 
00098 
00099 def debug_page(context):
00100     tc = context.to_dict()
00101     tc['var_table'] = var_table
00102     tc['code_table'] = code_table
00103     return t_body.render(tc)