February 10, 2009

Ubiquity's Python Feed Plugin

A few weeks ago I wrote about Ubiquity Feed Plugins, which are basically just a way of separating the user interface of subscribing to a new feature from the implementation of the feature itself.

As I’ve written about before, one of the things I’ve missed about the Mozilla development environment is its support for the Python programming language. Aside from being humane and having a great community, it has functionality that could complement the Mozilla platform quite nicely. So we’ve whipped up a quick proof-of-concept Python Feed Plugin for Ubiquity to explore this possibility.

Here’s a really simple command feed written in Python:

def cmd_sample_python_command(ubiquity):
    ubiquity.displayMessage('Hello from Python %s.' % sys.version)

cmd_sample_python_command.preview = 'a sample python command!'

For anyone who knows Python and has read the Ubiquity Author Tutorial, this should be fairly self-explanatory. The ubiquity argument is just passed in to the command execution function to make unit testing as easy as possible.

As with any Ubiquity feed, the file containing the above code needs to be referenced in the <HEAD> element of a web page:

<LINK REL=“python-commands” HREF=“sample_command.py”>

In this case the python-commands value of the REL attribute is what tells Ubiquity to process this feed using the Python feed plugin.

Visiting the web page containing this tag results in the same subscripton panel that shows up with any page that has a Ubiquity command feed. Because Python code executes at the same privilege level as default Ubiquity feeds, the Python feed plugin really ought to display the same warning page that default Ubiquity feeds show when the user clicks “Subscribe”; but because this is just a proof of concept, we skipped that implementation and currently only allow Python feeds to be used when they already exist on the user’s local filesystem—in other words, when the feed is at a file: URL.

The first time an end-user subscribes to a Python command feed, they’re presented with this non-intrusive message:

What happens behind-the-scenes is a bit more complex: Ubiquity uses virtualenv to bootstrap a sandboxed Python virtual environment inside the user’s profile directory. It then uses easy_install to install jsbridge, and then sets up a “feed server” process on the user’s system, which Ubiquity communicates with over jsbridge to execute Python code:

Pretty Frankenstein, but it works on OS X and Linux. Because Windows doesn’t come with Python built-in like the other two operating systems do, we’ll have to bootstrap it first, which we haven’t gotten around to yet.

So, if you’ve got OS X or Linux and like Python, feel free to play around with this; a sample Python command feed is included with the latest Ubiquity beta. If this is something that seems interesting to you, we’d love to know—you’re welcome to leave a comment on this post, drop us a line on the ubiquity-firefox Google Group, or say hello on #ubiquity on irc.mozilla.org.

© Atul Varma 2017