Preparations for JSON support
This commit is contained in:
parent
fe7926d9bc
commit
d643365e6c
|
@ -16,6 +16,29 @@ class Station:
|
||||||
def lon(self):
|
def lon(self):
|
||||||
return self.xcoord
|
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:
|
class Service:
|
||||||
def __init__(self, name, depst, deptime, arrst, arrtime, dest = None, deppf = None, currdep = None, arrpf = None, curarr = None):
|
def __init__(self, name, depst, deptime, arrst, arrtime, dest = None, deppf = None, currdep = None, arrpf = None, curarr = None):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
16
main.py
16
main.py
|
@ -2,6 +2,8 @@ import cgi
|
||||||
import datetime
|
import datetime
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
|
import xmljson
|
||||||
|
|
||||||
import workers.conn
|
import workers.conn
|
||||||
import workers.val
|
import workers.val
|
||||||
import workers.closest
|
import workers.closest
|
||||||
|
@ -23,6 +25,8 @@ def application(env, re):
|
||||||
yield "A request type must be provided.".encode()
|
yield "A request type must be provided.".encode()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
json = "json" in args
|
||||||
|
|
||||||
if rtype.lower() in ["conn", "connection"]:
|
if rtype.lower() in ["conn", "connection"]:
|
||||||
try:
|
try:
|
||||||
frm = args["from"][0]
|
frm = args["from"][0]
|
||||||
|
@ -62,7 +66,7 @@ def application(env, re):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
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:
|
except Exception as e:
|
||||||
re("500 Internal Server Error", [])
|
re("500 Internal Server Error", [])
|
||||||
yield "<h1>500 Internal Server Error</h1>".encode()
|
yield "<h1>500 Internal Server Error</h1>".encode()
|
||||||
|
@ -70,7 +74,7 @@ def application(env, re):
|
||||||
yield str(e).encode()
|
yield str(e).encode()
|
||||||
return
|
return
|
||||||
|
|
||||||
re("200 OK", [])
|
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
||||||
yield output.encode()
|
yield output.encode()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -88,7 +92,7 @@ def application(env, re):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
output = workers.val.worker(name)
|
output = workers.val.worker(name, json)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
re("500 Internal Server Error", [])
|
re("500 Internal Server Error", [])
|
||||||
|
@ -97,7 +101,7 @@ def application(env, re):
|
||||||
yield str(e).encode()
|
yield str(e).encode()
|
||||||
return
|
return
|
||||||
|
|
||||||
re("200 OK", [])
|
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
||||||
yield output.encode()
|
yield output.encode()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -128,7 +132,7 @@ def application(env, re):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
output = workers.closest.worker(lat, lon, distance)
|
output = workers.closest.worker(lat, lon, distance, json)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
re("500 Internal Server Error", [])
|
re("500 Internal Server Error", [])
|
||||||
|
@ -137,7 +141,7 @@ def application(env, re):
|
||||||
yield e.encode()
|
yield e.encode()
|
||||||
return
|
return
|
||||||
|
|
||||||
re("200 OK", [])
|
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
||||||
yield output.encode()
|
yield output.encode()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ def findStations(lat, lon, distance = 1000, validate = True):
|
||||||
|
|
||||||
return stations
|
return stations
|
||||||
|
|
||||||
def worker(lat, lon, distance = 1000):
|
def worker(lat, lon, distance = 1000, json = False):
|
||||||
outtext = """<?xml version="1.0" encoding="UTF-8"?>
|
outtext = """<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<stations>
|
<stations>
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -125,7 +125,7 @@ def connRequest(frm, to, count = 3, time = datetime.datetime.now(), mode = False
|
||||||
|
|
||||||
yield con
|
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)
|
conns = connRequest(getStation(frm), getStation(to), count, time, mode, details)
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
|
|
|
@ -21,13 +21,13 @@ def validateName(name):
|
||||||
|
|
||||||
yield Station(name = name, sttype = sttype, extid = extid, xcoord = xcoord, ycoord = ycoord, prodclass = prodclass)
|
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"?>
|
outtext = """<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<stations>
|
<stations>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
for station in validateName(name):
|
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>"
|
outtext += "</stations>"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue