def objgraph(self, request): import objgraph limit = int(request.args.get('limit', 10)) types = objgraph.most_common_types(limit=limit, shortnames=False) leaking = objgraph.most_common_types( limit=limit, objects=objgraph.get_leaking_objects(), shortnames=False, ) # html only links limits = [ 'Number of items: ', Link('{}', request.path + '?limit={}', 10).html(), Link('{}', request.path + '?limit={}', 20).html(), Link('{}', request.path + '?limit={}', 50).html(), ] return info_response( request, 'Python Objects', Content( 'Python Objects for Worker pid {}'.format(os.getpid()), 'h1', ), Content(' '.join(limits), 'p', text=False, escape=False), Content('Most Common Objects', 'h2'), Table(types), Content('Leaking Objects (no referrer)', 'h2'), Table(leaking), )
def test_link(): assert Link('link', '/link').html() == '<a href="/link">link</a>' assert Link('{}', '/link/{}', 'x').html() == '<a href="/link/x">x</a>' assert Link('{foo}', '/link/{foo}', foo='bar').html() == ('<a href="/link/bar">bar</a>') assert Link('link', '/link').text() == '/link' assert Link( 'link', '/link', host='http://example.com').text() == ('http://example.com/link')
def packages(self, request): """List of python packages installed.""" import pkg_resources rows = [] for p in sorted( pkg_resources.working_set, key=lambda p: p.project_name): rows.append(( p.project_name, p._version, p.location, Link( 'PyPI', 'https://pypi.org/project/{}/{}/', p.project_name, p._version, ), )) return info_response( request, 'Python Packages', Table( rows, headers=['Package', 'Version', 'Location', 'PyPI Link'], ) )
def test_table(): rows = [ ['a', 'b', 'c'], ['d', 'e', Link('foo', '/foo')], ] table = Table(rows, headers=['1', '2', '3']) assert table.html() == textwrap.dedent(""" <table> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> </tr> </thead> <tbody> <tr> <td>a</td> <td>b</td> <td>c</td> </tr> <tr> <td>d</td> <td>e</td> <td><a href="/foo">foo</a></td> </tr> </tbody> </table> """).strip() # Add # characters to stop editors striping eol whitespace! assert table.text() == textwrap.dedent(""" 1 2 3 # ----------# a b c # d e /foo# ----------# """).replace('#', '').lstrip()
def index(self, request): methods = [] base = request.host_url.rstrip('/') for url, funcname in self.urlmap.items(): if funcname is None: continue if url in self.no_index: continue try: func = getattr(self, funcname) except AttributeError: pass else: methods.append(( Link(url, self.prefix + url, host=base), str(func.__doc__), )) return info_response( request, 'Status', Table(methods), )
def test_table(): rows = [ ['a', 'b', 'c'], ['d', 'e', Link('foo', '/foo')], ] table = Table(rows, headers=['1', '2', '3'], id='table') assert table.html() == textwrap.dedent(""" <table> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> </tr> </thead> <tbody> <tr> <td>a</td> <td>b</td> <td>c</td> </tr> <tr> <td>d</td> <td>e</td> <td><a href="/foo">foo</a></td> </tr> </tbody> </table> """).strip() # Add # characters to stop editors striping eol whitespace! assert table.text() == textwrap.dedent(""" 1 2 3 # ----------# a b c # d e /foo# ----------# """).replace('#', '').lstrip() id, obj = table._json() assert id == 'table' assert obj == [ { '1': 'a', '2': 'b', '3': 'c' }, { '1': 'd', '2': 'e', '3': { 'href': '/foo', 'text': 'foo' } }, ] # special case in json for 2 column tables table2 = Table(list(dict(a=1, b=2).items()), id='table') assert table2._json() == ('table', {'a': 1, 'b': 2})