forked from rupa/sprunge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprunge.py
148 lines (123 loc) · 4.73 KB
/
sprunge.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import cgi
import os
import random
import sys
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext import db
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
import pygments.lexers
from pygments import highlight
from pygments.formatters import HtmlFormatter
class Sprunge(db.Model):
name = db.StringProperty()
content = db.TextProperty()
date = db.DateTimeProperty(auto_now_add=True)
class Index(webapp.RequestHandler):
u = 'http://sprunge.us'
r = 'sprunge'
def help(self, u, r):
f = 'data:text/html,<form action="%s" method="POST"><textarea name="%s" cols="80" rows="24"></textarea><br><button type="submit">%s</button></form>' % (u, r, r)
return """
<style> a { text-decoration: none } </style>
<pre>
sprunge(1) SPRUNGE sprunge(1)
NAME
sprunge: command line pastebin.
SYNOPSIS
<command> | curl -F '%s=<-' %s
DESCRIPTION
add <a href='http://pygments.org/docs/lexers/'>?<lang></a> to resulting url for line numbers and syntax highlighting
use <a href='%s'>this form</a> to paste from a browser
EXAMPLES
~$ curl -F '%s=<-' %s '< /path/to/file'
%s/VZiY
~$ firefox %s/VZiY?py#n-7
SEE ALSO
http://github.com/rupa/sprunge
</pre>""" % (r, u, f, r, u, u, u)
def new_id(self):
nid = ''
symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
while len(nid) < 4:
n = random.randint(0,35)
nid = nid + symbols[n:n+1]
return nid
def get(self, got):
if not got:
self.response.out.write(self.help(self.u, self.r))
return
# delete entry
#if got.endswith('/secretpassword'):
# got = got.split('/')
# c = Sprunge.gql('WHERE name = :1', got[0]).get()
# self.response.headers['Content-Type'] = 'text/plain; charset=UTF-8'
# if c:
# self.response.out.write('''
# deleting %s
# --------
# c.content
# ''' % (got[0], c.content))
# c.delete()
# else:
# self.response.out.write(got[0] + ' not here')
# return
# bulk delete
#if got.endswith('/secretpassword'):
# self.response.headers['Content-Type'] = 'text/plain; charset=UTF-8'
# c = Sprunge.gql('order by date asc limit 500')
# for x in c:
# self.response.out.write('%s\n' % x.date)
# x.delete()
# return
c = Sprunge.gql('WHERE name = :1', got).get()
if not c:
self.response.headers['Content-Type'] = 'text/plain; charset=UTF-8'
self.response.out.write(got + ' not found')
return
syntax = self.request.query_string
if not syntax:
self.response.headers['Content-Type'] = 'text/plain; charset=UTF-8'
self.response.out.write(c.content + '\n')
return
try:
lexer = pygments.lexers.get_lexer_by_name(syntax)
except:
lexer = pygments.lexers.TextLexer()
self.response.headers['Content-Type'] = 'text/html; charset=UTF-8'
self.response.out.write(highlight(c.content,
lexer,
HtmlFormatter(full=True,
style='borland',
lineanchors='n',
linenos='inline',
encoding='utf-8')))
def post(self, got):
self.response.headers['Content-Type'] = 'text/plain'
got = self.request.query_string
if self.request.get(self.r):
nid = self.new_id()
while Sprunge.gql('WHERE name = :1', nid).get():
nid = self.new_id()
s = Sprunge()
s.content = self.request.get(self.r)
s.name = nid
try:
s.put()
except Exception as ex:
self.response.out.write('{0}\n'.format(ex))
return
# clear out the same amount of space we've used up
to_clear = len(s.content)
while to_clear > 0:
old = Sprunge.gql('ORDER BY date ASC LIMIT 1').get()
if not old:
break
to_clear -= len(old.content)
old.delete()
self.response.out.write('{0}/{1}\n'.format(self.u, nid))
def main():
application = webapp.WSGIApplication([(r'/(.*)', Index)],debug=False)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()