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

#!/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 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)
        calc.calculate()
        print json.dumps({
            "objects": calc.circles,
            "scale3d": config.SCALE3D
            })
    else:
        print "unknown Action %s" % args["action"].value

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)

        print content

if __name__ == "__main__":
    run()

mercurial