[three]Bean

New tw2.devtools WidgetBrowser features

May 02, 2011 | categories: python, toscawidgets View Comments

I pumped out two new tw2.devtools WidgetBrowser features this afternoon -- you can see them live at http://tw2-demos.threebean.org.

  • A view source link that pops open a jquery-ui dialog with the nicely formatted sourcecode of the demo widget rendered by the widget browser (hg commit).
  • Metadata pulled from pypi including the latest release available and the total number of downloads across all releases (hg commit).
---

The 'view source' link stuff is pretty cool. I got the idea and the details both from the moksha demo dashboard using inspect and pygments. Here's the relevant piece of code:

import warnings
import inspect
import pygments
import pygments.lexers
import pygments.formatters

def prepare_source(s):
    try:
        source = inspect.getsource(s)
    except IOError as e:
        warnings.warn(repr(s) + " : " + str(e))
        return ""

    html_args = {'full': False}
    code = pygments.highlight(
        source,
        pygments.lexers.PythonLexer(),
        pygments.formatters.HtmlFormatter(**html_args)
    )

    return code
---

The metadata part was trickier but to get the same information yourself, run:

import xmlrpclib

module = 'tw2.core'

pypi = xmlrpclib.ServerProxy('http://pypi.python.org/pypi')

total = sum([
    sum([
        d['downloads'] for d in pypi.release_urls(module, version)
    ])
    for version in pypi.package_releases(module, True)
])

print module, total
View Comments
blog comments powered by Disqus