-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.py
136 lines (109 loc) · 3.71 KB
/
p.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
#!/usr/env python
# coding: utf8
'''
Created on 20.09.2011
p is a paste service. As the brother of s it is designed to be fast and small.
@author: Kevin Zuber
Copyright (c) 2011, Kevin Zuber.
License: MIT (see LICENSE for details)
'''
# python standard library
import sys
import os
import difflib
# 3rd party python module
from bottle import route, run, static_file, view, abort, redirect, request
from bottle import response
import bottle
sys.path.append(os.path.dirname(__file__))
# own python module
from service.paster import Paster
from service import formater
paste = Paster(os.path.join(os.path.dirname(__file__),"p.db"))
hdiffer = difflib.HtmlDiff(wrapcolumn=82)
def force_unicode(obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj
fu = force_unicode
@route("/")
@view("index")
def index():
return {"format_default": formater.format_default,
"formats_long_to_short": formater.formats_long_to_short,
"formats_short_to_long": formater.formats_short_to_long,
"top_formats": paste.top_formats,
"formats_long": formater.formats_long}
@route("/", method="POST")
def add_paste():
f = request.forms
if f.format not in formater.formats_short:
f.format = formater.format_default
if f.title and f.content and f.format:
key = paste.add(fu(f.title), fu(f.content),
fu(f.format), request['REMOTE_ADDR'])
redirect("/" + key)
else:
abort(400, "You did not provide all required fields.")
@route("/:key")
@view("view_paste")
def view_paste(key):
try:
thing = paste.thing[key]
format = thing["format"]
if format not in formater.formats_short:
format = formater.format_default
return {"title": thing["title"],
"content": thing["content"],
"content_html": formater.format(thing["content"], format),
"format_used": thing["format"],
"time": thing["time"],
"formats_long_to_short": formater.formats_long_to_short,
"formats_short_to_long": formater.formats_short_to_long,
"formats_long": formater.formats_long,
"top_formats": paste.top_formats,
"key": key}
except KeyError:
abort(404, "Key " + key + " not found")
@route("/:key1/d", method="POST")
def redirect_to_diff(key1):
redirect("/"+key1+"/d/"+request.forms.diffkey)
@route("/:key1/d/:key2")
@view("view_diff")
def view_html_diff(key1, key2):
try:
thing1 = paste.thing[key1]
thing2 = paste.thing[key2]
title1 = thing1["title"]
title2 = thing2["title"]
if title1 == "no title":
title1 = key1
if title2 == "no title":
title2 = key2
return {"title1": title1,
"title2": title2,
"content_html": hdiffer.make_table(thing1["content"].splitlines(1), thing2["content"].splitlines(1), title1, title2),
"key1": key1,
"key2": key2,
}
except KeyError:
abort(404, "Key " + key1 + " or " + key2 + " not found")
@route("/:key/raw")
def view_paste_raw(key):
try:
thing = paste.thing[key]
response.content_type = "text/plain; charset=utf-8"
return thing["content"]
except KeyError:
abort(404, "Key " + key + " not found")
@route('/static/:path#.+#')
def server_static(path):
return static_file(path, root='./static')
bottle.TEMPLATE_PATH.insert(0,
os.path.join(os.path.dirname(__file__), 'views/'))
if __name__ == '__main__':
bottle.debug(True)
run(host="localhost", port=8080, reloader=True)
else:
application = bottle.default_app()