Preparations for JSON support

This commit is contained in:
Klaus-Uwe Mitterer 2017-09-23 16:57:24 +02:00
parent fe7926d9bc
commit d643365e6c
5 changed files with 38 additions and 11 deletions

View file

@ -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 + "<station>\n"
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
out += (" " * indent + " <id>%s</id>\n" % self.useId()) if extid else ""
out += (" " * indent + " <type>%s</type>\n" % self.sttype) if sttype else ""
if coords:
out += " " * indent + " <coords>\n"
out += " " * indent + " <lon>%f</lon>\n" % self.xcoord
out += " " * indent + " <lat>%f</lat>\n" % self.ycoord
out += " " * indent + " </coords>\n"
out += (" " * indent + " <prodclass>%s</prodclass>\n" % self.prodclass) if prodclass else ""
out += " " * indent + "</station>\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

16
main.py
View file

@ -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 "<h1>500 Internal Server Error</h1>".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

View file

@ -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 = """<?xml version="1.0" encoding="UTF-8"?>
<stations>
"""

View file

@ -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

View file

@ -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 = """<?xml version="1.0" encoding="UTF-8"?>
<stations>
<stations>
"""
for station in validateName(name):
outtext += "<station><name>%s</name><id>%s</id></station>\n" % (station.name, station.useId())
outtext += station.xml(1)
outtext += "</stations>"