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
|
import urllib
DEFAULT_NAME=None
class Pastie:
def __init__(self, body='', parser=None, private=True, name = None):
self.body = body
self.parser = parser
self.private = private
self.name = name
def submit(self):
form = {
"paste[parser]": self.parser or 'plain_text',
"paste[body]": self.body,
"paste[authorization]": 'burger',
"paste[restricted]": self.private and '1' or '0'
}
if self.name is not None:
form['paste[display_name]'] = self.name
f = urllib.urlopen("http://pastie.org/pastes", urllib.urlencode(form))
f.close()
return f.geturl()
if __name__ == '__main__':
import sys, os
from optparse import OptionParser
extmap = {
'as': 'actionscript',
'c': 'c++',
'cpp': 'c++',
'c++': 'c++',
'cxx': 'c++',
'cc': 'c++',
'h': 'c++',
'hpp': 'c++',
'h++': 'c++',
'hxx': 'c++',
'hc': 'c++',
'css': 'css',
'diff': 'diff',
'patch': 'diff',
'erb': 'html_rails',
'rhtml': 'html_rails',
'html': 'html',
'htm': 'html',
'java': 'java',
'js': 'javascript',
'm': 'objective-c++',
'pas': 'pascal',
'pl': 'perl',
'php': 'php',
'phtml': 'php',
'php3': 'php',
'php4': 'php',
'php5': 'php',
'py': 'python',
'rb': 'ruby',
'sh': 'bash',
'sql': 'sql',
'yml': 'yaml'
}
pastie = Pastie(body='', name=DEFAULT_NAME)
parser = OptionParser()
parser.add_option("-p", "--public", dest='private', action='store_false', help="make pastie public")
parser.add_option("--private", dest='private', action='store_true', help="make pastie private (default)")
parser.add_option("-n", "--name", dest='name', action='store', type='string', help="author of the pastie", metavar="NAME")
parser.add_option("-t", "--type", dest='parser', action='store', type='string', help="file type (parser)", metavar="PARSER")
args = parser.parse_args(sys.argv[1:], pastie)[1]
if pastie.parser and '.' in pastie.parser:
try:
pastie.parser = extmap[pastie.parser.split('.')[-1].lower()]
except:
pastie.parser = None
if len(args) == 0:
pastie.body = sys.stdin.read()
else:
for name in args:
if name == '-':
body = sys.stdin.read()
name = 'stdin'
else:
body = open(name, 'r').read()
name = os.path.basename(name)
try:
parser = extmap[name.split('.')[-1].lower()]
except:
parser = 'plain_text'
pastie.body += '## %s [%s]\n'%(name, pastie.parser or parser)
pastie.body += body
if pastie.body[-1] != '\n':
pastie.body += '\n'
print pastie.submit()
|