""" Shows any variables that were put in ``environ['paste.testing_variables']`` at the bottom of each page. """ from wsgifilter import Filter from webhelpers.util import html_escape import re import pprint def smart_quote(s): s = html_escape(s) def space_repl(match): return match.group(0).replace(' ', '  ') s = s.replace('\t', ' ') s = re.sub(r' +', space_repl, s) s = s.replace('\n', '
\n') return s def safe_repr(value): try: return pprint.pformat(value) except Exception, e: return 'Error in pprint: %s' % e class TestingVars(Filter): standard_css = ''' ''' def __call__(self, environ, start_response): environ.setdefault('paste.testing_variables', {}) return super(TestingVars, self).__call__(environ, start_response) def filter(self, environ, headers, data): testing_vars = environ.get('paste.testing_variables') if not testing_vars: return data var_block = self.format_vars(testing_vars) new_body = self.add_to_end_of_body(data, var_block) return new_body def format_vars(self, vars): vars = vars.items() vars.sort() rows = [] for name, value in vars: name = smart_quote(name) value = smart_quote(safe_repr(value)) rows.append(''' %s %s ''' % (name, value)) table = ''' %s
Testing Variables
''' % (''.join(rows)) return self.standard_css + table end_body_re = re.compile(r'', re.I) def add_to_end_of_body(self, html, block): match = self.end_body_re.search(html) if match: return html[:match.start()] + block + html[match.end():] else: return html + block