diff options
author | David Runge <david.runge@frqrec.com> | 2013-11-03 12:30:45 +0100 |
---|---|---|
committer | David Runge <david.runge@frqrec.com> | 2013-11-03 12:30:45 +0100 |
commit | 67b0ec5741d327d27afe88783d85fcf2c4c4f204 (patch) | |
tree | 4ea47807c5e775927b96eb79933c09fbfcd0468e /bin/stikked | |
download | dotfiles-67b0ec5741d327d27afe88783d85fcf2c4c4f204.tar.gz dotfiles-67b0ec5741d327d27afe88783d85fcf2c4c4f204.tar.bz2 dotfiles-67b0ec5741d327d27afe88783d85fcf2c4c4f204.tar.xz dotfiles-67b0ec5741d327d27afe88783d85fcf2c4c4f204.zip |
First commit
Diffstat (limited to 'bin/stikked')
-rw-r--r-- | bin/stikked | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/bin/stikked b/bin/stikked new file mode 100644 index 0000000..b209208 --- /dev/null +++ b/bin/stikked @@ -0,0 +1,115 @@ +#! /usr/bin/env python2 + +import argparse +import os +import sys +import pycurl +import StringIO +import subprocess +import urllib + +## CONFIGURATION { +user='example-user' +apiurl='http://paste.giev.de/api/create' +## } + + +def main(): + global verbose + + # parse arguments + parser = argparse.ArgumentParser(prog='stikked') + parser.add_argument('-V', '--version', action='version', version='%(prog)s 0.1') + parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', help='verbose mode', default=False) + parser.add_argument('-s', dest='syntax', action='store', help='syntax to highlight', default='text') + parser.add_argument('-p', dest='private', action='store_true', help='set paste to private', default=False) + parser.add_argument('-t', dest='title', action='store', help='set title of paste') + parser.add_argument('fname', metavar='FILE|-', help='file to paste or - for stdin') + args = parser.parse_args() + if args.verbose: + verbose = args.verbose + print args + + # private + private = '1' if args.private else '0' + + # from stdin + if args.fname == '-': + title = args.title if args.title else 'stdin' + text=sys.stdin.read() + syntax=args.syntax + + # from file + else: + fname = args.fname + + if not os.path.isfile(fname): + print "No such file: {0}".format(fname) + sys.exit(1) + else: + title = args.title if args.title else os.path.split(fname)[1] + + if args.syntax == 'text': + ext = os.path.splitext(fname)[1][1:] + syntax = _detect_syntax(ext) + else: + syntax = args.syntax + + fopen = file(fname, 'r') + text = fopen.read() + + _paste(text, title, syntax, private) + + +# send paste to server +def _paste(text, title, syntax, private): + postparams = [ + ('text', text), + ('name', user), + ('title', title), + ('lang', syntax), + ('private', private)] + + postfields=urllib.urlencode(postparams) + + curl = pycurl.Curl() + curl.setopt(pycurl.URL, apiurl) + curl.setopt(pycurl.POST, True) + curl.setopt(pycurl.POSTFIELDS, postfields) + # Fixes the HTTP/1.1 417 Expectation Failed Bug + header = [] + header.append("Expect: ") + curl.setopt(pycurl.HTTPHEADER, header) + curl.setopt(pycurl.NOPROGRESS, True) + b = StringIO.StringIO() + curl.setopt(pycurl.WRITEFUNCTION, b.write) + curl.perform() + print b.getvalue(); + + # copy to primary clipboard + xsel_proc = xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE) + xsel_proc.communicate(b.getvalue()) + + +# more possible of course +def _detect_syntax(ext): + if ext == 'h': + syntax='c' + elif ext == 'log': + syntax='logcat' + elif ext == "py": + syntax = 'python' + elif ext == "pl": + syntax = 'perl' + elif ext == "patch": + syntax = 'diff' + elif ext == 'c' or ext == 'cpp' or ext == 'java' or ext == 'sh': + syntax=ext + else: + syntax='text' + + return syntax + + +if __name__ == '__main__': + main() |