--- a/service.py Thu Mar 29 23:55:36 2018 +0300 +++ b/service.py Fri Apr 13 17:32:40 2018 +0300 @@ -11,6 +11,7 @@ from busroute import reduce_schedule import buses +regions = {} suffix_regions = {'naantalin pikatie', 'helsingin valtatie', 'kansanpuisto'} # Varmista ettei järjestelmän kieliasetukset sotke muotoiluja @@ -28,7 +29,7 @@ reset_locale() -# Lataa käännökset +# Load translations class Translator: def __init__(self): self.languages = {} @@ -49,6 +50,18 @@ pass else: return name[:1].upper() + name[1:] + def load_region(self, region): + for key, value in region.items(): + if ':' in key: + name_type, language = key.split(':', 1) + if name_type.endswith('name') and language: + section = 'region_' + name_type + if section not in self.languages[language]: + self.languages[language][section] = {} + self.languages[language][section][region['name']] = value + def load_regions(self, regions): + for region in regions.values(): + self.load_region(region) tr = Translator() for file in listdir('tr'): @@ -56,7 +69,7 @@ def language_for_page(): from flask import request - if request.args.get('kääntämätön') is not None: + if request.args.get('untranslated') is not None: return None else: for language_name in tr.languages: @@ -67,22 +80,25 @@ def sign_elements(schedule_entry, format = 'medium'): from math import ceil - from busroute import greatly_simplify_name + from busroute import simplify_name trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance regions = schedule_entry['trip'].concise_schedule(schedule_entry['stop']) - if format == 'short': - regions = [greatly_simplify_name(region) for region in regions] - return reduce_schedule( - regions, - trip_length = trip_length, - format = format) + return [ + simplify_name(name) + for name in reduce_schedule( + regions, + trip_length = trip_length, + format = format + ) + ] def sign(schedule_entry, format = 'medium'): sign = sign_elements(schedule_entry, format = format) if sign: - sign_representation = ' - '.join(tr(place, 'places') for place in sign if place not in suffix_regions) - sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) - return sign_representation + # sign_representation = ' - '.join(tr(place, 'region_short_name') for place in sign if place not in suffix_regions) + # sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) + # return sign_representation + return ' - '.join(tr(place, 'region_short_name') for place in sign) else: return schedule_entry['trip'].schedule[-1].stop.name @@ -92,8 +108,8 @@ sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, format = format) if sign: return { - 'destination': tr(sign[-1], 'places'), - 'via': [tr(place, 'places') for place in sign[:-1]], + 'destination': tr(sign[-1], 'region_short_name'), + 'via': [tr(place, 'region_short_name') for place in sign[:-1]], } else: return { @@ -171,9 +187,9 @@ return render_template( 'stop.html', schedule = schedule, - name = bus_stop.code + ' ' + tr(bus_stop.name, 'bus-stops', 'places'), + name = bus_stop.code + ' ' + tr(bus_stop.name, 'bus-stops'), link_to_map = bus_stop.location.link_to_map, - region = hasattr(bus_stop, 'region') and bus_stop.region or None, + region = hasattr(bus_stop, 'region_name') and bus_stop.region or None, location = bus_stop.location, cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, tr = tr, @@ -230,7 +246,7 @@ def describe(bus_stop): schedule = [] from collections import defaultdict, Counter - from busroute import greatly_simplify_name + from busroute import simplify_name destinations_per_route = defaultdict(Counter) def route_key(route_ref): try: @@ -238,7 +254,7 @@ except ValueError: return () def filter_names(names): - if len(names) == 1 and names[0] == (bus_stop.region and greatly_simplify_name(bus_stop.region)): + if len(names) == 1 and names[0] == (bus_stop.region and simplify_name(bus_stop.region)): return type(names)() else: return names @@ -285,7 +301,7 @@ ): result.append(( list(condense_route_list(sorted(routes, key = route_key))), - ' - '.join(tr(region, 'regions') for region in regions) + ' - '.join(tr(region, 'region_short_name') for region in regions) )) return { 'night-routes': night_routes, @@ -613,10 +629,10 @@ if profile['regions']['use-regions']: if halt.stop.region != region and not (region and not halt.stop.region): if len(schedule) and not schedule[-1]['name']: - schedule[-1]['name'] = tr(halt.stop.region or '', 'places') + schedule[-1]['name'] = tr(halt.stop.region or '', 'region_name') else: schedule.append({ - 'name': tr(halt.stop.region or '', 'places'), + 'name': tr(halt.stop.region or '', 'region_name'), 'time': formatted_time, 'stops': [], 'index': len(schedule), @@ -624,7 +640,7 @@ region = halt.stop.region else: schedule.append({ - 'name': tr(halt.stop.name or '', 'bus-stops', 'places'), + 'name': tr(halt.stop.name or '', 'bus-stops'), 'time': formatted_time, 'stops': [], 'index': len(schedule), @@ -633,18 +649,18 @@ 'time': formatted_time, 'id': halt.stop.reference, 'code': halt.stop.code, - 'name': tr(halt.stop.name, 'bus-stops', 'places'), + 'name': tr(halt.stop.name, 'bus-stops'), }) sign = trip.concise_schedule() try: - sign = [simplify_name(sign[0]), simplify_name(sign[-1])] + sign = [sign[0], sign[-1]] except IndexError: sign = [trip.schedule[0].stop.name, trip.schedule[-1].stop.name] return render_template('trip.html', schedule = schedule, trip_reference = trip_reference, route = trip.route.reference, - description = ' - '.join(tr(place, 'places') for place in sign), + description = ' - '.join(tr(place, 'region_name') for place in sign), night = is_night_time(datetime.combine(today(), time()) + trip.schedule[-1].arrival_time), tr = tr, length = trip.length / 1000, @@ -700,7 +716,13 @@ args = parser.parse_args() profile.read(args.profile_path) -buses.load_buses(args.gtfs_zip_path) +if profile['regions']['use-regions']: + from regions import parse_regions + regions = parse_regions(profile['regions']['osm-path']) + tr.load_regions(regions) + import busroute + busroute.regions = regions +buses.load_buses(args.gtfs_zip_path, regions = regions) if __name__ == '__main__': app.run(debug = args.debug, port = args.port)