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