stop_week

Mon, 11 Dec 2017 12:55:00 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 11 Dec 2017 12:55:00 +0200
changeset 77
83cd29dee853
parent 76
5fd79554c3aa
child 78
ead971f9569c

stop_week

buses.py file | annotate | diff | comparison | revisions
profiles/föli.ini file | annotate | diff | comparison | revisions
service.py file | annotate | diff | comparison | revisions
static/style.css file | annotate | diff | comparison | revisions
templates/cluster.html file | annotate | diff | comparison | revisions
templates/stop.html file | annotate | diff | comparison | revisions
--- a/buses.py	Thu Dec 07 16:55:44 2017 +0200
+++ b/buses.py	Mon Dec 11 12:55:00 2017 +0200
@@ -118,6 +118,8 @@
 					if stop_time + timedelta(minutes = 1) >= now():
 						# lisää halt listaan.
 						result.append({
+							'date': date,
+							'offset': stop.departure_time,
 							'time': stop_time,
 							'trip': trip,
 							'stop': stop,
--- a/profiles/föli.ini	Thu Dec 07 16:55:44 2017 +0200
+++ b/profiles/föli.ini	Mon Dec 11 12:55:00 2017 +0200
@@ -105,7 +105,7 @@
 vähä-heikkilä = 25
 särkilahti = 25
 urusvuori = 25
-port arthur = 25
+port arthur = 50
 brinkhall = 100
 myllykylä = 100
 harjattula = 200
--- 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
--- a/static/style.css	Thu Dec 07 16:55:44 2017 +0200
+++ b/static/style.css	Mon Dec 11 12:55:00 2017 +0200
@@ -237,3 +237,8 @@
 	background: #fff3b3;
 	text-decoration: none;
 }
+
+.route-suffix
+{
+	font-size: 75%;
+}
--- a/templates/cluster.html	Thu Dec 07 16:55:44 2017 +0200
+++ b/templates/cluster.html	Mon Dec 11 12:55:00 2017 +0200
@@ -1,3 +1,4 @@
+{% from "macros.html" import route_rep %}
 <html>
 <head>
 	<link rel="icon" type="image/png" href="../static/favicon.png" />
@@ -27,12 +28,6 @@
 	{
 		text-align: left;
 	}
-	/*
-	body
-	{
-		background: url('/static/tausta-pysäkki.png') no-repeat center center fixed;
-		background-size: cover;
-	}*/
 	</style>
 </head>
 <body>
@@ -79,7 +74,7 @@
 				<a href="../stop/{{halt['stop_id']}}"><img src="../static/pysäkki.png" height="24" /> {{halt['stop_code']}}</a>
 			</td>
 			<td class='sarake-linja linja'>
-				<a href="../trip/{{halt['trip']}}">{{halt['route']}}</a>
+				<a href="../trip/{{halt['trip']}}">{{route_rep(halt['route-splice'])}}</a>
 			</td>
 			<td class='sarake-määränpää' colspan='2'>
 				<a href="../trip/{{halt['trip']}}">{{halt['sign']}}</a>
--- a/templates/stop.html	Thu Dec 07 16:55:44 2017 +0200
+++ b/templates/stop.html	Mon Dec 11 12:55:00 2017 +0200
@@ -1,3 +1,4 @@
+{% from "macros.html" import route_rep %}
 <html>
 <head>
 	<link rel="icon" type="image/png" href="../static/favicon.png" />
@@ -42,7 +43,7 @@
 				{% endif %}
 				{{halt['time']}}</td>
 			<td class='sarake-linja linja'>
-				<a href="../trip/{{halt['trip']}}">{{halt['route']}}</a>
+				<a href="../trip/{{halt['trip']}}">{{route_rep(halt['route-splice'])}}</a>
 			</td>
 			<td class='sarake-määränpää' colspan='2'>
 				<a href="../trip/{{halt['trip']}}">{{halt['sign']}}</a>

mercurial