buses.py

changeset 93
53ee9e945673
parent 90
36efdea68d03
child 94
e27c18f080d1
--- a/buses.py	Tue Apr 24 23:21:34 2018 +0300
+++ b/buses.py	Thu Apr 26 16:12:23 2018 +0300
@@ -185,7 +185,7 @@
 clusters_by_name = {}
 services_for_day = {}
 
-def load_buses(gtfs_zip_path, regions):
+def load_buses(gtfs_zip_path):
 	global viimeinen_käyttöpäivä
 	from zipfile import ZipFile
 	with ZipFile(gtfs_zip_path) as gtfs_zip:
@@ -196,6 +196,22 @@
 				routes[route.reference] = route
 				routes_per_id[route.id] = route
 		print('%d routes' % len(routes), file = stderr)
+		# Add services
+		import re
+		service_patterns = {}
+		if 'service-patterns' in profile:
+			for service_type, regexps in profile['service-patterns'].items():
+				service_patterns[service_type] = {re.compile(regexp) for regexp in regexps.split('@')}
+		if 'services' in profile and profile['services'].get('default-service'):
+			print('Tagging services...', end = '')
+			for route in routes.values():
+				for service_type, regexps in service_patterns.items():
+					if any(regexp.match(route.reference) for regexp in regexps):
+						route.service = service_type
+						break
+				else:
+					route.service = profile['services']['default-service']
+			print('')
 		print('Loading trips... ', file = stderr, end = '', flush = True)
 		shape_distances = {}
 		try:
@@ -231,8 +247,7 @@
 			hour, minute, second = map(int, teksti.split(':'))
 			return timedelta(hours = hour, minutes = minute, seconds = second)
 
-		print('Ladataan päiväykset... ', file = stderr, flush = True)
-
+		print('Loading dates... ', file = stderr, flush = True)
 		viimeinen_käyttöpäivä = date.today()
 
 		def date_range(start_date, end_date, *, include_end = False):
@@ -504,13 +519,11 @@
 		trips_by_vehicle_info = {}
 		for trip in all_trips.values():
 			trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip
-		if 'services' in profile and profile['services'].get('default-service'):
-			for route in routes.values():
-				if not route.service:
-					route.service = profile['services']['default-service']
-				for trip in route.trips:
-					for halt in trip.schedule:
-						halt.stop.services.add(route.service)
+		# Add services to all bus stops
+		for route in routes.values():
+			for trip in route.trips:
+				for halt in trip.schedule:
+					halt.stop.services.add(route.service)
 
 if __name__ == '__main__':
 	profile.read('profiles/föli.ini')

mercurial