changeset 18:400a9edd79ad

Added a latest_jetpack_rev command
author Atul Varma <varmaa@toolness.com>
date Wed, 31 Mar 2010 16:53:38 -0700
parents 557f0edc2ca4
children 854065d98c3d
files my-enso-commands.py
diffstat 1 files changed, 66 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 = '<a href="%s">%s</a>' % (info['link'],
+                                    html_escape(info['title']))
+
+    ensoapi.set_selection({"text": text, "html": html})
+
+# Taken from http://wiki.python.org/moin/EscapingHtml
+
+html_escape_table = {
+    "&": "&amp;",
+    '"': "&quot;",
+    "'": "&apos;",
+    ">": "&gt;",
+    "<": "&lt;",
+    }
+
+def html_escape(text):
+    """Produce entities within text."""
+
+    return "".join(html_escape_table.get(c,c) for c in text)