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