# HG changeset patch # User Teemu Piippo # Date 1512989700 -7200 # Node ID 83cd29dee853265f5f36912aa5d217de63d02d70 # Parent 5fd79554c3aabd73f2ca5e4d494ebd2e4f15b051 stop_week diff -r 5fd79554c3aa -r 83cd29dee853 buses.py --- 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, diff -r 5fd79554c3aa -r 83cd29dee853 profiles/föli.ini --- 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 diff -r 5fd79554c3aa -r 83cd29dee853 service.py --- 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/') 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/') 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/') +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/') def trip(trip_reference): from flask import request diff -r 5fd79554c3aa -r 83cd29dee853 static/style.css --- 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%; +} diff -r 5fd79554c3aa -r 83cd29dee853 templates/cluster.html --- 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 %} @@ -27,12 +28,6 @@ { text-align: left; } - /* - body - { - background: url('/static/tausta-pysäkki.png') no-repeat center center fixed; - background-size: cover; - }*/ @@ -79,7 +74,7 @@ {{halt['stop_code']}} - {{halt['route']}} + {{route_rep(halt['route-splice'])}} {{halt['sign']}} diff -r 5fd79554c3aa -r 83cd29dee853 templates/stop.html --- 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 %} @@ -42,7 +43,7 @@ {% endif %} {{halt['time']}} - {{halt['route']}} + {{route_rep(halt['route-splice'])}} {{halt['sign']}}