service.py

changeset 71
d2e19670b772
parent 68
382dd85b83a2
child 72
65408ed066b3
equal deleted inserted replaced
70:c2fc50748efd 71:d2e19670b772
89 89
90 90
91 def imminent(schedule_entry): 91 def imminent(schedule_entry):
92 return (schedule_entry['time'] - now()) <= timedelta(minutes = 3) 92 return (schedule_entry['time'] - now()) <= timedelta(minutes = 3)
93 93
94 def first_halt_in_trip_in_place(trip, place):
95 for halt in trip.schedule:
96 if halt.stop.region == place:
97 return halt
98 else:
99 return None
100
101 place_abbreviations = ConfigParser()
102 place_abbreviations.read('abbreviations.ini')
103
104 def place_abbreviation(place):
105 try:
106 return place_abbreviations['abbreviations'][place]
107 except KeyError:
108 return place
109
110 def trip_abbreviation(trip):
111 entries = [trip.from_place]
112 old_places = None
113 starting_halt = None
114 while True:
115 remaining_length = trip.length
116 if starting_halt:
117 remaining_length -= starting_halt.traveled_distance
118 places = reduce_schedule(trip.concise_schedule(starting_stop = starting_halt), trip_length = remaining_length, long = False)
119 new_places = set(places) - set(entries)
120 if not new_places or places == old_places:
121 break
122 for place in places:
123 if place in new_places:
124 starting_halt = first_halt_in_trip_in_place(trip, place)
125 entries += [place]
126 break
127 old_places = places
128 if trip.to_place not in entries:
129 entries += [trip.to_place]
130 return trip.route.reference + ':' + '-'.join(map(place_abbreviation, entries))
94 131
95 @app.route('/stop/<reference>') 132 @app.route('/stop/<reference>')
96 def bus_stop_schedule(reference): 133 def bus_stop_schedule(reference):
97 from buses import bus_stops 134 from buses import bus_stops
98 schedule = [] 135 schedule = []
117 region = bus_stop.region, 154 region = bus_stop.region,
118 location = bus_stop.location, 155 location = bus_stop.location,
119 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, 156 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None,
120 tr = tr, 157 tr = tr,
121 ) 158 )
159
160 @app.route('/api/describe_destination/<trip_reference>/<stop_reference>/<int:index>')
161 def describe_destination(trip_reference, stop_reference, index):
162 from buses import bus_stops, all_trips
163 from busroute import simplify_name
164 from flask import jsonify
165 try:
166 trip = all_trips[trip_reference]
167 bus_stop = bus_stops[stop]
168 schedule_entry = [schedule_entry for schedule_entry in trip.schedule if schedule_entry.stop.reference == stop_reference][index]
169 except KeyError:
170 abort(404)
171 except ValueError:
172 abort(404)
173 return jsonify(long_form_sign({'trip': trip, 'stop': schedule_entry}))
174
175 @app.route('/api/trip_abbreviation/<block_id>/<int:arrival_time_offset>')
176 def api_trip_abbreviation(block_id, arrival_time_offset):
177 from buses import trips_by_vehicle_info
178 from flask import jsonify
179 from datetime import timedelta
180 try:
181 trip = trips_by_vehicle_info[block_id, timedelta(seconds = arrival_time_offset)]
182 except KeyError:
183 abort(404)
184 return jsonify({
185 'abbreviation': trip_abbreviation(trip),
186 })
122 187
123 @app.route('/stop_display/<reference>') 188 @app.route('/stop_display/<reference>')
124 def stop_display(reference): 189 def stop_display(reference):
125 from buses import bus_stops 190 from buses import bus_stops
126 schedule = [] 191 schedule = []
284 schedule = [] 349 schedule = []
285 region = '' 350 region = ''
286 for halt in trip.schedule: 351 for halt in trip.schedule:
287 stop_time = datetime.combine(today(), time()) + halt.arrival_time 352 stop_time = datetime.combine(today(), time()) + halt.arrival_time
288 formatted_time = time_representation(stop_time) 353 formatted_time = time_representation(stop_time)
289 if halt.stop.region != region and not (region and not halt.stop.region): 354 if profile['regions']['use-regions']:
290 if len(schedule) and not schedule[-1]['name']: 355 if halt.stop.region != region and not (region and not halt.stop.region):
291 schedule[-1]['name'] = tr(halt.stop.region or '', 'paikat') 356 if len(schedule) and not schedule[-1]['name']:
292 else: 357 schedule[-1]['name'] = tr(halt.stop.region or '', 'paikat')
293 schedule.append({ 358 else:
294 'name': tr(halt.stop.region or '', 'paikat'), 359 schedule.append({
295 'time': formatted_time, 360 'name': tr(halt.stop.region or '', 'paikat'),
296 'stops': [], 361 'time': formatted_time,
297 'index': len(schedule), 362 'stops': [],
298 }) 363 'index': len(schedule),
299 region = halt.stop.region 364 })
365 region = halt.stop.region
366 else:
367 schedule.append({
368 'name': tr(halt.stop.name or '', 'bus-stops'),
369 'time': formatted_time,
370 'stops': [],
371 'index': len(schedule),
372 })
300 schedule[-1]['stops'].append({ 373 schedule[-1]['stops'].append({
301 'time': formatted_time, 374 'time': formatted_time,
302 'id': halt.stop.reference, 375 'id': halt.stop.reference,
303 'code': halt.stop.code, 376 'code': halt.stop.code,
304 'name': tr(halt.stop.name, 'bus-stops'), 377 'name': tr(halt.stop.name, 'bus-stops'),
365 if __name__ == '__main__': 438 if __name__ == '__main__':
366 parser.add_argument('-p', '--port', type = int, default = 5000) 439 parser.add_argument('-p', '--port', type = int, default = 5000)
367 parser.add_argument('-d', '--debug', action = 'store_true') 440 parser.add_argument('-d', '--debug', action = 'store_true')
368 441
369 args = parser.parse_args() 442 args = parser.parse_args()
370 profile = ConfigParser()
371 profile.read(args.profile_path) 443 profile.read(args.profile_path)
372 buses.load_buses(args.gtfs_zip_path, profile = profile) 444 buses.load_buses(args.gtfs_zip_path)
373 445
374 if __name__ == '__main__': 446 if __name__ == '__main__':
375 app.run(debug = args.debug, port = args.port) 447 app.run(debug = args.debug, port = args.port)

mercurial