Refactoring: Split functions in main.py

This commit is contained in:
Klaus-Uwe Mitterer 2017-10-28 15:57:05 +02:00
parent 31592abfea
commit 76fd3ba93e
1 changed files with 57 additions and 51 deletions

108
main.py
View File

@ -101,6 +101,57 @@ def doVal(args, cfrm):
return HTTP200, JSON if json else XML, content
def doNearby(args):
try:
lat = float(args["lat"][0].replace(",", "."))
lon = float(args["lon"][0].replace(",", "."))
if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
raise ValueError()
except:
content = "<h1>400 Bad Request</h1>\n"
content += "\"lat\" and \"lon\" values are required for this type of request."
return HTTP400, HTML, content
distance = args["distance"][0] if "distance" in args and args["distance"] else 1000
try:
distance = int(distance)
if distance < 0 or distance > 10000:
raise ValueError()
except:
content = "<h1>400 Bad Request</h1>\n"
content += "\"distance\" must be a value between 0 and 10000."
return HTTP400, HTML, content
json = "json" in args
try:
content = workers.closest.worker(lat, lon, distance, json)
except Exception as e:
content = "<h1>500 Internal Server Error</h1>"
if "debug" in args:
content += repr(e)
return HTTP500, HTML, content
return HTTP200, JSON if json else XML, content
def doRadar(args):
trains = args["train"] if "train" in args else None
json = "json" in args
try:
content = workers.radar.worker(trains, json)
except Exception as e:
content = "<h1>500 Internal Server Error</h1>\n"
if "debug" in args:
content += repr(e)
return HTTP500, HTML, content
return HTTP200, JSON if json else XML, content
def application(env, re):
if env["REQUEST_METHOD"] == "POST":
args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True)
@ -144,8 +195,6 @@ def application(env, re):
yield "A request type must be provided.".encode()
return
json = "json" in args
if rtype.lower() in ["conn", "connection"]:
status, ctype, content = doConn(args, cfrm, cto)
re(status, [("Content-Type", ctype)])
@ -159,58 +208,15 @@ def application(env, re):
return
elif rtype.lower() in ["closest", "close", "near", "nearby"]:
try:
lat = float(args["lat"][0].replace(",", "."))
lon = float(args["lon"][0].replace(",", "."))
if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
raise ValueError()
except:
re("400 Bad Request", [])
yield "<h1>400 Bad Request</h1>".encode()
yield "\"lat\" and \"lon\" values are required for this type of request.".encode()
return
distance = args["distance"][0] if "distance" in args and args["distance"] else 1000
try:
distance = int(distance)
if distance < 0 or distance > 10000:
raise ValueError()
except:
re("400 Bad Request", [])
yield "<h1>400 Bad Request</h1>"
yield "\"distance\" must be a value between 0 and 10000."
return
try:
output = workers.closest.worker(lat, lon, distance, json)
except Exception as e:
re("500 Internal Server Error", [])
yield "<h1>500 Internal Server Error</h1>".encode()
if "debug" in args:
yield e.encode()
return
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
yield output.encode()
status, ctype, content = doNearby(args)
re(status, [("Content-Type", ctype)])
yield content.encode()
return
elif rtype.lower() in ["radar", "live"]:
trains = args["train"] if "train" in args else None
try:
output = workers.radar.worker(trains, json)
except Exception as e:
re("500 Internal Server Error", [])
yield "<h1>500 Internal Server Error</h1>".encode()
if "debug" in args:
yield e.encode()
return
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
yield output.encode()
status, ctype, content = doRadar(args)
re(status, [("Content-Type", ctype)])
yield content.encode()
return
else: