service.py

changeset 77
83cd29dee853
parent 76
5fd79554c3aa
child 79
ba854da8c424
--- a/service.py	Thu Dec 07 16:55:44 2017 +0200
+++ b/service.py	Mon Dec 11 12:55:00 2017 +0200
@@ -138,6 +138,12 @@
 		entries += [trip.to_place]
 	return trip.route.reference + ':' + '-'.join(map(place_abbreviation, entries))
 
+def split_route_ref(route_ref):
+	try:
+		return list(parse_route_ref(route_ref))
+	except ValueError:
+		return ['', route_ref, '']
+
 @app.route('/stop/<reference>')
 def bus_stop_schedule(reference):
 	from buses import bus_stops
@@ -147,9 +153,11 @@
 	except KeyError:
 		abort(404)
 	for schedule_entry in bus_stop.schedule(max_amount = 100, arrivals = True):
+		route_ref = schedule_entry['trip'].route.reference
 		schedule.append({
 			'time': time_representation(schedule_entry['time']),
-			'route': schedule_entry['trip'].route.reference,
+			'route': route_ref,
+			'route-splice': split_route_ref(route_ref),
 			'sign': sign(schedule_entry),
 			'trip': schedule_entry['stop'].trip.name,
 			'night': is_night_time(schedule_entry['time']),
@@ -178,6 +186,14 @@
 	else:
 		return (route,)
 
+def parse_route_ref(route_ref):
+	from re import search
+	match = search(r'^([^0-9]*)([0-9]+)(.*)$', route_ref)
+	try:
+		return match.group(1), int(match.group(2)), match.group(3)
+	except AttributeError:
+		raise ValueError(route_ref)
+
 @app.route('/stop_description/<reference>')
 def bus_stop_description(reference):
 	from buses import bus_stops
@@ -190,11 +206,9 @@
 	from busroute import simplify_name
 	destinations_per_route = defaultdict(Counter)
 	def route_key(route_ref):
-		from re import search
-		match = search(r'^([^0-9]*)([0-9]+)(.*)$', route_ref).groups()
-		if match:
-			return match[0], int(match[1]), match[2]
-		else:
+		try:
+			return parse_route_ref(route_ref)
+		except ValueError:
 			return ()
 	def filter_names(names):
 		if len(names) == 1 and names[0] == (bus_stop.region and simplify_name(bus_stop.region)):
@@ -396,6 +410,7 @@
 		schedule.append({
 			'time': time_representation(schedule_entry['time']),
 			'route': schedule_entry['trip'].route.reference,
+			'route-splice': split_route_ref(schedule_entry['trip'].route.reference),
 			'sign': sign(schedule_entry),
 			'trip': schedule_entry['stop'].trip.name,
 			'night': is_night_time(schedule_entry['time']),
@@ -423,6 +438,70 @@
 		tr = tr,
 	)
 
+
+def hour_key(hour):
+	return (hour - 5) % 24
+
+def day_class(weekday):
+	if weekday < 5:
+		return 'working-day'
+	elif weekday == 5:
+		return 'saturday'
+	else:
+		assert weekday == 6
+		return 'sunday'
+
+@app.route('/stop_week/<stop_reference>')
+def stop_week(stop_reference):
+	from buses import bus_stops
+	from flask import request
+	if 'routes' in request.args:
+		filtered_routes = set(request.args['routes'].split(';'))
+		route_filter = lambda route: route in filtered_routes
+	else:
+		route_filter = lambda route: True
+	if 'dest' in request.args:
+		dests = {bus_stops.get(dest, None) for dest in request.args['dest'].split(';')}
+		dests.discard(None)
+		dest_filter = lambda trip: any(trip.contains_stop(dest) for dest in dests)
+	else:
+		dest_filter = lambda trip: True
+	schedule = []
+	try:
+		bus_stop = bus_stops[stop_reference]
+	except KeyError:
+		abort(404)
+	week_model = {}
+	for schedule_entry in week_schedule(bus_stop, arrivals = True):
+		route_ref = schedule_entry['trip'].route.reference
+		if route_filter(route_ref) and dest_filter(schedule_entry['trip']):
+			time = schedule_entry['time']
+			date = schedule_entry['date']
+			if date not in week_model:
+				week_model[date] = {}
+			day_model = week_model[date]
+			if time.hour not in day_model:
+				day_model[time.hour] = []
+			hour_model = day_model[time.hour]
+			hour_model.append({
+				'route': route_ref,
+				'route-splice': split_route_ref(route_ref),
+				'trip': schedule_entry['stop'].trip.name,
+				'night': is_night_time(schedule_entry['time']),
+				'minute': time.minute,
+			})
+	week_model = [{'day': day, 'schedule': schedule, 'day-class': day_class(day.weekday())} for day, schedule in week_model.items()]
+	week_model = sorted(week_model, key = lambda day: day['day'])
+	from pprint import pprint
+	pprint(week_model)
+	return render_template(
+		'stop_week.html',
+		ref = bus_stop.code,
+		name = tr(bus_stop.name, 'bus-stops'),
+		tr = tr,
+		week = week_model,
+	)
+
 @app.route('/trip/<trip_reference>')
 def trip(trip_reference):
 	from flask import request

mercurial