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