-
- #!/usr/bin/python
- """
- The purpose of this program is such that if you bind a keystroke (say, Windows+E)
- to, say:
-
- python /home/joe/quickeval.py
-
- and virtkey types the python output of whatever code is in the clipboard.
-
- Dangerous? Maybe a little, if you're writing an article about python and rm -rf. Useful? Kind of a lot. Be sure to take your hands off the keyboard after you execute this via keybinding so you don't combine what's being typed with your keybinding's keys.
-
- """
-
- from cStringIO import StringIO
-
- import gtk, os, sys, time, re, operator, itertools, datetime, django, math, random # convenience
- import pygtk
- import virtkey
- pygtk.require('2.0')
- # useful functions
- def njoin(a, n=1): return ('\n'*n).join(a)
- def nsplit(s, include_blanks=True):
- return [a for a in s.split('\n') if (include_blanks or a)]
- def ejoin(a): return ''.join(a)
- def sjoin(a): return ' '.join(a)
- def cjoin(a): return ', '.join(a)
- def velcro(a, b): return dict(zip(a,b))
- def flatten(a): return list(itertools.chain(*a))
- def stripall(a): return [b.strip() for b in a]
-
-
- def getclip():
- try:
- s = gtk.clipboard_get().wait_for_text()
- return s
- except:
- print 'Failed!'
- def main():
-
- old_stdout = sys.stdout
- sys.stdout = mystdout = StringIO()
-
- # useful variables
- alphabet = map(chr,range(65,65+26))
- newline = '\n' # probably the best way to fix the issue of needing newlines in-code
- nl = newline # quicker
-
- # execute the clipboard
- cmds = getclip()
- exec(cmds)
- result = mystdout.getvalue()
- if not result:
- x = ''
- exec('x = ' + cmds)
- result = str(x)
- mystdout.close()
-
- # type the result
- time.sleep(0.4)
- v = virtkey.virtkey()
- for i in [ord(c) for c in list(str(result.rstrip()))]:
- v.press_unicode(i)
- v.release_unicode(i)
-
- sys.stdout = old_stdout
- return 0
-
- if __name__ == '__main__': main()
-