service.py

changeset 31
60045b362d71
parent 30
a5bfd99bc2a3
child 38
d0ae1996abdd
equal deleted inserted replaced
30:a5bfd99bc2a3 31:60045b362d71
33 def load_language(self, file_path): 33 def load_language(self, file_path):
34 language_name = path.splitext(path.basename(file_path))[0] 34 language_name = path.splitext(path.basename(file_path))[0]
35 ini = ConfigParser() 35 ini = ConfigParser()
36 ini.read(path.join(file_path)) 36 ini.read(path.join(file_path))
37 self.languages[language_name] = ini 37 self.languages[language_name] = ini
38 def __call__(self, name, section, language = None): 38 def __call__(self, name, *sections, language = None):
39 language = language or language_for_page() 39 language = language or language_for_page()
40 try: 40 for section in sections:
41 return self.languages[language][section][name] 41 try:
42 except KeyError: 42 return self.languages[language][section][name]
43 except KeyError:
44 pass
45 else:
43 return name 46 return name
44 47
45 tr = Translator() 48 tr = Translator()
46 for file in listdir('tr'): 49 for file in listdir('tr'):
47 tr.load_language(path.join('tr', file)) 50 tr.load_language(path.join('tr', file))
77 schedule = [] 80 schedule = []
78 try: 81 try:
79 bus_stop = bus_stops[reference] 82 bus_stop = bus_stops[reference]
80 except KeyError: 83 except KeyError:
81 abort(404) 84 abort(404)
82 for schedule_entry in bus_stop.schedule(100): 85 for schedule_entry in bus_stop.schedule(max_amount = 100, arrivals = True):
83 schedule.append({ 86 schedule.append({
84 'time': time_representation(schedule_entry['time']), 87 'time': time_representation(schedule_entry['time']),
85 'route': schedule_entry['trip'].route.reference, 88 'route': schedule_entry['trip'].route.reference,
86 'sign': sign(schedule_entry), 89 'sign': sign(schedule_entry),
87 'trip': schedule_entry['stop'].trip.name, 90 'trip': schedule_entry['stop'].trip.name,
106 return '%dm' % round(time_difference.seconds / 60) 109 return '%dm' % round(time_difference.seconds / 60)
107 elif time.date() == today(): 110 elif time.date() == today():
108 return '%d:%02d' % (time.hour, time.minute) 111 return '%d:%02d' % (time.hour, time.minute)
109 elif time_difference < timedelta(7): 112 elif time_difference < timedelta(7):
110 with activate_locale(): 113 with activate_locale():
111 return time.strftime('%-a %H:%M') 114 return time.strftime('%-a %H:%M').replace(' ', '\xa0')
112 else: 115 else:
113 with activate_locale(): 116 with activate_locale():
114 return time.strftime('%-d.%-m. %H:%M') 117 return time.strftime('%-d.%-m. %H:%M').replace(' ', '\xa0')
115 118
116 @app.route('/pysäkkiryhmä/<cluster_name>') 119 @app.route('/pysäkkiryhmä/<cluster_name>')
117 def cluster_schedule(cluster_name): 120 def cluster_schedule(cluster_name):
118 from buses import bus_stops, clusters_by_name 121 from buses import bus_stops, clusters_by_name
119 schedule = [] 122 schedule = []
120 try: 123 try:
121 cluster = clusters_by_name[cluster_name] 124 cluster = clusters_by_name[cluster_name]
122 except KeyError: 125 except KeyError:
123 abort(404) 126 abort(404)
124 for schedule_entry in cluster.schedule(100): 127 for schedule_entry in cluster.schedule(max_amount = 100):
125 schedule.append({ 128 schedule.append({
126 'time': time_representation(schedule_entry['time']), 129 'time': time_representation(schedule_entry['time']),
127 'route': schedule_entry['trip'].route.reference, 130 'route': schedule_entry['trip'].route.reference,
128 'sign': sign(schedule_entry), 131 'sign': sign(schedule_entry),
129 'trip': schedule_entry['stop'].trip.name, 132 'trip': schedule_entry['stop'].trip.name,
130 'night': is_night_time(schedule_entry['time']), 133 'night': is_night_time(schedule_entry['time']),
131 'stop_id': schedule_entry['stop'].stop.reference, 134 'stop_id': schedule_entry['stop'].stop.reference,
132 'stop_code': schedule_entry['stop'].stop.code, 135 'stop_code': schedule_entry['stop'].stop.code,
133 'stop_name': tr(schedule_entry['stop'].stop.name, 'bus_stops'), 136 'stop_name': tr(schedule_entry['stop'].stop.name, 'pysäkit'),
134 }) 137 })
135 stops_in_cluster = sorted( 138 stops_in_cluster = sorted(
136 ({ 139 ({
137 'id': stop.reference, 140 'id': stop.reference,
138 'code': stop.code, 141 'code': stop.code,
139 'name': tr(stop.name, 'bus_stops'), 142 'name': tr(stop.name, 'pysäkit'),
140 } for stop in cluster.stops), 143 } for stop in cluster.stops),
141 key = lambda stop: (len(stop['id']), stop['id']) 144 key = lambda stop: (len(stop['id']), stop['id'])
142 ) 145 )
143 return render_template( 146 return render_template(
144 'cluster.html', 147 'cluster.html',
145 schedule = schedule, 148 schedule = schedule,
146 name = cluster.name, 149 name = tr(cluster.name, 'paikat', 'pysäkkiryhmät', 'pysäkit'),
147 link_to_map = cluster.center.link_to_map, 150 link_to_map = cluster.center.link_to_map,
148 location = cluster.center, 151 location = cluster.center,
149 stops_in_cluster = stops_in_cluster, 152 stops_in_cluster = stops_in_cluster,
150 amount_of_stops_in_cluster = len(stops_in_cluster), 153 amount_of_stops_in_cluster = len(stops_in_cluster),
151 tr = tr, 154 tr = tr,
153 156
154 @app.route('/ajovuoro/<trip_reference>') 157 @app.route('/ajovuoro/<trip_reference>')
155 def trip(trip_reference): 158 def trip(trip_reference):
156 from flask import request 159 from flask import request
157 from buses import all_trips 160 from buses import all_trips
161 from busroute import simplify_name
158 try: 162 try:
159 trip = all_trips[trip_reference] 163 trip = all_trips[trip_reference]
160 except KeyError: 164 except KeyError:
161 abort(404) 165 abort(404)
162 schedule = [] 166 schedule = []
163 concise_schedule = [] 167 region = ''
164 used_areas = set()
165 for halt in trip.schedule: 168 for halt in trip.schedule:
166 stop_time = datetime.combine(today(), time()) + halt.arrival_time 169 stop_time = datetime.combine(today(), time()) + halt.arrival_time
167 formatted_time = time_representation(stop_time) 170 formatted_time = time_representation(stop_time)
168 schedule.append({ 171 if halt.stop.region != region and not (region and not halt.stop.region):
172 if len(schedule) and not schedule[-1]['name']:
173 schedule[-1]['name'] = tr(halt.stop.region or '', 'paikat')
174 else:
175 schedule.append({
176 'name': tr(halt.stop.region or '', 'paikat'),
177 'time': formatted_time,
178 'stops': [],
179 'index': len(schedule),
180 })
181 region = halt.stop.region
182 schedule[-1]['stops'].append({
169 'time': formatted_time, 183 'time': formatted_time,
170 'id': halt.stop.reference, 184 'id': halt.stop.reference,
171 'code': halt.stop.code, 185 'code': halt.stop.code,
172 'region': tr(halt.stop.region or '', 'paikat'),
173 'name': tr(halt.stop.name, 'bus_stops'), 186 'name': tr(halt.stop.name, 'bus_stops'),
174 }) 187 })
175 region = halt.stop.region 188 sign = trip.concise_schedule()
176 if region: 189 try:
177 if region not in used_areas: 190 sign = [simplify_name(sign[0]), simplify_name(sign[-1])]
178 concise_schedule.append({
179 'time': formatted_time,
180 'region': region or '',
181 })
182 used_areas.add(region)
183 sign = reduce_schedule([k['region'] for k in concise_schedule], whole = True, trip_length = trip.length)
184 try:
185 sign = [sign[0], sign[-1]]
186 except IndexError: 191 except IndexError:
187 sign = [trip.schedule[0].stop.name, trip.schedule[-1].stop.name] 192 sign = [trip.schedule[0].stop.name, trip.schedule[-1].stop.name]
188 for entry in concise_schedule:
189 entry['region'] = tr(entry['region'], 'paikat')
190 return render_template('trip.html', 193 return render_template('trip.html',
191 schedule = schedule, 194 schedule = schedule,
192 concise_schedule = concise_schedule,
193 trip_reference = trip_reference, 195 trip_reference = trip_reference,
194 route = trip.route.reference, 196 route = trip.route.reference,
195 description = ' - '.join(tr(place, 'paikat') for place in sign), 197 description = ' - '.join(tr(place, 'paikat') for place in sign),
196 night = is_night_time(datetime.combine(today(), time()) + trip.schedule[-1].arrival_time), 198 night = is_night_time(datetime.combine(today(), time()) + trip.schedule[-1].arrival_time),
197 tr = tr, 199 tr = tr,

mercurial