spacepaste

  1.  
  2. #!/usr/bin/python
  3. """
  4. The purpose of this program is such that if you bind a keystroke (say, Windows+E)
  5. to, say:
  6. python /home/joe/quickeval.py
  7. and virtkey types the python output of whatever code is in the clipboard.
  8. 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.
  9. """
  10. from cStringIO import StringIO
  11. import gtk, os, sys, time, re, operator, itertools, datetime, django, math, random # convenience
  12. import pygtk
  13. import virtkey
  14. pygtk.require('2.0')
  15. # useful functions
  16. def njoin(a, n=1): return ('\n'*n).join(a)
  17. def nsplit(s, include_blanks=True):
  18. return [a for a in s.split('\n') if (include_blanks or a)]
  19. def ejoin(a): return ''.join(a)
  20. def sjoin(a): return ' '.join(a)
  21. def cjoin(a): return ', '.join(a)
  22. def velcro(a, b): return dict(zip(a,b))
  23. def flatten(a): return list(itertools.chain(*a))
  24. def stripall(a): return [b.strip() for b in a]
  25. def getclip():
  26. try:
  27. s = gtk.clipboard_get().wait_for_text()
  28. return s
  29. except:
  30. print 'Failed!'
  31. def main():
  32. old_stdout = sys.stdout
  33. sys.stdout = mystdout = StringIO()
  34. # useful variables
  35. alphabet = map(chr,range(65,65+26))
  36. newline = '\n' # probably the best way to fix the issue of needing newlines in-code
  37. nl = newline # quicker
  38. # execute the clipboard
  39. cmds = getclip()
  40. exec(cmds)
  41. result = mystdout.getvalue()
  42. if not result:
  43. x = ''
  44. exec('x = ' + cmds)
  45. result = str(x)
  46. mystdout.close()
  47. # type the result
  48. time.sleep(0.4)
  49. v = virtkey.virtkey()
  50. for i in [ord(c) for c in list(str(result.rstrip()))]:
  51. v.press_unicode(i)
  52. v.release_unicode(i)
  53. sys.stdout = old_stdout
  54. return 0
  55. if __name__ == '__main__': main()
  56.