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