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.

22 Responses to “Ubiquity’s Python Feed Plugin”

  1. sep332 Says:

    What version of Python is used/required?

    Also, REL=”python-commands” seems prone to namespace clutter issues. I don’t know much about this stuff, but could you have ?

  2. Jimmy Says:

    This is exactly what I have been waiting for. Now I will definitely start making some ubiquity commands.

    Thank you very much.

  3. Atul Says:

    @sep332: We’re actually just using whatever version of Python is already on the user’s system; I think that jsbridge and the feed server use features that require at least Python 2.4, but I’m pretty sure we don’t use anything specific to 2.5 or 2.6.

  4. Danny Says:

    Woah. Thank you!

  5. Ben Keating Says:

    *very nice*
    It was a given that you’d be the guy to get python involved. ;)

  6. Philippe Says:

    Cool, but I can’t find the command. I updated to the latest beta with the link on this page, but I don’t see the sample command anywhere.

  7. Atul Says:

    @Philippe: Sorry, it should be listed in the first section of the right column of the about:ubiquity page, under the heading “Thanks for trying out Ubiquity 0.2pre13!”.

  8. Tempura Says:

    What about http://pyxpcomext.mozdev.org ? They bring Python in an xpi for all major plattforms, without the need of an local installed interpreter.

  9. james Says:

    Why not use MozRepl with a custom interactor? Then you could write a feed process in any language.

    MozRepl:
    http://wiki.github.com/bard/mozrepl

  10. sep332 Says:

    oops, forgot xml & blog comments don’t mix :-)

    I meant
    REL=”python-commands”
    might become
    REL=”ubiquity-commands” LANG=”python”

  11. PyXPCOM vs. jsbridge at Toolness Says:

    [...] Tempura left a good question as a comment on my blog post concerning Ubiquity’s experimental support for Python: What about http://pyxpcomext.mozdev.org ? They bring Python in an xpi for all major plattforms, [...]

  12. Atul Says:

    @Tempura: Good question! I’ve answered it in a new blog post here.

    @james: That’s a great idea! I knew that jsbridge was built on MozRepl, but I didn’t know MozRepl made it easy to communicate with any language. We’ll definitely have to look into this.

    @sep332: Good idea; right now the LINK REL tag only works the way it does because that’s the way Feed Plugins are currently implemented–so it was the easiest way to get a proof-of-concept out–but your suggestion makes things more readable.

  13. Jimmy Says:

    I have two versions of python installed on my mac. 2.5 and 2.6. Any clue how to get it to point to my 2.6 install rather than my 2.5.

    P.S. Can’t wait to convert my enso scripts to ubiquity. I can finally change songs in itunes from ubiquity.

  14. Atul Says:

    @Jimmy: Good question; right now the location of the Python executable is hardcoded to /usr/bin/python, so we’ll make that a user preference soon. In the mean time, you could try having that executable point to 2.6, though it could screw up other things on your system, or you could hack Ubiquity’s source code to get that to work.

    Also, we don’t yet actually support noun types on Python commands, so you won’t be able to make a very wide variety of Python-based commands just yet–but more support is on the way!

  15. Lakshman Says:

    Is this a python wrapper for Ubiquity? If Yes, Cool!!

  16. Konrad Says:

    Quite cool stuff, Atul!

    I am really keen on getting this running on OpenBSD – which is not the case so far. :)

    I linked /usr/local/bin/python to /usr/bin/python and also got the “Before subscribing …” message. Then I subscribe to the command and created one myself and subscribed to it. But the command is not in the command list.

    The ubiquity_python.log in the subfolder of .firefox ends with “DONE:SUCCESS” so the installation seems to be fine. The python in the bin folder works. In the ubiquity_ann.sqlite db I found an entry of the command. I am not sure what’s wrong. Do you have any proposal what I could do/check? Many thanks in advance!

    Cheers!

    Konrad

  17. Mandar Vaze Says:

    I am unable to get this sample python command to work. I have Ubiquity 0.2pre13 running on Firefox 3.1 Beta 2 on Ubuntu 8.10
    I have subscribed to both LDFP and WFP, got subscription successful msg for both.
    But I don’t see additional sample commands, neither “Hello from Python”
    I understand this is WAY early, but more details would be helpful

  18. Pywinder Singh Says:

    I’m having the same issue that Mandar is, except I’m running on OSX 10.5.6.

  19. PyWinder Sing Says:

    Fixed the issue… Extra invisible characters when pasting from webpage source. (which actually makes it a python problem, sort of. ;) )

    (to save other users the same headache, check your firefox console log for:

    ^ SyntaxError: Invalid Syntax
    )

  20. Mandar Vaze Says:

    Upgraded to 0.2pre14, and got the link to subscribe to “example Python Feed Plugin command feed”. It is 4th link under “Thanks for trying out Ubiquity 0.2pre14″ on right-top. Also got the ubiquity notification, as explained above.

  21. Yuppie Says:

    Seems that this is a client side scripting with Python in Mozilla. Isn`t it more usefull to deploy the Python environment like Python Extension pythonext does ?

  22. Clochix Says:

    I’m playing with Jetpack, and it seems to be close to Ubiquity. So, do you think it will be possible to also use Python with Jetpack ? (I see there is a jsbridge.js file into Jetpack).

    I think it would be a good news, because Python seems to be a better language for learning how to programm.