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 + "\n" % ("" if iid is False else " id=\"%i\"" % iid) out += (" " * indent + " %s\n" % self.name) if name else "" if dest and self.dest: out += " " * indent + " \n" out += self.dest.xml(indent + 2, **stationkwargs) + "\n" out += " " * indent + " \n" if depst or deptime or deppf or currdep: out += " " * indent + " \n" out += (self.depst.xml(indent + 2, **stationkwargs) + "\n") if depst else "" if deptime: out += " " * indent + " %s\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y") out += " " * indent + " \n" % datetime.datetime.strftime(self.deptime, "%H:%M") out += (" " * indent + " %s\n" % self.currdep) if currdep and self.currdep else "" out += (" " * indent + " %s\n" % self.deppf) if deppf and self.deppf else "" out += " " * indent + " \n" if arrst or arrtime or arrpf or curarr: out += " " * indent + " \n" out += (self.arrst.xml(indent + 2, **stationkwargs) + "\n") if arrst else "" if arrtime: out += " " * indent + " %s\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y") out += " " * indent + " \n" % datetime.datetime.strftime(self.arrtime, "%H:%M") out += (" " * indent + " %s\n" % self.curarr) if curarr and self.curarr else "" out += (" " * indent + " %s\n" % self.arrpf) if arrpf and self.arrpf else "" out += " " * indent + " \n" out += " " * indent + "" 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