service.py

changeset 90
36efdea68d03
parent 88
3b86597c5a88
child 91
209f5930d038
equal deleted inserted replaced
89:438d77bca50e 90:36efdea68d03
7 7
8 app = Flask(__name__) 8 app = Flask(__name__)
9 9
10 from misc import * 10 from misc import *
11 from busroute import reduce_schedule 11 from busroute import reduce_schedule
12 from busroute import simplify_name
12 import buses 13 import buses
13 14
14 regions = {} 15 regions = {}
15 suffix_regions = {'naantalin pikatie', 'helsingin valtatie', 'kansanpuisto'} 16 suffix_regions = {'naantalin pikatie', 'helsingin valtatie', 'kansanpuisto'}
16 17
52 return name[:1].upper() + name[1:] 53 return name[:1].upper() + name[1:]
53 def load_region(self, region): 54 def load_region(self, region):
54 for key, value in region.items(): 55 for key, value in region.items():
55 if ':' in key: 56 if ':' in key:
56 name_type, language = key.split(':', 1) 57 name_type, language = key.split(':', 1)
57 if name_type.endswith('name') and language: 58 if (name_type.endswith('name') or name_type == 'genitive') and language:
58 section = 'region_' + name_type 59 section = 'region_' + name_type
59 if section not in self.languages[language]: 60 if section not in self.languages[language]:
60 self.languages[language][section] = {} 61 self.languages[language][section] = {}
61 self.languages[language][section][region['name']] = value 62 self.languages[language][section][region['name']] = value
62 def load_regions(self, regions): 63 def load_regions(self, regions):
82 from math import ceil 83 from math import ceil
83 from busroute import simplify_name 84 from busroute import simplify_name
84 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance 85 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance
85 regions = schedule_entry['trip'].concise_schedule(schedule_entry['stop']) 86 regions = schedule_entry['trip'].concise_schedule(schedule_entry['stop'])
86 return [ 87 return [
87 simplify_name(name) 88 name
88 for name in reduce_schedule( 89 for name in reduce_schedule(
89 regions, 90 regions,
90 trip_length = trip_length, 91 trip_length = trip_length,
91 format = format 92 format = format
92 ) 93 )
93 ] 94 ]
94 95
96 def genitive(name):
97 from busroute import regions
98 region = regions.get(name)
99 if region:
100 return region.get('genitive:fi', simplify_name(name) + 'n')
101 else:
102 return simplify_name(name) + 'n'
103
104 def via_fi(via):
105 if len(via) > 1:
106 return ', '.join(via[:-1]) + ' ja ' + via[-1]
107 else:
108 return via[0]
109
95 def sign(schedule_entry, format = 'medium'): 110 def sign(schedule_entry, format = 'medium'):
96 sign = sign_elements(schedule_entry, format = format) 111 sign = sign_elements(schedule_entry, format = format)
97 if sign: 112 if sign:
113 #if language_for_page() == 'fi':
114 # if len(sign) > 1:
115 # return simplify_name(sign[-1]) + ' ' + via_fi([genitive(place) for place in sign[:-1]]) + ' kautta'
116 # else:
117 # return simplify_name(sign[0])
98 # sign_representation = ' - '.join(tr(place, 'region_short_name') for place in sign if place not in suffix_regions) 118 # sign_representation = ' - '.join(tr(place, 'region_short_name') for place in sign if place not in suffix_regions)
99 # sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) 119 # sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions)
100 # return sign_representation 120 # return sign_representation
101 return ' - '.join(tr(place, 'region_short_name') for place in sign) 121 return ' - '.join(tr(simplify_name(place), 'region_short_name') for place in sign)
102 else: 122 else:
103 return schedule_entry['trip'].schedule[-1].stop.name 123 return schedule_entry['trip'].schedule[-1].stop.name
104 124
105 def long_form_sign(schedule_entry, format = 'long'): 125 def long_form_sign(schedule_entry, format = 'long'):
106 from math import ceil 126 from math import ceil
191 link_to_map = bus_stop.location.link_to_map, 211 link_to_map = bus_stop.location.link_to_map,
192 region = hasattr(bus_stop, 'region_name') and bus_stop.region or None, 212 region = hasattr(bus_stop, 'region_name') and bus_stop.region or None,
193 location = bus_stop.location, 213 location = bus_stop.location,
194 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, 214 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None,
195 tr = tr, 215 tr = tr,
216 typename = bus_stop.typename,
196 ) 217 )
197 218
198 def week_schedule(bus_stop, **kwargs): 219 def week_schedule(bus_stop, **kwargs):
199 for i in range(-1, 7): 220 for i in range(-1, 7):
200 yield from bus_stop.schedule_for_day(today() + timedelta(i), **kwargs) 221 try:
222 yield from bus_stop.schedule_for_day(today() + timedelta(i), **kwargs)
223 except ValueError:
224 # went outside bounds
225 return
201 226
202 def route_key(route): 227 def route_key(route):
203 match = re.search(r'^([a-zA-Z]*)(\d+)(.*)$', route) 228 match = re.search(r'^([a-zA-Z]*)(\d+)(.*)$', route)
204 if match: 229 if match:
205 groups = match.groups() 230 groups = match.groups()
498 'night': is_night_time(schedule_entry['time']), 523 'night': is_night_time(schedule_entry['time']),
499 'stop_id': schedule_entry['stop'].stop.reference, 524 'stop_id': schedule_entry['stop'].stop.reference,
500 'stop_code': schedule_entry['stop'].stop.code, 525 'stop_code': schedule_entry['stop'].stop.code,
501 'stop_name': tr(schedule_entry['stop'].stop.name, 'bus-stops', 'places'), 526 'stop_name': tr(schedule_entry['stop'].stop.name, 'bus-stops', 'places'),
502 'imminent': imminent(schedule_entry), 527 'imminent': imminent(schedule_entry),
528 'typename': schedule_entry['stop'].stop.typename,
503 }) 529 })
504 stops_in_cluster = sorted( 530 stops_in_cluster = sorted(
505 ({ 531 ({
506 'id': stop.reference, 532 'id': stop.reference,
507 'code': stop.code, 533 'code': stop.code,
608 ref = bus_stop.code, 634 ref = bus_stop.code,
609 name = tr(bus_stop.name, 'bus-stops'), 635 name = tr(bus_stop.name, 'bus-stops'),
610 tr = tr, 636 tr = tr,
611 week = week_model, 637 week = week_model,
612 description = describe(bus_stop), 638 description = describe(bus_stop),
639 typename = bus_stop.typename,
613 ) 640 )
614 641
615 @app.route('/trip/<trip_reference>') 642 @app.route('/trip/<trip_reference>')
616 def trip(trip_reference): 643 def trip(trip_reference):
617 from flask import request 644 from flask import request
648 schedule[-1]['stops'].append({ 675 schedule[-1]['stops'].append({
649 'time': formatted_time, 676 'time': formatted_time,
650 'id': halt.stop.reference, 677 'id': halt.stop.reference,
651 'code': halt.stop.code, 678 'code': halt.stop.code,
652 'name': tr(halt.stop.name, 'bus-stops'), 679 'name': tr(halt.stop.name, 'bus-stops'),
680 'typename': halt.stop.typename,
653 }) 681 })
654 sign = trip.concise_schedule() 682 sign = trip.concise_schedule()
655 try: 683 try:
656 sign = [sign[0], sign[-1]] 684 sign = [sign[0], sign[-1]]
657 except IndexError: 685 except IndexError:

mercurial