Start refactoring main.py
This commit is contained in:
parent
316eb271ef
commit
e6270fa43a
130
main.py
130
main.py
|
@ -7,6 +7,73 @@ import workers.val
|
|||
import workers.closest
|
||||
import workers.radar
|
||||
|
||||
HTTP200 = "200 OK"
|
||||
HTTP400 = "400 Bad Request"
|
||||
HTTP500 = "500 Internal Server Error"
|
||||
|
||||
HTML = "text/html"
|
||||
JSON = "application/json"
|
||||
XML = "text/xml"
|
||||
|
||||
def doConn(args, cfrm = None, cto = None):
|
||||
try:
|
||||
try:
|
||||
frm = (cfrm or args["from"][0]).encode("latin-1").decode("utf-8")
|
||||
to = (cto or args["to"][0]).encode("latin-1").decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
frm = cfrm or args["from"][0]
|
||||
to = cto or args["to"][0]
|
||||
|
||||
if not frm or not to:
|
||||
raise ValueError()
|
||||
|
||||
except Exception:
|
||||
content = "<h1>400 Bad Request</h1>\n"
|
||||
content += "\"from\"and \"to\" values are required for this type of request.\n"
|
||||
return (HTTP400, HTML, content)
|
||||
|
||||
count = args["count"][0] if "count" in args and args["count"] else 6
|
||||
date = args["date"][0] if "date" in args and args["date"] else datetime.datetime.strftime(datetime.datetime.now(pytz.timezone("Europe/Vienna")),"%d.%m.%Y")
|
||||
time = args["time"][0] if "time" in args and args["time"] else datetime.datetime.strftime(datetime.datetime.now(pytz.timezone("Europe/Vienna")),"%H:%M")
|
||||
mode = True if "mode" in args and args["mode"] and args["mode"][0].lower() == "arr" else False
|
||||
details = True if "details" in args else False
|
||||
|
||||
try:
|
||||
count = int(count)
|
||||
if count < 0 or count > 10:
|
||||
raise ValueError()
|
||||
except:
|
||||
content = "<h1>400 Bad Request</h1>\n"
|
||||
content += "The \"count\" value must be a value between 0 and 10."
|
||||
return (HTTP400, HTML, content)
|
||||
|
||||
try:
|
||||
outtime = datetime.datetime.strptime("%s %s" % (date, time), "%d.%m.%Y %H:%M")
|
||||
except:
|
||||
content = "<h1>400 Bad Request</h1>\n"
|
||||
content += "The \"date\" value must be in DD.MM.YYYY format, the \"time\" value must be in HH:MM format."
|
||||
return (HTTP400, HTML, content)
|
||||
|
||||
via = list(args["via"]) if "via" in args else None
|
||||
|
||||
if via and len(via) > 3:
|
||||
content = "<h1>400 Bad Request</h1>\n"
|
||||
content += "It is not possible to route through more than three \"via\" stations."
|
||||
return (HTTP400, HTML, content)
|
||||
|
||||
json = "json" in args
|
||||
|
||||
try:
|
||||
content = workers.conn.worker(frm, to, count, outtime, mode, details, json, via)
|
||||
except Exception as e:
|
||||
content = "<h1>500 Internal Server Error</h1>\n"
|
||||
if "debug" in args:
|
||||
content += str(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)
|
||||
|
@ -53,66 +120,9 @@ def application(env, re):
|
|||
json = "json" in args
|
||||
|
||||
if rtype.lower() in ["conn", "connection"]:
|
||||
try:
|
||||
try:
|
||||
frm = (cfrm or args["from"][0]).encode("latin-1").decode("utf-8")
|
||||
to = (cto or args["to"][0]).encode("latin-1").decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
frm = cfrm or args["from"][0]
|
||||
to = cto or args["to"][0]
|
||||
|
||||
if not frm or not to:
|
||||
raise ValueError()
|
||||
|
||||
except:
|
||||
re("400 Bad Request", [])
|
||||
yield "<h1>400 Bad Request</h1>".encode()
|
||||
yield "\"from\" and \"to\" values are required for this type of request.".encode()
|
||||
return
|
||||
|
||||
count = args["count"][0] if "count" in args and args["count"] else 6
|
||||
date = args["date"][0] if "date" in args and args["date"] else datetime.datetime.strftime(datetime.datetime.now(pytz.timezone("Europe/Vienna")),"%d.%m.%Y")
|
||||
time = args["time"][0] if "time" in args and args["time"] else datetime.datetime.strftime(datetime.datetime.now(pytz.timezone("Europe/Vienna")),"%H:%M")
|
||||
mode = True if "mode" in args and args["mode"] and args["mode"][0].lower() == "arr" else False
|
||||
details = True if "details" in args else False
|
||||
|
||||
try:
|
||||
count = int(count)
|
||||
if count < 0 or count > 10:
|
||||
raise ValueError()
|
||||
except:
|
||||
re("400 Bad Request", [])
|
||||
yield "<h1>400 Bad Request</h1>".encode()
|
||||
yield "The \"count\" value must be a value between 0 and 10.".encode()
|
||||
return
|
||||
|
||||
try:
|
||||
outtime = datetime.datetime.strptime("%s %s" % (date, time), "%d.%m.%Y %H:%M")
|
||||
except:
|
||||
re("400 Bad Request", [])
|
||||
yield "<h1>400 Bad Request</h1>".encode()
|
||||
yield "The \"date\" value must be in DD.MM.YYYY format, the \"time\" value must be in HH:MM format.".encode()
|
||||
return
|
||||
|
||||
via = list(args["via"]) if "via" in args else None
|
||||
|
||||
if via and len(via) > 3:
|
||||
re("400 Bad Request", [])
|
||||
yield "<h1>400 Bad Request</h1>".encode()
|
||||
yield "It is not possible to route through more than three \"via\" stations.".encode()
|
||||
return
|
||||
|
||||
try:
|
||||
output = workers.conn.worker(frm, to, count, outtime, mode, details, json, via)
|
||||
except Exception as e:
|
||||
re("500 Internal Server Error", [])
|
||||
yield "<h1>500 Internal Server Error</h1>".encode()
|
||||
if "debug" in args:
|
||||
yield str(e).encode()
|
||||
return
|
||||
|
||||
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
||||
yield output.encode()
|
||||
status, ctype, content = doConn(args, cfrm, cto)
|
||||
re(status, [("Content-Type", ctype)])
|
||||
yield content.encode()
|
||||
return
|
||||
|
||||
elif rtype.lower() in ["val", "validate"]:
|
||||
|
|
Loading…
Reference in a new issue