From d643365e6c5be62fa416a6be78535f9e530f375b Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sat, 23 Sep 2017 16:57:24 +0200 Subject: [PATCH] Preparations for JSON support --- classes/__init__.py | 23 +++++++++++++++++++++++ main.py | 16 ++++++++++------ workers/closest.py | 2 +- workers/conn.py | 2 +- workers/val.py | 6 +++--- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/classes/__init__.py b/classes/__init__.py index fd10124..37a0792 100644 --- a/classes/__init__.py +++ b/classes/__init__.py @@ -16,6 +16,29 @@ class Station: def lon(self): return self.xcoord + def json(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False): + out = "" + + return + + def xml(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False): + out = " " * indent + "\n" + out += (" " * indent + " %s\n" % self.name) if name else "" + out += (" " * indent + " %s\n" % self.useId()) if extid else "" + out += (" " * indent + " %s\n" % self.sttype) if sttype else "" + + if coords: + out += " " * indent + " \n" + out += " " * indent + " %f\n" % self.xcoord + out += " " * indent + " %f\n" % self.ycoord + out += " " * indent + " \n" + + out += (" " * indent + " %s\n" % self.prodclass) if prodclass else "" + + out += " " * indent + "\n" + + return out + class Service: def __init__(self, name, depst, deptime, arrst, arrtime, dest = None, deppf = None, currdep = None, arrpf = None, curarr = None): self.name = name diff --git a/main.py b/main.py index 16c43f0..31bbc83 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,8 @@ import cgi import datetime import pytz +import xmljson + import workers.conn import workers.val import workers.closest @@ -23,6 +25,8 @@ def application(env, re): yield "A request type must be provided.".encode() return + json = "json" in args + if rtype.lower() in ["conn", "connection"]: try: frm = args["from"][0] @@ -62,7 +66,7 @@ def application(env, re): return try: - output = workers.conn.worker(frm, to, count, outtime, mode, details) + output = workers.conn.worker(frm, to, count, outtime, mode, details, json) except Exception as e: re("500 Internal Server Error", []) yield "

500 Internal Server Error

".encode() @@ -70,7 +74,7 @@ def application(env, re): yield str(e).encode() return - re("200 OK", []) + re("200 OK", [("Content-Type", "application/json" if json else "text/xml")]) yield output.encode() return @@ -88,7 +92,7 @@ def application(env, re): return try: - output = workers.val.worker(name) + output = workers.val.worker(name, json) except Exception as e: re("500 Internal Server Error", []) @@ -97,7 +101,7 @@ def application(env, re): yield str(e).encode() return - re("200 OK", []) + re("200 OK", [("Content-Type", "application/json" if json else "text/xml")]) yield output.encode() return @@ -128,7 +132,7 @@ def application(env, re): return try: - output = workers.closest.worker(lat, lon, distance) + output = workers.closest.worker(lat, lon, distance, json) except Exception as e: re("500 Internal Server Error", []) @@ -137,7 +141,7 @@ def application(env, re): yield e.encode() return - re("200 OK", []) + re("200 OK", [("Content-Type", "application/json" if json else "text/xml")]) yield output.encode() return diff --git a/workers/closest.py b/workers/closest.py index c4c383d..5dc83ae 100644 --- a/workers/closest.py +++ b/workers/closest.py @@ -59,7 +59,7 @@ def findStations(lat, lon, distance = 1000, validate = True): return stations -def worker(lat, lon, distance = 1000): +def worker(lat, lon, distance = 1000, json = False): outtext = """ """ diff --git a/workers/conn.py b/workers/conn.py index 464b197..014328d 100644 --- a/workers/conn.py +++ b/workers/conn.py @@ -125,7 +125,7 @@ def connRequest(frm, to, count = 3, time = datetime.datetime.now(), mode = False yield con -def worker(frm, to, count = 3, time = datetime.datetime.now(pytz.timezone("Europe/Vienna")), mode = False, details = False): +def worker(frm, to, count = 3, time = datetime.datetime.now(pytz.timezone("Europe/Vienna")), mode = False, details = False, json = False): conns = connRequest(getStation(frm), getStation(to), count, time, mode, details) i = 0 diff --git a/workers/val.py b/workers/val.py index a09856d..77c9ec6 100644 --- a/workers/val.py +++ b/workers/val.py @@ -21,13 +21,13 @@ def validateName(name): yield Station(name = name, sttype = sttype, extid = extid, xcoord = xcoord, ycoord = ycoord, prodclass = prodclass) -def worker(name): +def worker(name, json = False): outtext = """ - + """ for station in validateName(name): - outtext += "%s%s\n" % (station.name, station.useId()) + outtext += station.xml(1) outtext += ""