1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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()
|