cylindertransport-web.py

changeset 14
ba3d8c56e6f5
parent 11
098335a1d510
child 18
ff1941c85fd3
--- a/cylindertransport-web.py	Wed Apr 05 00:39:31 2017 +0200
+++ b/cylindertransport-web.py	Wed Apr 05 00:59:45 2017 +0200
@@ -1,16 +1,22 @@
 #!/usr/bin/env python
 # -*- coding: UTF-8 -*-
+"""
+Web CGI script for 3D View
+"""
 
 import cgi, data, json, config
 from cylindertransport import CylinderSpacerCalculator
 
 # enable debugging
-import cgitb, sys
-sys.stderr = sys.stdout
+#import sys
+#sys.stderr = sys.stdout
+#import cgitb
 #cgitb.enable()
 
-
 def do_action(args):
+    """
+    process some actions (like JSON requests)
+    """
     if args["action"].value == "calculate":
         cylinders = args.getlist("cylinders[]")
         calc = CylinderSpacerCalculator(cylinders)
@@ -22,24 +28,26 @@
     else:
         print "unknown Action %s" % args["action"].value
 
-
-
-
-
-
-print("Content-Type: text/html;charset=utf-8")
-print("")
+def run():
+    """
+    Main program, without action displays the html content
+    """
+    print "Content-Type: text/html;charset=utf-8"
+    print ""
 
-args = cgi.FieldStorage()
-if "action" in args:
-    do_action(args)
-else:
-    # display the html content
-    content = open("stlviewer.html", "r").read()
-    cyls = ""
-    for cyl in sorted(data.CYLINDER.keys()):
-        cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % (
-            cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4]);
-    content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls)
+    args = cgi.FieldStorage()
+    if "action" in args:
+        do_action(args)
+    else:
+        # display the html content
+        content = open("stlviewer.html", "r").read()
+        cyls = ""
+        for cyl in sorted(data.CYLINDER.keys()):
+            cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % (
+                cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4])
+        content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls)
 
-    print content
+        print content
+
+if __name__ == "__main__":
+    run()

mercurial