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