Mercurial > my-enso-commands
annotate my-enso-commands.py @ 19:854065d98c3d default tip
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
| author | Atul Varma <varmaa@toolness.com> |
|---|---|
| date | Sun, 04 Apr 2010 19:48:18 -0700 |
| parents | 400a9edd79ad |
| children |
| rev | line source |
|---|---|
| 0 | 1 # -*-Mode:python-*- |
| 2 | |
| 3 import os | |
| 4 import tempfile | |
| 5 import sys | |
| 6 import subprocess | |
| 7 import webbrowser | |
| 8 import urllib | |
| 9 import threading | |
|
1
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
10 import logging |
| 0 | 11 from StringIO import StringIO |
| 12 | |
|
18
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
13 import simplejson as json |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
14 |
|
14
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
15 def _do_service(ensoapi, serviceName, text=None): |
|
1
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
16 import AppKit |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
17 |
|
14
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
18 if not text: |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
19 text = ensoapi.get_selection().get("text", "").strip() |
|
2
c28ae8f664ba
Removed a bunch of diagnostic or highly experimental commands. Also removed the PyXPCOM commands b/c I'm not running Enso on top of Firefox right now, but they're still stored in the repository for future use.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
20 |
|
c28ae8f664ba
Removed a bunch of diagnostic or highly experimental commands. Also removed the PyXPCOM commands b/c I'm not running Enso on top of Firefox right now, but they're still stored in the repository for future use.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
21 if not text: |
|
c28ae8f664ba
Removed a bunch of diagnostic or highly experimental commands. Also removed the PyXPCOM commands b/c I'm not running Enso on top of Firefox right now, but they're still stored in the repository for future use.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
22 ensoapi.display_message("No text selection!") |
|
c28ae8f664ba
Removed a bunch of diagnostic or highly experimental commands. Also removed the PyXPCOM commands b/c I'm not running Enso on top of Firefox right now, but they're still stored in the repository for future use.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
23 return |
|
1
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
24 |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
25 pb = AppKit.NSPasteboard.pasteboardWithName_("EnsoServiceContent") |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
26 |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
27 pb.declareTypes_owner_( [AppKit.NSStringPboardType], None ) |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
28 |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
29 try: |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
30 if not pb.setString_forType_( text, AppKit.NSStringPboardType ): |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
31 raise Exception( "Failed to set pasteboard data." ) |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
32 |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
33 if not AppKit.NSPerformService( serviceName, pb ): |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
34 raise Exception( "Failed to perform service." ) |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
35 finally: |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
36 pass |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
37 |
|
14
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
38 def cmd_define(ensoapi, word=None): |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
39 "Look up the given word in the dictionary." |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
40 |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
41 _do_service(ensoapi, "Look Up in Dictionary", word) |
|
3
9767abfe290b
Added more service-oriented commands.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
42 |
| 16 | 43 DEFAULT_COUNTRY_CODE = "1" |
| 44 DEFAULT_AREA_CODE = "415" | |
| 45 | |
| 46 def number_to_pstn(number, default_country_code=DEFAULT_COUNTRY_CODE, | |
| 47 default_area_code=DEFAULT_AREA_CODE): | |
| 48 """ | |
| 49 Converts a colloquial phone number to a pstn. | |
| 50 | |
| 51 >>> number_to_pstn('123.456.7890', default_country_code='1') | |
| 52 '+11234567890' | |
| 53 | |
| 54 >>> number_to_pstn('456.7890', default_country_code='1', | |
| 55 ... default_area_code='614') | |
| 56 '+16144567890' | |
| 57 | |
| 58 >>> number_to_pstn('5') is None | |
| 59 True | |
| 60 """ | |
| 61 | |
| 62 DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] | |
| 63 | |
| 64 chars = "".join([char for char in number if char in DIGITS]) | |
| 65 | |
| 66 if len(chars) == 11: | |
| 67 pass | |
| 68 if len(chars) == 10: | |
| 69 chars = default_country_code + chars | |
| 70 elif len(chars) == 7: | |
| 71 chars = default_country_code + default_area_code + chars | |
| 72 else: | |
| 73 chars = None | |
| 74 | |
| 75 if chars: | |
| 76 return "+%s" % chars | |
| 77 return None | |
| 78 | |
| 79 def cmd_skype(ensoapi, number=None): | |
| 80 "Call the given number with Skype." | |
| 81 | |
| 82 if not number: | |
| 83 number = ensoapi.get_selection().get("text", "").strip() | |
| 84 | |
| 85 if not number: | |
| 86 ensoapi.display_message("No text selection!") | |
| 87 return | |
| 88 | |
| 89 number = number_to_pstn(number) | |
| 90 | |
| 91 if not number: | |
| 92 ensoapi.display_message("That doesn't look like a phone number.") | |
| 93 return | |
| 94 | |
| 95 _runAppleScript('tell application "Skype" ' | |
| 96 'to send command "CALL %s" ' | |
| 97 'script name "Enso"' % number) | |
| 98 | |
|
14
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
99 def cmd_speak(ensoapi, what=None): |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
100 "Speak the given text out loud." |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
101 |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
102 _do_service(ensoapi, "Speech/Start Speaking Text", what) |
|
3
9767abfe290b
Added more service-oriented commands.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
103 |
|
9767abfe290b
Added more service-oriented commands.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
104 def cmd_summarize(ensoapi): |
|
14
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
105 "Summarize the selected text." |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
106 |
|
3
9767abfe290b
Added more service-oriented commands.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
107 _do_service(ensoapi, "Summarize") |
|
9767abfe290b
Added more service-oriented commands.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
108 |
|
14
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
109 def cmd_spotlight(ensoapi, query=None): |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
110 "Search your computer for the given query." |
|
3
9767abfe290b
Added more service-oriented commands.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
111 |
|
14
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
112 _do_service(ensoapi, "Spotlight", query) |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
113 |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
114 def cmd_line_count(ensoapi): |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
115 "Count the number of lines in your selection." |
|
9ca05d02bc3a
Added optional params to some commands
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
116 |
| 0 | 117 seldict = ensoapi.get_selection() |
| 118 ensoapi.display_message( "Selection is %s lines." % | |
| 119 len(seldict.get("text","").splitlines()) ) | |
| 120 | |
| 121 def _get_filelike_timestamp(): | |
| 122 import time | |
| 123 | |
| 124 return time.strftime("%Y%m%d%H%M%S") | |
| 125 | |
|
8
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
126 def _get_current_app(): |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
127 import AppKit |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
128 |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
129 app = AppKit.NSWorkspace.sharedWorkspace().activeApplication() |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
130 return app.objectForKey_("NSApplicationBundleIdentifier") |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
131 |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
132 def maybe_export_photoshop_clipboard(func): |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
133 def wrapper(*args, **kwargs): |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
134 if _get_current_app() == 'com.adobe.Photoshop': |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
135 _runAppleScript('tell application "Adobe Photoshop CS3" ' |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
136 'to copy merged') |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
137 _runAppleScript('tell application "Finder" to activate') |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
138 retval = func(*args, **kwargs) |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
139 _runAppleScript('tell application "Adobe Photoshop CS3" ' |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
140 'to activate') |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
141 return retval |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
142 return func(*args, **kwargs) |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
143 return wrapper |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
144 |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
145 @maybe_export_photoshop_clipboard |
| 0 | 146 def _get_clipboard_img_datafile(): |
| 147 import AppKit | |
| 148 | |
| 149 pb = AppKit.NSPasteboard.generalPasteboard() | |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
150 typestr = pb.availableTypeFromArray_([AppKit.NSTIFFPboardType, |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
151 AppKit.NSPICTPboardType]) |
|
6
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
152 tiffbytes = None |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
153 if typestr == AppKit.NSTIFFPboardType: |
|
6
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
154 tiffbytes = pb.dataForType_(AppKit.NSTIFFPboardType).bytes() |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
155 elif typestr == AppKit.NSPICTPboardType: |
|
6
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
156 img = AppKit.NSImage.alloc().initWithPasteboard_(pb) |
|
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
157 tiffbytes = img.TIFFRepresentation().bytes() |
|
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
158 |
|
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
159 if tiffbytes: |
|
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
160 return StringIO(str(tiffbytes)) |
|
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
161 return None |
| 0 | 162 |
| 163 def image_command(func): | |
| 164 def wrapper(ensoapi): | |
| 165 import Image | |
| 166 | |
| 167 datafile = _get_clipboard_img_datafile() | |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
168 |
| 0 | 169 if datafile: |
| 170 img = Image.open(datafile) | |
| 171 return func(ensoapi, img) | |
| 172 else: | |
| 173 files = ensoapi.get_selection().get("files", []) | |
| 174 if files: | |
| 175 if len(files) == 1: | |
| 176 filename = files[0] | |
| 177 img = None | |
| 178 try: | |
| 179 img = Image.open(filename) | |
| 180 except IOError, e: | |
| 181 ensoapi.display_message("An error occurred: %s." % e) | |
| 182 if img: | |
| 183 return func(ensoapi, img) | |
| 184 else: | |
| 185 ensoapi.display_message("More than one file selected.") | |
| 186 else: | |
| 187 ensoapi.display_message("No image in clipboard, and no " | |
| 188 "file selected.") | |
| 189 return wrapper | |
| 190 | |
| 191 @image_command | |
| 192 def cmd_get_image_size(ensoapi, img): | |
| 193 outputfile = StringIO() | |
| 194 img.save(outputfile, "PNG") | |
| 195 png_size = outputfile.tell() | |
| 196 outputfile = StringIO() | |
| 197 img.save(outputfile, "JPEG", quality=90) | |
| 198 jpg_size = outputfile.tell() | |
| 199 ensoapi.display_message("png size: %d bytes. jpg-90 size: %d bytes." % | |
| 200 (png_size, jpg_size)) | |
| 201 | |
| 9 | 202 def cmd_mute(ensoapi): |
| 203 """mute the speaker.""" | |
| 204 | |
| 205 _runAppleScript('set volume output muted true') | |
| 206 ensoapi.display_message("Speaker muted.") | |
| 207 | |
| 208 def cmd_unmute(ensoapi): | |
| 209 """unmute the speaker.""" | |
| 210 | |
| 211 _runAppleScript('set volume output muted false') | |
| 212 ensoapi.display_message("Speaker unmuted.") | |
| 213 | |
| 0 | 214 def cmd_unupload_image(ensoapi): |
| 215 url = ensoapi.get_selection().get("text", "") | |
| 216 if url: | |
| 217 if url.startswith(REMOTE_UPLOAD_URL): | |
| 218 filename = url[len(REMOTE_UPLOAD_URL):] | |
| 219 localfile = os.path.join(LOCAL_UPLOAD_DIR, filename) | |
| 220 # It's just easier to upload a truncated file than it is | |
| 221 # to remove the file remotely. | |
| 222 open(localfile, "w").close() | |
| 223 popen = subprocess.Popen( | |
| 224 ["scp", | |
| 225 localfile, | |
| 226 "%s%s" % (REMOTE_UPLOAD_DIR, filename)] | |
| 227 ) | |
| 228 while popen.poll() is None: | |
| 229 yield | |
| 230 if popen.returncode == 0: | |
| 231 os.remove(localfile) | |
| 232 ensoapi.display_message("Image removed.") | |
| 233 else: | |
| 234 ensoapi.display_message("An error occurred.") | |
| 235 else: | |
| 236 ensoapi.display_message("URL is not an upload URL.") | |
| 237 else: | |
| 238 ensoapi.display_message("No selection!") | |
| 239 | |
| 240 LOCAL_UPLOAD_DIR = "/Users/varmaa/Archive/toolness-images/" | |
| 241 REMOTE_UPLOAD_DIR = "toolness.com:/home/varmaa/toolness.com/images/" | |
| 242 REMOTE_UPLOAD_URL = "http://www.toolness.com/images/" | |
| 243 | |
| 244 @image_command | |
| 245 def cmd_upload_image(ensoapi, img): | |
| 246 filename = "%s.jpg" % _get_filelike_timestamp() | |
| 247 localfile = os.path.join(LOCAL_UPLOAD_DIR, filename) | |
| 248 img.save(localfile, quality=90) | |
| 249 | |
| 250 ensoapi.display_message("Uploading image...") | |
| 251 | |
| 252 popen = subprocess.Popen( | |
| 253 ["scp", | |
| 254 localfile, | |
| 255 "%s%s" % (REMOTE_UPLOAD_DIR, filename)] | |
| 256 ) | |
| 257 while popen.poll() is None: | |
| 258 yield | |
| 259 if popen.returncode == 0: | |
| 260 webbrowser.open("%s%s" % (REMOTE_UPLOAD_URL, filename)) | |
| 261 else: | |
| 262 ensoapi.display_message("An error occurred.") | |
| 263 | |
| 264 class ThreadedFunc(threading.Thread): | |
| 265 def __init__(self, target): | |
| 266 self.__target = target | |
| 267 threading.Thread.__init__(self) | |
| 268 self.__success = False | |
| 269 self.__retval = None | |
| 270 self.start() | |
| 271 | |
| 272 def run(self): | |
| 273 self.__retval = self.__target() | |
| 274 self.__success = True | |
| 275 | |
| 276 def wasSuccessful(self): | |
| 277 if self.isAlive(): | |
| 278 raise Exception( "Thread not finished" ) | |
| 279 return self.__success | |
| 280 | |
| 281 def getRetval(self): | |
| 282 if self.isAlive(): | |
| 283 raise Exception( "Thread not finished" ) | |
| 284 return self.__retval | |
| 285 | |
| 286 def htmlifier(func): | |
| 287 def wrapper(ensoapi): | |
| 288 seldict = ensoapi.get_selection() | |
| 289 text = seldict.get("text", "") | |
| 290 html = seldict.get("html", text) | |
| 291 if not text: | |
| 292 ensoapi.display_message("No selection!") | |
| 293 else: | |
| 294 result = func(ensoapi, html) | |
| 295 ensoapi.set_selection( | |
| 296 {"html":result, | |
| 297 "text":result} | |
| 298 ) | |
| 299 | |
| 300 return wrapper | |
| 301 | |
| 302 @htmlifier | |
| 303 def cmd_bold(ensoapi, text): | |
| 304 return "<b>%s</b>" % text | |
| 305 | |
| 306 @htmlifier | |
| 307 def cmd_italics(ensoapi, text): | |
| 308 return "<i>%s</i>" % text | |
| 309 | |
| 310 @htmlifier | |
| 311 def cmd_monospace(ensoapi, text): | |
|
6
c41c4896c97e
upload image cmd now accepts more image types in clipboard, changed cmd_monospace to use tt instead of pre.
Atul Varma <varmaa@toolness.com>
parents:
5
diff
changeset
|
312 return "<tt>%s</tt>" % text |
| 0 | 313 |
| 314 def cmd_normalize(ensoapi): | |
| 315 normal_template = "<span style=\"font-weight: normal;\">%s</span>" | |
| 316 seldict = ensoapi.get_selection() | |
| 317 text = seldict.get("text", "") | |
| 318 if not text: | |
| 319 ensoapi.display_message("No selection!") | |
| 320 else: | |
| 321 ensoapi.set_selection( | |
| 322 {"html":normal_template % text, | |
| 323 "text":text} ) | |
| 324 | |
| 325 def make_get_url_func(url): | |
| 326 def get_url(): | |
| 327 import urllib2 | |
| 328 | |
| 329 f = urllib2.urlopen(url) | |
| 330 return f.read() | |
| 331 return get_url | |
| 332 | |
| 333 def get_weather(xml_data): | |
| 334 """ | |
| 335 Shows the weather for the given place. | |
| 336 """ | |
| 337 | |
| 338 import elementtree.ElementTree as ET | |
| 339 | |
| 340 page = ET.fromstring(xml_data) | |
| 341 | |
| 342 weather = page.find( "weather/current_conditions" ) | |
| 343 | |
| 344 return { | |
| 345 'f' : weather.find( "temp_f" ).get( "data" ), | |
| 346 'c' : weather.find( "temp_c" ).get( "data" ), | |
| 347 'humidity' : weather.find( "humidity" ).get( "data" ), | |
| 348 'wind' : weather.find( "wind_condition" ).get( "data" ) | |
| 349 } | |
| 350 | |
| 351 def test_get_weather(): | |
| 352 xml_data = """<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0"><forecast_information><city data="Chicago, IL"/><postal_code data="60657"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2008-04-07"/><current_date_time data="2008-04-07 06:38:00 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data="Cloudy"/><temp_f data="57"/><temp_c data="14"/><humidity data="Humidity: 47%"/><icon data="/images/weather/cloudy.gif"/><wind_condition data="Wind: N at 0 mph"/></current_conditions><forecast_conditions><day_of_week data="Today"/><low data="40"/><high data="56"/><icon data="/images/weather/cloudy.gif"/><condition data="Cloudy"/></forecast_conditions><forecast_conditions><day_of_week data="Tue"/><low data="45"/><high data="54"/><icon data="/images/weather/thunderstorm.gif"/><condition data="Thunderstorm"/></forecast_conditions><forecast_conditions><day_of_week data="Wed"/><low data="40"/><high data="54"/><icon data="/images/weather/mostly_sunny.gif"/><condition data="Partly Sunny"/></forecast_conditions><forecast_conditions><day_of_week data="Thu"/><low data="38"/><high data="49"/><icon data="/images/weather/chance_of_rain.gif"/><condition data="Chance of Showers"/></forecast_conditions></weather></xml_api_reply>""" | |
| 353 | |
| 354 assert get_weather(xml_data) == {'c': '14', 'humidity': 'Humidity: 47%', 'wind': 'Wind: N at 0 mph', 'f': '57'} | |
| 355 | |
| 356 def cmd_weather(ensoapi, place="san francisco"): | |
| 357 zipcode = cmd_weather.places[place] | |
| 358 url = "http://www.google.com/ig/api?weather=%s" % zipcode | |
| 359 thread = ThreadedFunc(make_get_url_func(url)) | |
| 360 while thread.isAlive(): | |
| 361 yield | |
| 362 weather_xml = thread.getRetval() | |
| 363 if not weather_xml: | |
| 364 ensoapi.display_message("An error occurred when getting the weather.") | |
| 365 else: | |
| 366 wdict = get_weather(weather_xml) | |
| 367 wdict["place"] = place | |
| 368 ensoapi.display_message(u"In %(place)s it is " | |
| 369 u"%(f)s\u00b0F/%(c)s\u00b0C, " | |
| 370 u"%(humidity)s, %(wind)s." % wdict) | |
| 371 | |
| 372 cmd_weather.places = { "san francisco" : "94115", | |
| 373 "chicago" : "60640", | |
| 374 "mountain view" : "94043" } | |
| 375 | |
| 376 cmd_weather.valid_args = cmd_weather.places.keys() | |
| 377 | |
| 378 def cmd_what_is_my_ip(ensoapi): | |
| 379 thread = ThreadedFunc(make_get_url_func("http://www.toolness.com/ip/")) | |
| 380 while thread.isAlive(): | |
| 381 yield | |
| 382 ensoapi.display_message("%s." % thread.getRetval()) | |
| 383 | |
| 384 def cmd_character_count(ensoapi): | |
| 385 text = ensoapi.get_selection().get("text", "").strip() | |
| 386 if not text: | |
| 387 ensoapi.display_message( "No selection." ) | |
| 388 ensoapi.display_message( "%d characters." % len(text) ) | |
| 389 | |
| 390 def cmd_email_to_me(ensoapi): | |
| 391 text = ensoapi.get_selection().get("text", "").strip() | |
| 392 if not text: | |
| 393 ensoapi.display_message( "No selection." ) | |
| 394 else: | |
| 395 server = "mail.toolness.com" | |
| 396 port = 587 | |
| 397 username = "no-reply@toolness.com" | |
| 398 password = "4p5o3j45ot%$" | |
| 399 | |
| 400 mailfrom = "varmaa@gmail.com" | |
| 401 rcpttos = ["varmaa@gmail.com"] | |
| 402 | |
| 403 text = text.encode("ascii", "xmlcharrefreplace") | |
| 404 subject = text.splitlines()[0] | |
| 405 text = ("This message was sent by Enso's " | |
| 406 "'email to' command.\n\n" + text) | |
| 407 data = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s" % \ | |
| 408 (mailfrom, ", ".join(rcpttos), subject, text) | |
| 409 | |
| 410 def sendmail(): | |
| 411 import smtplib | |
| 412 | |
| 413 try: | |
| 414 svr = smtplib.SMTP( server, port ) | |
| 415 svr.login( username, password ) | |
| 416 svr.sendmail( mailfrom, rcpttos, data ) | |
| 417 svr.quit() | |
| 418 except: | |
| 419 success[0] = False | |
| 420 raise | |
| 421 | |
| 422 thread = ThreadedFunc(sendmail) | |
| 423 ensoapi.display_message( "Sending message..." ) | |
| 424 while thread.isAlive(): | |
| 425 yield | |
| 426 if thread.wasSuccessful(): | |
| 427 ensoapi.display_message( "Message sent." ) | |
| 428 else: | |
| 429 ensoapi.display_message( | |
| 430 "An error occurred when sending the message." | |
| 431 ) | |
| 432 | |
| 433 def cmd_date(ensoapi): | |
| 434 import time | |
| 435 | |
| 436 ensoapi.display_message( "%s" % time.asctime() ) | |
| 437 | |
| 438 def cmd_insert_date(ensoapi): | |
| 439 import time | |
| 440 | |
| 441 ensoapi.set_selection( time.asctime() ) | |
| 442 | |
| 443 class WebSearchCmd(object): | |
| 444 def __init__(self, url_template): | |
| 445 self._url_template = url_template | |
| 446 | |
| 447 def __call__(self, ensoapi, query=None): | |
| 448 if not query: | |
| 449 query = ensoapi.get_selection().get("text", u"") | |
| 450 query = query.strip() | |
| 451 | |
| 452 if not query: | |
| 453 ensoapi.display_message( "No query." ) | |
| 454 return | |
| 455 | |
| 456 query = urllib.quote( query.encode("utf-8") ) | |
| 457 | |
| 458 webbrowser.open( self._url_template % {"query" : query} ) | |
| 459 | |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
460 cmd_wiki = WebSearchCmd("http://en.wikipedia.org/wiki/Special:Search?search=%(query)s") |
| 0 | 461 cmd_wowhead = WebSearchCmd("http://www.wowhead.com/?search=%(query)s") |
| 462 cmd_amaz = WebSearchCmd("http://www.amazon.com/exec/obidos/search-handle-url/index%%3Dblended%%26field-keywords%%3D%(query)s%%26store-name%%3Dall-product-search") | |
| 463 cmd_imdb = WebSearchCmd("http://www.imdb.com/find?s=all&q=%(query)s&x=0&y=0") | |
| 464 cmd_mdc = WebSearchCmd("http://www.google.com/search?hl=en&q=%(query)s+site%%3Adeveloper.mozilla.org&btnG=Google+Search") | |
| 465 cmd_mxr = WebSearchCmd("http://mxr.mozilla.org/mozilla/search?string=%(query)s") | |
| 12 | 466 cmd_phonebook = WebSearchCmd("https://ldap.mozilla.org/phonebook/#search/%(query)s") |
| 0 | 467 |
| 468 # For help on this one see http://www.squarefree.com/bugzilla/quicksearch-help.html | |
| 469 cmd_bugzilla = WebSearchCmd("https://bugzilla.mozilla.org/buglist.cgi?quicksearch=%(query)s") | |
| 470 | |
| 471 def cmd_farewell(ensoapi): | |
| 472 import AppKit | |
| 473 | |
| 474 app = AppKit.NSApplication.sharedApplication() | |
| 475 app.terminate_(None) | |
| 476 | |
| 477 class Launcher(object): | |
| 478 def __init__(self): | |
| 479 import Foundation | |
| 480 import AppKit | |
| 481 | |
| 482 self._targets = {} | |
| 483 | |
| 484 query = Foundation.NSMetadataQuery.alloc().init() | |
| 485 queryString = ( "((kMDItemKind == \"Application\") " | |
| 486 " and (kMDItemSupportFileType != \"MDSystemFile\"))" ) | |
| 487 queryString += " or (kMDItemKind == \"Mac OS X Preference Pane\")" | |
| 488 predicate = Foundation.NSPredicate.predicateWithFormat_( | |
| 489 queryString | |
| 490 ) | |
| 491 query.setPredicate_( predicate ) | |
| 492 if query.startQuery() != True: | |
| 493 raise Exception( "startQuery() failed." ) | |
| 494 self._query = query | |
| 495 self._workspace = AppKit.NSWorkspace.sharedWorkspace() | |
| 496 self._targets = {} | |
| 497 | |
| 498 def get_namespace(self): | |
| 499 return self._targets.keys() | |
| 500 | |
| 501 def get_target(self, name): | |
| 502 return self._targets[name] | |
| 503 | |
| 504 def update(self): | |
| 505 query = self._query | |
| 506 | |
| 507 while query.isGathering(): | |
| 508 yield | |
| 509 | |
| 510 # TODO: Modify this so we just get notified whenever the query | |
| 511 # results change instead of constantly "polling" every time | |
| 512 # the quasimode starts. | |
| 513 | |
| 514 resultList = [] | |
| 515 targets = {} | |
| 516 | |
| 517 query.disableUpdates() | |
| 518 numresults = query.resultCount() | |
| 519 | |
| 520 BATCH_SIZE = 10 | |
|
10
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
521 CUSTOM_TARGETS = { |
|
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
522 'computer': '/', |
|
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
523 'home': '~', |
|
11
eed902827dc7
added 'desktop' to open targets'
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
524 'desktop': '~/Desktop', |
|
10
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
525 'documents': '~/Documents', |
|
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
526 'downloads': '~/Downloads', |
|
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
527 'applications': '/Applications', |
|
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
528 } |
| 0 | 529 |
| 530 for i in range( numresults ): | |
| 531 result = query.resultAtIndex_( i ) | |
| 532 fsname = result.valueForAttribute_("kMDItemFSName") | |
| 533 name = result.valueForAttribute_("kMDItemDisplayName") | |
| 534 kind = result.valueForAttribute_("kMDItemKind") | |
| 535 if name: | |
| 536 name = name.lower() | |
| 537 itempath = result.valueForAttribute_("kMDItemPath") | |
| 538 if kind == "Mac OS X Preference Pane": | |
| 539 name += " preferences" | |
| 540 target = ShellOpenLaunchableTarget(itempath) | |
| 541 else: | |
| 542 target = AppLaunchableTarget(self._workspace, itempath) | |
| 543 resultList.append(name) | |
| 544 targets[name] = target | |
| 545 if i / BATCH_SIZE == i / float(BATCH_SIZE): | |
| 546 yield | |
| 547 #print "total results: %s" % numresults | |
| 548 query.enableUpdates() | |
|
10
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
549 for name in CUSTOM_TARGETS: |
|
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
550 path = os.path.expanduser(CUSTOM_TARGETS[name]) |
|
10e73bb57b01
Added more custom targets for 'open', like 'home', 'documents', etc.
Atul Varma <varmaa@toolness.com>
parents:
9
diff
changeset
|
551 targets[name] = ShellOpenLaunchableTarget(path) |
| 0 | 552 self._targets = targets |
| 553 | |
| 554 class AppLaunchableTarget(object): | |
| 555 def __init__(self, workspace, path): | |
| 556 self._workspace = workspace | |
| 557 self._path = path | |
| 558 | |
| 559 def launch(self, with_files=None): | |
| 560 if with_files: | |
| 561 for filename in with_files: | |
| 562 self._workspace.openFile_withApplication_( | |
| 563 filename, | |
| 564 self._path | |
| 565 ) | |
| 566 else: | |
| 567 self._workspace.launchApplication_( self._path ) | |
| 568 | |
| 569 def can_launch_with(self): | |
| 570 return True | |
| 571 | |
| 572 class ShellOpenLaunchableTarget(object): | |
| 573 def __init__(self, path): | |
| 574 self._path = path | |
| 575 | |
| 576 def launch(self): | |
| 577 subprocess.Popen( ["open", self._path] ) | |
| 578 | |
| 579 def can_launch_with(self): | |
| 580 return False | |
| 581 | |
| 582 class OpenCommand(object): | |
| 583 """ | |
| 584 Opens an application, folder, or URL. | |
| 585 """ | |
| 586 | |
| 587 def __init__(self, launcher): | |
| 588 self.launcher = launcher | |
| 589 self._isFetchingArgs = False | |
| 590 | |
| 591 def on_quasimode_start(self): | |
| 592 if self._isFetchingArgs: | |
| 593 return | |
| 594 | |
| 595 self._isFetchingArgs = True | |
| 596 | |
| 597 for _ in self.launcher.update(): | |
| 598 yield | |
| 599 | |
| 600 self.valid_args = self.launcher.get_namespace() | |
| 601 | |
| 602 self._isFetchingArgs = False | |
| 603 | |
| 604 valid_args = [] | |
| 605 | |
| 606 def __call__(self, ensoapi, target=None): | |
| 607 if not target: | |
| 608 seldict = ensoapi.get_selection() | |
| 609 if seldict.get("files"): | |
| 610 for file in seldict["files"]: | |
| 611 subprocess.Popen( ["open", file] ) | |
| 612 elif seldict.get("text"): | |
| 613 filename = seldict["text"].strip() | |
| 614 if os.path.isabs(filename): | |
| 615 subprocess.Popen( ["open", filename] ) | |
| 616 else: | |
| 617 webbrowser.open(filename) | |
| 618 else: | |
| 619 self.launcher.get_target(target).launch() | |
| 620 | |
| 621 class OpenWithCommand(object): | |
| 622 """ | |
| 623 Opens the selected file(s) with a particular application. | |
| 624 """ | |
| 625 | |
| 626 def __init__(self, launcher): | |
| 627 self.launcher = launcher | |
| 628 | |
| 629 def _get_valid_args(self): | |
| 630 return self.launcher.get_namespace() | |
| 631 | |
| 632 valid_args = property(_get_valid_args) | |
| 633 | |
| 634 def __call__(self, ensoapi, target): | |
| 635 files = ensoapi.get_selection().get("files", []) | |
| 636 targ = self.launcher.get_target(target) | |
| 637 if not files: | |
| 638 ensoapi.display_message("No files selected!") | |
| 639 elif not targ.can_launch_with(): | |
| 640 ensoapi.display_message("Can't open files with %s." % target) | |
| 641 else: | |
| 642 targ.launch(files) | |
| 643 | |
| 644 cmd_go = OpenCommand(Launcher()) | |
| 645 cmd_open = cmd_go | |
| 646 cmd_bust_with = OpenWithCommand(cmd_open.launcher) | |
| 647 | |
| 648 def cmd_run_python(ensoapi): | |
| 649 """ | |
| 650 runs the selected text as python code. | |
| 651 """ | |
| 652 | |
| 653 text = ensoapi.get_selection().get("text","") | |
| 654 | |
| 655 if not text: | |
| 656 ensoapi.display_message("No text selection!") | |
| 657 return | |
| 658 | |
| 659 output_fd, output_name = tempfile.mkstemp() | |
| 660 | |
| 661 # TODO: This is for OSX only; will have to figure out what to do | |
| 662 # for other platforms. | |
| 663 cmd = text.replace( "\r", "\n" ) | |
| 664 | |
| 665 try: | |
| 666 compile( cmd, "<selected text>", "eval" ) | |
| 667 is_eval = True | |
| 668 except: | |
| 669 is_eval = False | |
| 670 | |
| 671 if is_eval: | |
| 672 cmd = "print %s," % cmd | |
| 673 | |
| 674 cmd = ("try:\n" | |
| 675 " from autoimp import *\n" | |
| 676 "except ImportError:\n" | |
| 677 " pass\n\n" + cmd ) | |
| 678 | |
| 679 popen = subprocess.Popen( | |
| 680 [ sys.executable, | |
| 681 "-c", cmd ], | |
| 682 stdout = output_fd, | |
| 683 stderr = subprocess.STDOUT, | |
| 684 ) | |
| 685 | |
| 686 while popen.poll() is None: | |
| 687 yield | |
| 688 | |
| 689 os.close(output_fd) | |
| 690 | |
| 691 output_text = open(output_name, "r").read().strip() | |
| 692 if output_text: | |
| 693 seldict = {"text" : "\n".join([text, output_text])} | |
| 694 ensoapi.set_selection(seldict) | |
| 695 else: | |
| 696 if popen.returncode == 0: | |
| 697 ensoapi.display_message("Code run successfully.") | |
| 698 else: | |
| 699 ensoapi.display_message("An error occurred.") | |
| 700 | |
| 701 os.remove(output_name) | |
| 702 | |
| 703 def cmd_doctest(ensoapi): | |
| 704 """ | |
| 705 python doctests the selected section of plain text. | |
| 706 """ | |
| 707 | |
| 708 text = ensoapi.get_selection().get("text","") | |
| 709 | |
| 710 if not text: | |
| 711 ensoapi.display_message("No text selection!") | |
| 712 return | |
| 713 | |
| 714 # TODO: This is for OSX only; will have to figure out what to do | |
| 715 # for other platforms. | |
| 716 new_text = text.replace( "\r", "\n" ) | |
| 717 | |
| 718 fd, source_name = tempfile.mkstemp() | |
| 719 os.close(fd) | |
| 720 open(source_name, "wb").write(new_text) | |
| 721 | |
| 722 output_fd, output_name = tempfile.mkstemp() | |
| 723 | |
| 724 cmd = ("import doctest; doctest.testfile(%s, " | |
| 725 "module_relative=False)" % repr(source_name)) | |
| 726 | |
| 727 popen = subprocess.Popen( | |
| 728 [ sys.executable, | |
| 729 "-c", cmd ], | |
| 730 stdout = output_fd, | |
| 731 stderr = subprocess.STDOUT, | |
| 732 ) | |
| 733 | |
| 734 while popen.poll() is None: | |
| 735 yield | |
| 736 | |
| 737 os.close(output_fd) | |
| 738 | |
| 739 if popen.returncode == 0: | |
| 740 output_text = open(output_name, "r").read() | |
| 741 if output_text: | |
| 742 seldict = {"text":text + output_text} | |
| 743 ensoapi.set_selection(seldict) | |
| 744 else: | |
| 745 ensoapi.display_message("All doctests successful!") | |
| 746 else: | |
| 747 ensoapi.display_message("An error occurred.") | |
| 748 | |
| 749 os.remove(source_name) | |
| 750 os.remove(output_name) | |
| 751 | |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
752 def cmd_wow_firefox(ensoapi): |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
753 popen = subprocess.Popen(["/Users/varmaa/bin/wow-firefox"]) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
754 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
755 def cmd_work_firefox(ensoapi): |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
756 popen = subprocess.Popen(["/Users/varmaa/bin/work-firefox"]) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
757 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
758 def cmd_mozpics_firefox(ensoapi): |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
759 popen = subprocess.Popen(["/Users/varmaa/bin/mozpics-firefox"]) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
760 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
761 def cmd_firefox(ensoapi): |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
762 popen = subprocess.Popen(["/Users/varmaa/bin/personal-firefox"]) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
763 |
| 0 | 764 def cmd_nosetest(ensoapi): |
| 765 """ | |
| 766 runs nosetests on the selected region of python code. | |
| 767 """ | |
| 768 | |
| 769 text = ensoapi.get_selection().get("text","") | |
| 770 | |
| 771 if not text: | |
| 772 ensoapi.display_message("No text selection!") | |
| 773 return | |
| 774 | |
| 775 # TODO: This is for OSX only; will have to figure out what to do | |
| 776 # for other platforms. | |
| 777 new_text = text.replace( "\r", "\n" ) | |
| 778 | |
| 779 fd, source_name = tempfile.mkstemp(suffix=".py") | |
| 780 os.close(fd) | |
| 781 open(source_name, "wb").write(new_text) | |
| 782 | |
| 783 output_fd, output_name = tempfile.mkstemp() | |
| 784 | |
| 785 popen = subprocess.Popen( | |
|
5
56d44190362e
Had to provide an absolute path to nosetests.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
786 [ "/usr/local/bin/nosetests", |
| 0 | 787 source_name, |
| 788 "--with-doctest" ], | |
| 789 stdout = output_fd, | |
| 790 stderr = subprocess.STDOUT, | |
| 791 ) | |
| 792 | |
| 793 while popen.poll() is None: | |
| 794 yield | |
| 795 | |
| 796 os.close(output_fd) | |
| 797 | |
| 798 output_text = open(output_name, "r").read() | |
| 799 if output_text: | |
| 800 if popen.returncode == 0: | |
| 801 lines = output_text.splitlines() | |
| 802 ensoapi.display_message(lines[2]) | |
| 803 else: | |
| 804 seldict = {"text":text + output_text} | |
| 805 ensoapi.set_selection(seldict) | |
| 806 else: | |
| 807 raise AssertionError( "nosetests didn't return anything!" ) | |
| 808 | |
| 809 os.remove(source_name) | |
| 810 os.remove(output_name) | |
| 811 | |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
812 def cmd_markdown_to_html(ensoapi): |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
813 """ |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
814 Converts the plaintext markdown selection to plaintext |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
815 HTML source. |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
816 """ |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
817 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
818 import markdown2 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
819 text = ensoapi.get_selection().get("text", "") |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
820 html = markdown2.markdown(text) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
821 ensoapi.set_selection({"text": html}) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
822 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
823 def cmd_unquote(ensoapi): |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
824 """ |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
825 Removes a '<' at the beginning of every line. |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
826 """ |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
827 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
828 text = ensoapi.get_selection().get("text", "") |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
829 lines = ["%s\n" % line[2:] |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
830 for line in text.splitlines() |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
831 if line.startswith("> ")] |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
832 ensoapi.set_selection({"text": ''.join(lines)}) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
833 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
834 def cmd_quote(ensoapi): |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
835 """ |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
836 Puts a '>' at the beginning of every line. |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
837 """ |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
838 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
839 text = ensoapi.get_selection().get("text", "") |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
840 lines = ["> %s\n" % line |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
841 for line in text.splitlines()] |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
842 ensoapi.set_selection({"text": ''.join(lines)}) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
843 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
844 def cmd_markdown(ensoapi): |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
845 """ |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
846 Converts the plaintext markdown selection to rich text. |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
847 """ |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
848 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
849 import markdown2 |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
850 text = ensoapi.get_selection().get("text", "") |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
851 html = markdown2.markdown(text) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
852 ensoapi.set_selection({"html": html, |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
853 "text": text}) |
|
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
854 |
| 0 | 855 def cmd_insert_html(ensoapi): |
| 856 ensoapi.set_selection( {"html":"<b>hi</b> <p>there</p>"} ) | |
| 857 | |
| 858 def _runAppleScript( script ): | |
| 859 params = [ "osascript", "-e", script ] | |
| 860 popen = subprocess.Popen( params ) | |
|
8
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
861 if popen.wait(): |
|
4285e37bea69
added workaround for getting photoshop merged selection, applescript failures now raise exceptiosn
Atul Varma <varmaa@toolness.com>
parents:
7
diff
changeset
|
862 raise Exception("Applescript failed: %s" % script) |
| 0 | 863 |
| 864 def cmd_screen_saver(ensoapi): | |
| 865 """ | |
| 866 Starts your screen saver. | |
| 867 """ | |
| 868 | |
| 869 _runAppleScript( | |
| 870 "tell application id \"com.apple.ScreenSaver.Engine\" " | |
| 871 "to launch" | |
| 872 ) | |
| 873 | |
| 874 def cmd_sleep(ensoapi): | |
| 875 """ | |
| 876 Puts your computer to sleep. | |
| 877 """ | |
| 878 | |
| 879 _runAppleScript( | |
| 880 "tell application \"Finder\" to sleep" | |
| 881 ) | |
| 882 | |
| 883 def cmd_play(ensoapi): | |
| 884 """ | |
| 885 Plays the current iTunes song. | |
| 886 """ | |
| 887 | |
| 888 _runAppleScript( | |
| 889 "tell application \"iTunes\" to play" | |
| 890 ) | |
| 891 | |
| 892 def cmd_pause(ensoapi): | |
| 893 """ | |
| 894 Pauses the current iTunes song. | |
| 895 """ | |
| 896 | |
| 897 _runAppleScript( | |
| 898 "tell application \"iTunes\" to pause" | |
| 899 ) | |
| 900 | |
| 901 | |
| 902 def cmd_next_track(ensoapi): | |
| 903 """ | |
| 904 Goes to the next track on iTunes. | |
| 905 """ | |
| 906 | |
| 907 _runAppleScript( | |
| 908 "tell application \"iTunes\" to next track" | |
| 909 ) | |
| 910 | |
| 911 def cmd_previous_track(ensoapi): | |
| 912 """ | |
| 913 Goes to the previous track on iTunes. | |
| 914 """ | |
| 915 | |
| 916 _runAppleScript( | |
| 917 "tell application \"iTunes\" to back track" | |
| 918 ) | |
| 919 | |
|
1
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
920 def cmd_html_template(ensoapi): |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
921 ensoapi.set_selection({"text" : HTML_TEMPLATE}) |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
922 |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
923 HTML_TEMPLATE = """\ |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
924 <html> |
|
1
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
925 <head> |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
926 <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
927 <link rel="stylesheet" type="text/css" media="all" |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
928 href="" /> |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
929 <title></title> |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
930 </head> |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
931 <body> |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
932 |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
933 </body> |
|
7
41bc59775872
wow, haven't committed changes in a long time.
Atul Varma <varmaa@toolness.com>
parents:
6
diff
changeset
|
934 <script src=""></script> |
|
1
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
935 </html> |
|
3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
Atul Varma <varmaa@toolness.com>
parents:
0
diff
changeset
|
936 """ |
|
17
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
937 |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
938 def _gen_daily_edition(ensoapi, args=""): |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
939 logfile = open('/tmp/daily-edition.log', 'w') |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
940 popen = subprocess.Popen([ |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
941 "ssh", "labs.toolness.com", |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
942 "/home/varmaa/bin/publish-edition %s" % args |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
943 ], stdout=logfile, stderr=subprocess.STDOUT) |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
944 ensoapi.display_message("Please wait, this may take a while.") |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
945 while popen.poll() is None: |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
946 yield |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
947 logfile.close() |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
948 if popen.returncode: |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
949 ensoapi.display_message("Failed to generate edition!") |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
950 else: |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
951 retval = subprocess.call([ |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
952 "open", |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
953 "-a", "/Applications/Safari.app", |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
954 "http://labs.toolness.com/daily-edition/" |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
955 ]) |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
956 if retval: |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
957 ensoapi.display_message("Opening web browser failed.") |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
958 |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
959 def cmd_publish_new_daily_edition(ensoapi): |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
960 "Generates a new issue of The Daily Edition with new news." |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
961 |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
962 return _gen_daily_edition(ensoapi, "--refresh-feeds") |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
963 |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
964 def cmd_publish_old_daily_edition(ensoapi): |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
965 "Generates a new issue of The Daily Edition with old news." |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
966 |
|
557f0edc2ca4
added daily-edition commands
Atul Varma <varmaa@toolness.com>
parents:
16
diff
changeset
|
967 return _gen_daily_edition(ensoapi) |
|
18
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
968 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
969 class SubprocessRunner(object): |
|
19
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
970 def __init__(self, cmdline, env=None, raise_on_error=True): |
|
18
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
971 self.cmdline = cmdline |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
972 self.output_text = None |
|
19
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
973 self.env = env |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
974 self.raise_on_error = raise_on_error |
|
18
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
975 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
976 def run(self): |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
977 output_fd, output_name = tempfile.mkstemp() |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
978 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
979 popen = subprocess.Popen(self.cmdline, |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
980 stdout = output_fd, |
|
19
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
981 stderr = subprocess.STDOUT, |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
982 env = self.env) |
|
18
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
983 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
984 while popen.poll() is None: |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
985 yield |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
986 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
987 os.close(output_fd) |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
988 output_text = open(output_name, "r").read().strip() |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
989 os.remove(output_name) |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
990 |
|
19
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
991 self.returncode = popen.returncode |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
992 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
993 if popen.returncode and self.raise_on_error: |
|
18
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
994 raise Exception("Process failed with output %s" % |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
995 repr(output_text)) |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
996 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
997 self.output_text = output_text |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
998 |
|
19
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
999 def infer_patch_from_url(url): |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1000 """ |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1001 >>> infer_patch_from_url("blah") |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1002 (None, None) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1003 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1004 >>> url = "https://bugzilla.mozilla.org/show_bug.cgi?id=556075" |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1005 >>> infer_patch_from_url(url) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1006 ('556075', None) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1007 """ |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1008 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1009 import re |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1010 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1011 BUG_RE = r"https:\/\/bugzilla.mozilla.org\/show_bug.cgi\?id=([0-9]+)" |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1012 bug_re = re.compile(BUG_RE) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1013 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1014 match = bug_re.match(url) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1015 if match: |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1016 return (match.group(1), None) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1017 return (None, None) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1018 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1019 def cmd_try_jetpack_bugzilla_patch(ensoapi): |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1020 """Fetches the patch for the currently selected bug or attachment URL |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1021 and creates a clone of the Jetpack repository that has it applied.""" |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1022 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1023 url = ensoapi.get_selection().get("text", "").strip() |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1024 if not url: |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1025 ensoapi.display_message("Please select a bug or attachment URL.") |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1026 return |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1027 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1028 bug_id, attach_id = infer_patch_from_url(url) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1029 if not bug_id: |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1030 ensoapi.display_message("That does not appear to be a bug or " |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1031 "attachment URL.") |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1032 return |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1033 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1034 mypkg = os.path.expanduser("~/Documents/MyPythonPackage") |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1035 hgzilla = mypkg + "/scripts/hgzilla.py" |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1036 root_repo_path = os.path.expanduser("~/Documents/jetpack-sdk") |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1037 clone_name = "jetpack-sdk-bug-%s" % bug_id |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1038 clone_path = os.path.expanduser("~/Documents/" + clone_name) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1039 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1040 if os.path.exists(clone_path): |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1041 runner = SubprocessRunner(["/bin/hrm", clone_path]) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1042 for _ in runner.run(): |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1043 yield |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1044 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1045 env = {} |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1046 env.update(os.environ) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1047 env['PYTHONPATH'] = mypkg |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1048 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1049 cmdline = [sys.executable, hgzilla, |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1050 "fork", |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1051 "--hg-path", "/usr/local/bin/hg", |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1052 "--bug", bug_id, |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1053 "--clone-dir", clone_path, |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1054 "--root-repo-dir", root_repo_path] |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1055 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1056 if attach_id: |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1057 cmdline.extend(["--attachment", attach_id]) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1058 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1059 runner = SubprocessRunner(cmdline, env=env, |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1060 raise_on_error=False) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1061 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1062 ensoapi.display_message("Please wait.") |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1063 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1064 for _ in runner.run(): |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1065 yield |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1066 |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1067 if runner.returncode: |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1068 ensoapi.display_message(runner.output_text) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1069 else: |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1070 _runAppleScript('tell application "Terminal" to activate') |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1071 _runAppleScript('tell application "Terminal" ' |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1072 'to do script "cd %s;source bin/activate"' % |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1073 clone_path) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1074 ensoapi.display_message("Repository created at %s." % clone_path) |
|
854065d98c3d
Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
1075 |
|
18
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1076 def cmd_latest_jetpack_rev(ensoapi): |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1077 "Dumps info about the latest Jetpack revision." |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1078 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1079 path = "~/Documents/MyPythonPackage/scripts/get_latest_feed_entry.py" |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1080 url = "http://hg.mozilla.org/labs/jetpack-sdk/atom-log" |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1081 cmdline = [sys.executable, os.path.expanduser(path), url] |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1082 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1083 runner = SubprocessRunner(cmdline) |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1084 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1085 ensoapi.display_message("Please wait.") |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1086 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1087 for _ in runner.run(): |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1088 yield |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1089 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1090 info = json.loads(runner.output_text) |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1091 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1092 text = "\n".join([" %s" % info['title'], |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1093 " By %s" % info['author'], |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1094 " %s" % info['link']]) |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1095 html = '<a href="%s">%s</a>' % (info['link'], |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1096 html_escape(info['title'])) |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1097 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1098 ensoapi.set_selection({"text": text, "html": html}) |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1099 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1100 # Taken from http://wiki.python.org/moin/EscapingHtml |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1101 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1102 html_escape_table = { |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1103 "&": "&", |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1104 '"': """, |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1105 "'": "'", |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1106 ">": ">", |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1107 "<": "<", |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1108 } |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1109 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1110 def html_escape(text): |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1111 """Produce entities within text.""" |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1112 |
|
400a9edd79ad
Added a latest_jetpack_rev command
Atul Varma <varmaa@toolness.com>
parents:
17
diff
changeset
|
1113 return "".join(html_escape_table.get(c,c) for c in text) |
