Skip to content

Instantly share code, notes, and snippets.

@starenka
Created January 16, 2011 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starenka/781919 to your computer and use it in GitHub Desktop.
Save starenka/781919 to your computer and use it in GitHub Desktop.
openpaste.org wrapper
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# openpaste.org wrapper
#
# reads from stdin and copies uri to klipper by default
#
# @author: starenka
# @email: admin[at]starenka[dot]net
# @version: 1.1
# @since Oct 9, 2009
import os,sys,xmlrpclib,commands
from optparse import OptionParser
URI = 'http://openpaste.org/api/xml-rpc/'
DEFAULT_OUT_CB = 'klipper_kde4' # klipper | xclip | ??
usage = '%s < /somewhere/file.py -s python -a Guido'%(sys.argv[0])
oparser = OptionParser(usage)
oparser.add_option('-f','--file',action='store',dest='file',default = False,\
help='read data from file (you can also use stdin: openpyster.py < /tmp/file)')
oparser.add_option('-i','--from-klipper',action='store_true',dest='klipper',default = False,\
help='read data form klipper (KDE)')
oparser.add_option('-k','--from-klipper-kde4',action='store_true',dest='klipper_kde4',default = False,\
help='read data form klipper (KDE4)')
oparser.add_option('-x','--from-xclip',action='store_true',dest='xclip',default = False,\
help='read from X clipboard')
oparser.add_option('-s','--syntax',action='store',dest='syntax',default = False,\
help='code syntax highlight')
oparser.add_option('-l','--list-syntax',action='store_true',dest='list',default = False,\
help='list available syntax highlighters')
oparser.add_option('-a','--author',action='store',dest='author',default = False,\
help='paste author')
oparser.add_option('-p','--private',action='store_true',dest='private',default = False,\
help='private paste?')
oparser.add_option('-w','--password',action='store',dest='passwd',default = False,\
help='paste password')
oparser.add_option('-d','--description',action='store',dest='desc',default = False,\
help='paste description')
oparser.add_option('-y','--secret-key',action='store',dest='key',default = False,\
help='paste secret key')
(options,args) = oparser.parse_args()
acronyms = {}
paste = {'code': None,
'syntax': 'text',
'author': '',
'desc': '',
'private': False,
'password': '',
'secret_key': '' }
try: rpc = xmlrpclib.ServerProxy(URI)
except Exception,e : print(e)
for a in rpc.Openpaste.getSupportedSyntax(): acronyms[a['acronym']] = a['name']
if options.list:
print '(i) Available syntax highlighting...'
for a,n in acronyms.items(): print '%s %s'%(n.ljust(25),a)
sys.exit()
if options.file:
print '(i) Reading from file...'
try:
f = open(options.file)
paste['code'] = f.read()
f.close()
except Exception,e: sys.exit('(!) %s'%e)
if options.klipper:
print '(i) Getting klipper contents...'
try: paste['code'] = commands.getoutput('dcop klipper klipper getClipboardContents')
except Exception,e: sys.exit('(!) %s'%e)
if options.klipper_kde4:
print '(i) Getting klipper-kde4 contents...'
try: paste['code'] = commands.getoutput('dbus-send --print-reply --dest=org.kde.klipper /klipper org.kde.klipper.klipper.getClipboardContents')
except Exception,e: sys.exit('(!) %s'%e)
if options.xclip:
print '(i) Getting X clipboard contents...'
try: paste['code'] = commands.getoutput('xclip -o')
except Exception,e: sys.exit('(!) %s'%e)
if paste['code'] == None:
print '(i) Reading from stdin... (if not passinga file, hit ctrl+d for EOF)'
paste['code'] = sys.stdin.read()
if options.syntax and options.syntax in acronyms:
paste['syntax'] = options.syntax
else: print '(w) Missing or invalid highlight acronym. Assuming "raw". Use openpyster -l to list.'
if options.desc: paste['desc'] = options.desc
if options.author: paste['author'] = options.author
if options.passwd: paste['password'] = options.passwd
if options.key: paste['secret_key'] = options.key
if options.private: paste['private'] = True
print '(i) Pasting item...'
try: uri = rpc.Openpaste.addPost(paste['code'], paste['syntax'], paste['author'], paste['desc'], \
bool(paste['private']), paste['password'], paste['secret_key'])
except Exception,e: sys.exit('(!) %s'%e)
print '(i) Copying uri to %s...'%DEFAULT_OUT_CB
for DEFAULT_OUT_CB in [DEFAULT_OUT_CB]:
if DEFAULT_OUT_CB == 'klipper':
try: os.system('dcop klipper klipper setClipboardContents %s'%uri['URL'])
except Exception,e: print '(!) %s'%e
break
if DEFAULT_OUT_CB == 'xclip':
try: os.system('echo "%s" | xclip -i < /dev/stdin'%uri['URL'])
except Exception,e: print '(!) %s'%e
break
if DEFAULT_OUT_CB == 'klipper_kde4':
try: os.system('dbus-send --type=method_call --dest=org.kde.klipper /klipper org.kde.klipper.klipper.setClipboardContents string:"%s"'%uri['URL'])
except Exception,e: print '(!) %s'%e
break
print "\n(x) Here's your uri, son. %s"%uri['URL']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment