# HG changeset patch # User Atul Varma # Date 1270079618 25200 # Node ID 400a9edd79adf41e125d47ab470908c405380971 # Parent 557f0edc2ca4fc2d14d830780bdd3c48c3ce982a Added a latest_jetpack_rev command diff -r 557f0edc2ca4 -r 400a9edd79ad my-enso-commands.py --- a/my-enso-commands.py Sun Mar 28 17:00:39 2010 -0700 +++ b/my-enso-commands.py Wed Mar 31 16:53:38 2010 -0700 @@ -10,6 +10,8 @@ import logging from StringIO import StringIO +import simplejson as json + def _do_service(ensoapi, serviceName, text=None): import AppKit @@ -963,3 +965,67 @@ "Generates a new issue of The Daily Edition with old news." return _gen_daily_edition(ensoapi) + +class SubprocessRunner(object): + def __init__(self, cmdline): + self.cmdline = cmdline + self.output_text = None + + def run(self): + output_fd, output_name = tempfile.mkstemp() + + popen = subprocess.Popen(self.cmdline, + stdout = output_fd, + stderr = subprocess.STDOUT) + + while popen.poll() is None: + yield + + os.close(output_fd) + output_text = open(output_name, "r").read().strip() + os.remove(output_name) + + if popen.returncode: + raise Exception("Process failed with output %s" % + repr(output_text)) + + self.output_text = output_text + +def cmd_latest_jetpack_rev(ensoapi): + "Dumps info about the latest Jetpack revision." + + path = "~/Documents/MyPythonPackage/scripts/get_latest_feed_entry.py" + url = "http://hg.mozilla.org/labs/jetpack-sdk/atom-log" + cmdline = [sys.executable, os.path.expanduser(path), url] + + runner = SubprocessRunner(cmdline) + + ensoapi.display_message("Please wait.") + + for _ in runner.run(): + yield + + info = json.loads(runner.output_text) + + text = "\n".join([" %s" % info['title'], + " By %s" % info['author'], + " %s" % info['link']]) + html = '%s' % (info['link'], + html_escape(info['title'])) + + ensoapi.set_selection({"text": text, "html": html}) + +# Taken from http://wiki.python.org/moin/EscapingHtml + +html_escape_table = { + "&": "&", + '"': """, + "'": "'", + ">": ">", + "<": "<", + } + +def html_escape(text): + """Produce entities within text.""" + + return "".join(html_escape_table.get(c,c) for c in text)