-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.py
executable file
·123 lines (102 loc) · 3.45 KB
/
server.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
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4
"""
1. Install Scruffy
2. Run server:
$ pip install bottle
$ python server.py
3. Browse http://localhost:8080/
"""
from bottle import route, run, template, request, response, static_file, HTTPError
import os
import re
import reportlab.graphics
import suml.suml2pic
import suml.yuml2dot
import svglib.svglib
import urllib
import xml.dom.expatbuilder
import xml.etree.ElementTree as ET
@route('/favicon.ico')
def favicon():
return static('favicon.ico')
@route('/robots.txt')
def robots():
return static('robots.txt')
# Those files should be served directly by the web server,
# but for ease during development they are served here.
@route('/static/<path:path>')
def static(path):
return static_file(path, 'static')
@route('/<type>/<spec:path>.<ext:re:png|svg|pdf>')
def image(type, spec=' ', ext='png'):
# Parameters for `suml`.
import suml.common
import optparse
options = optparse.Values(({
'scruffy': True,
'png': ext == 'png',
'svg': ext == 'svg' or ext == 'pdf',
'font': os.getenv('SCRUFFY_FONT', suml.common.defaultScruffyFont()),
'shadow': False,
}))
from tempfile import SpooledTemporaryFile
fout = SpooledTemporaryFile()
# Execute Scruffy `suml`.
if type == 'class':
suml.yuml2dot.transform(spec, fout, options)
elif type == 'sequence':
suml.suml2pic.transform(spec, fout, options)
else:
return HTTPError(404, 'Unhandled diagram type.')
# Retrieve the data generated.
fout.seek(0)
data = fout.read()
fout.close()
# Convert SVG to PDF?
if ext == 'pdf':
# Load SVG file.
doc = xml.dom.expatbuilder.parseString(data)
# Convert to a RLG drawing
svg_renderer = svglib.svglib.SvgRenderer()
svg_renderer.render(doc.documentElement)
drawing = svg_renderer.finish()
# Generate PDF.
data = reportlab.graphics.renderPDF.drawToString(drawing)
# Server the generated image.
if ext == 'png':
response.content_type = 'image/png'
elif ext == 'svg':
response.content_type = 'image/svg+xml'
elif ext == 'pdf':
response.content_type = 'application/pdf'
else:
return HTTPError(500, 'Unhandled extension type.')
return data
@route('/')
def index():
return template('home.tpl')
@route('/<type>/')
@route('/<type>/<spec:path>')
def index(type='class', spec=''):
spec = request.query.spec or spec
if type != 'class' and type != 'sequence':
return HTTPError(404, 'Unhandled diagram type.')
autocollapse = True
if not spec:
if type =='class':
spec = '// Cool Class Diagram,[ICustomer|+name;+email|]^-.-[Customer],[Customer]<>-orders*>[Order],[Order]++-0..*>[LineItem],[Order]-[note:Aggregate root.]'
elif type == 'sequence':
spec = '[Patron]order food>[Waiter],[Waiter]order food>[Cook],[Waiter]serve wine>[Patron],[Cook]pickup>[Waiter],[Waiter]serve food>[Patron],[Patron]pay>[Cashier]'
else:
return HTTPError(404, 'Unhandled diagram type.')
autocollapse = False
encoded_spec = urllib.quote_plus(spec.replace('\r\n', ',').replace('\r', ',').replace('\n', ','))
return template(
'index.tpl',
type=type,
spec=spec.replace(',', '\n'),
encoded_spec=encoded_spec,
autocollapse=autocollapse)
if __name__ == "__main__":
run(host='0.0.0.0', port=8080)