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