-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.py
137 lines (105 loc) · 3.48 KB
/
group.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import os;
import re;
import Data;
import Filefunc;
import Service;
# Groups all the different service data files together into one big one
# And use them to automatically produce the trains
services = {};
attrs = {}; # the attributes of those services
# the list of files we got data from
sources = [];
def sameStation(a, b):
if a == b: # either the same station + track
return True;
if a[0] != b[0]: # or the same station
return False;
if a[1] == '' or b[1] == '': # with one missing their track nr
return True;
return a[1][:len(b[1])] == b[1][:len(a[1])];
Filefunc.LoadData(loadIndicators = False, services = False);
for fileName in os.listdir("DataSources"):
# only load data files
if not fileName.endswith(".txt"):
continue;
sources.append(fileName);
# get all the services in that file
Filefunc.LoadServices(filename = os.path.join("DataSources", fileName), loadIndicators = False, verify = True);
for service in Service.services:
# make a secret copy for ourselves
if service in services:
# add to existing service data
services[service].extend(Service.services[service].orders);
attrs[service].update(Service.services[service].attrs);
# in the correct order
services[service].sort(key=lambda order: order[0]);
else:
services[service] = Service.services[service].orders;
attrs[service] = Service.services[service].attrs;
# do some checking and fixing
for item in list(services.items()):
# remove empty services
if not item[1]:
services.pop(item[0]);
# write it all down
print("version: 6");
print("#Generated by group.py from the following base files:");
for source in sources:
print("#\t{}".format(source));
# we've already got all the services, so this one is easy
print("services:");
for service in services:
print("\t{}:".format(service));
print("\t[");
for key in attrs[service]:
print("\t{}: {},".format(key, attrs[service][key]));
print("\t]");
for stop in services[service]:
if stop[1][1] != '':
print("\t\t{}, {}, {}, {}".format(int(stop[0]) // 60, int(stop[0]) % 60, stop[1][0], stop[1][1]));
else:
print("\t\t{}, {}, {}".format(int(stop[0]) // 60, int(stop[0]) % 60, stop[1][0]));
print("\t:end");
print(":end");
# sort the services for easier searching of departures
servicesByTime = sorted(services.items(), key=lambda x: (x[1], x[0]));
print("trains:");
index = 0;
while len(servicesByTime) > 0:
# add this service to the new schedule
schedule = [servicesByTime.pop(0)];
# match the types of trains if possible
try:
stock = attrs[schedule[0][0]]["stock"];
except KeyError:
stock = False;
while True:
lastOrder = schedule[-1][1][-1];
newIndex = 0;
# find the next service to depart at the arrival station
for newService in servicesByTime:
firstOrder = newService[1][0];
# check the train types
try:
newStock = attrs[newService[0]]["stock"];
except KeyError:
newStock = stock;
# if the new service starts later than this one in the same station with the same train
if firstOrder[0] > lastOrder[0] and sameStation(firstOrder[1], lastOrder[1]) and (not stock or (newStock == stock)):
stock = newStock;
schedule.append(servicesByTime.pop(newIndex));
break;
newIndex += 1;
else:
break;
# save the data
if stock:
print("\t{} {}:".format(stock, index)); # describe the vehicle if possible
else:
print("\t{}:".format(index));
for service in schedule:
print("\t\t{}".format(service[0]));
print("\t:end");
index += 1;
print(":end");