cylindertransport-web.py

Wed, 05 Apr 2017 00:59:45 +0200

author
mdd
date
Wed, 05 Apr 2017 00:59:45 +0200
changeset 14
ba3d8c56e6f5
parent 11
098335a1d510
child 18
ff1941c85fd3
permissions
-rwxr-xr-x

code cleanup

11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
1 #!/usr/bin/env python
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
2 # -*- coding: UTF-8 -*-
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
3 """
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
4 Web CGI script for 3D View
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
5 """
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
6
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
7 import cgi, data, json, config
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
8 from cylindertransport import CylinderSpacerCalculator
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
9
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
10 # enable debugging
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
11 #import sys
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
12 #sys.stderr = sys.stdout
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
13 #import cgitb
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
14 #cgitb.enable()
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
15
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
16 def do_action(args):
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
17 """
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
18 process some actions (like JSON requests)
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
19 """
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
20 if args["action"].value == "calculate":
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
21 cylinders = args.getlist("cylinders[]")
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
22 calc = CylinderSpacerCalculator(cylinders)
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
23 calc.calculate()
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
24 print json.dumps({
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
25 "objects": calc.circles,
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
26 "scale3d": config.SCALE3D
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
27 })
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
28 else:
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
29 print "unknown Action %s" % args["action"].value
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
30
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
31 def run():
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
32 """
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
33 Main program, without action displays the html content
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
34 """
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
35 print "Content-Type: text/html;charset=utf-8"
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
36 print ""
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
37
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
38 args = cgi.FieldStorage()
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
39 if "action" in args:
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
40 do_action(args)
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
41 else:
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
42 # display the html content
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
43 content = open("stlviewer.html", "r").read()
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
44 cyls = ""
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
45 for cyl in sorted(data.CYLINDER.keys()):
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
46 cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % (
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
47 cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4])
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
48 content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls)
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
49
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
50 print content
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
51
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
52 if __name__ == "__main__":
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
53 run()

mercurial