forked from leafstorm/railgen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-nodes.py
executable file
·67 lines (58 loc) · 1.94 KB
/
generate-nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
import yaml
import json
import argparse
from pprint import pprint
def getStopStation(stop):
if type(stop) is list:
return stop[0]
else:
return stop
def removeCorners(lines):
for linename,line in lines.iteritems():
stops = []
for stop in line["stops"]:
if not(type(getStopStation(stop)) is int):
stops.append(stop)
line["stops"]=stops
lines[linename] = line
return lines
parser = argparse.ArgumentParser(description='''
Generates a json file containing node link information,
to be used to determine fastest route.''')
parser.add_argument('inputfile', metavar='INPUT', type=str, help='input yaml file')
parser.add_argument('outputfile', metavar='OUTPUT', type=str, help='output json file')
parser.add_argument('--javascript', action='store_true', help='output valid js file hack')
parser.add_argument('--quiet', action='store_true', help='suppress JSON on stdout')
args = parser.parse_args()
yaml_file = open(args.inputfile, 'r')
yaml_data = yaml.load(yaml_file)
yaml_file.close()
stations = yaml_data["stations"]
lines = yaml_data["lines"]
lines = removeCorners(lines)
for station in stations:
stations[station]["destinations"] = []
for linename,line in lines.iteritems():
previous = ""
for stopindex, stop in enumerate(line["stops"]):
if stopindex > 0 or line["flow"] == "loop":
previousStation = getStopStation(line["stops"][stopindex-1])
stations[getStopStation(stop)]["destinations"].append(previousStation)
try:
if line["flow"] == "loop" and stopindex+1 > len(line["stops"])-1:
nextStation = getStopStation(line["stops"][0])
else:
nextStation = getStopStation(line["stops"][stopindex+1])
stations[getStopStation(stop)]["destinations"].append(nextStation)
except IndexError:
pass
if not(args.quiet):
pprint(stations)
json_file = open(args.outputfile, 'w')
if args.javascript:
json_file.write("stations =")
json.dump(stations, json_file)
if args.javascript:
json_file.write(";")
json_file.close()