summaryrefslogtreecommitdiffstats
path: root/pasteme.py
blob: d0bd2fe06e68934b176caf5b11857d1f89f96d28 (plain)
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
#!/usr/bin/python3

import bottle
import pygments
import pygments.formatters
import pygments.lexers
import identigen
import config
from pathlib import Path

application = bottle.default_app() # application used for wsgi mode
pathbase = Path(config.pastedir)
pygment_formater = pygments.formatters.HtmlFormatter(linenos="table")

if not pathbase.exists():
    pathbase.mkdir(mode=0o700, parents=True)

@bottle.route('/')
def route_root():
    return bottle.template('root')

@bottle.route('/', method='POST')
def route_paste_post():
    content = bottle.request.forms.get('content')
    try:
        pid = identigen.generate(content)
    except AttributeError as e:
        print(e)
        bottle.abort(400)
    path = pathbase / pid
    with path.open(mode='wb') as fd:
        fd.write(content.encode('utf8'))
    bottle.redirect('/' + pid)

@bottle.route('/static/<path:path>')
def route_static(path):
    return bottle.static_file(path, root='static')

@bottle.route('/<pid>')
@bottle.route('/<pid>/<pformat>')
def route_paste_get(pid, pformat='colored'):
    if pformat != 'colored' and pformat != 'raw':
        return bottle.template('bad_format')
    path = pathbase / pid
    try:
        with path.open() as fd:
            content = fd.read()
    except IOError:
        # use this template for all file based exception
        bottle.abort(404)
    if pformat == 'colored':
        try:
            lexer = pygments.lexers.guess_lexer(content)
            content = pygments.highlight(content, lexer, pygment_formater)
        except pygments.util.ClassNotFound:
            pass
        return bottle.template('paste', content=content, pid=pid)
    bottle.response.content_type = 'text/plain; charset=UTF8' # HTTP header
    return content

@bottle.error(404)
def error404(error):
    return bottle.template("not_found")

@bottle.error(400)
def error400(error):
    return bottle.template("bad_request")

if __name__ == '__main__':
    print('I: Starting application with development server')
    bottle.run(host='0.0.0.0', port=8080, debug=True, reloader=True)
else:
    print('I: Starting application as a wsgi application')