oebb_py/classes/service.py

111 lines
4.3 KiB
Python
Raw Normal View History

2017-09-25 20:41:51 +00:00
import datetime
class Service:
def __init__(self, name, depst, deptime, arrst, arrtime, dest = None, deppf = None, currdep = None, arrpf = None, curarr = None):
self.name = name
self.dest = dest
self.depst = depst
self.deptime = deptime
self.arrst = arrst
self.arrtime = arrtime
self.deppf = deppf
self.currdep = currdep
self.arrpf = arrpf
self.curarr = curarr
def duration(self):
return self.arrtime - self.deptime
def xml(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True, stationkwargs = {}):
out = " " * indent + "<service%s>\n" % ("" if iid is False else " id=\"%i\"" % iid)
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
if dest and self.dest:
out += " " * indent + " <destination>\n"
out += self.dest.xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </destination>\n"
if depst or deptime or deppf or currdep:
out += " " * indent + " <departure>\n"
out += (self.depst.xml(indent + 2, **stationkwargs) + "\n") if depst else ""
if deptime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.deptime, "%H:%M")
out += (" " * indent + " <current>%s</current>\n" % self.currdep) if currdep and self.currdep else ""
out += (" " * indent + " <platform>%s</platform>\n" % self.deppf) if deppf and self.deppf else ""
out += " " * indent + " </departure>\n"
if arrst or arrtime or arrpf or curarr:
out += " " * indent + " <arrival>\n"
out += (self.arrst.xml(indent + 2, **stationkwargs) + "\n") if arrst else ""
if arrtime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.arrtime, "%H:%M")
out += (" " * indent + " <current>%s</current>\n" % self.curarr) if curarr and self.curarr else ""
out += (" " * indent + " <platform>%s</platform>\n" % self.arrpf) if arrpf and self.arrpf else ""
out += " " * indent + " </arrival>\n"
out += " " * indent + "</service>"
return out
def json(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True, stationkwargs = {}):
out = " " * indent + "{\n"
out += (" " * indent + " \"@id\": %i,\n" % iid) if iid is not False else ""
out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else ""
if dest and self.dest:
out += " " * indent + " \"destination\":\n"
out += self.dest.json(indent + 2, **stationkwargs) + ",\n"
if depst or deptime or deppf or currdep:
dep = " " * indent + " \"departure\": {\n"
if depst:
dep += " " * indent + " \"station\":\n"
dep += self.depst.json(indent + 3, **stationkwargs) + ",\n"
if deptime:
dep += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
dep += " " * indent + " \"time\": \"%s\",\n" % datetime.datetime.strftime(self.deptime, "%H:%M")
dep += (" " * indent + " \"current\": \"%s\",\n" % self.currdep) if currdep and self.currdep else ""
dep += (" " * indent + " \"platform\": \"%s\",\n" % self.deppf) if deppf and self.deppf else ""
dep = "".join(dep.rsplit(",", 1))
out += dep + " " * indent + " },\n"
if arrst or arrtime or arrpf or curarr:
arr = " " * indent + " \"arrival\": {\n"
if arrst:
arr += " " * indent + " \"station\":\n"
arr += self.arrst.json(indent + 3, **stationkwargs) + ",\n"
if arrtime:
arr += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
arr += " " * indent + " \"time\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime, "%H:%M")
arr += (" " * indent + " \"current\": \"%s\",\n" % self.curarr) if curarr and self.curarr else ""
arr += (" " * indent + " \"platform\": \"%s\",\n" % self.arrpf) if arrpf and self.arrpf else ""
arr = "".join(arr.rsplit(",", 1))
out += arr + " " * indent + " },\n"
out += " " * indent + "}"
out = "".join(out.rsplit(",", 1))
return out