Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nlehuby committed Dec 30, 2019
1 parent ee4cb1e commit 224f4d4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 44 deletions.
9 changes: 4 additions & 5 deletions osm2gtfs/creators/ci_abidjan/agency_creator_ci_abidjan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

from osm2gtfs.creators.agency_creator import AgencyCreator


class AgencyCreatorCiAbidjan(AgencyCreator):
# In the case of Abidjan, the agency that is defined in the config.json
# file is used as the default agency.
# Any other agencies are input from the OSM data in trip_creator_ci_abidjan.py
# file is used as the default agency.
# Any other agencies are input from the OSM data in trip_creator_ci_abidjan.py
# based on the "operator" and "operator:website" tags

def add_agency_to_feed(self, feed):
feed.SetDefaultAgency(self.prepare_agency())


101 changes: 64 additions & 37 deletions osm2gtfs/creators/ci_abidjan/trips_creator_ci_abidjan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@

class TripsCreatorCiAbidjan(TripsCreator):

_DAYS_OF_WEEK = [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' ]
_DAYS_OF_WEEK = ['monday', 'tuesday', 'wednesday',
'thursday', 'friday', 'saturday', 'sunday']
_DEFAULT_SCHEDULE = {
'opening_hours': 'Mo-Su,PH 05:00-22:00',
'interval': '01:00'
}
_DAY_ABBREVIATIONS = {
'monday': 'Mo',
'tuesday': 'Tu',
'wednesday': 'We',
'thursday': 'Th',
'friday': 'Fr',
'saturday': 'Sa',
'sunday': 'Su'
}
_DEFAULT_TRIP_DURATION = 120 # minutes

'monday': 'Mo',
'tuesday': 'Tu',
'wednesday': 'We',
'thursday': 'Th',
'friday': 'Fr',
'saturday': 'Sa',
'sunday': 'Su'
}
_DEFAULT_TRIP_DURATION = 120 # minutes

def _service_id_from_transport_hour(self, a_transport_hour):
service_days = [ day_name for day_name in self._DAYS_OF_WEEK if day_name in a_transport_hour and a_transport_hour[day_name]]
service_days = [day_name for day_name in self._DAYS_OF_WEEK
if day_name in a_transport_hour and a_transport_hour[day_name]]

if not service_days:
logging.warning('Transport_hour missing service days. Assuming 7 days a week.')
logging.warning(
'Transport_hour missing service days. Assuming 7 days a week.')
service_days = self._DAYS_OF_WEEK

def date_range(start, end):
Expand All @@ -55,7 +57,7 @@ def _init_service_period(self, feed, hour):
service_period = ServicePeriod(id=service_id)
service_period.SetStartDate(self.config['feed_info']['start_date'])
service_period.SetEndDate(self.config['feed_info']['end_date'])
for i,day_of_week in enumerate(self._DAYS_OF_WEEK):
for i, day_of_week in enumerate(self._DAYS_OF_WEEK):
if (day_of_week in hour and hour[day_of_week]):
service_period.SetDayOfWeekHasService(i)
feed.AddServicePeriodObject(service_period)
Expand All @@ -80,9 +82,11 @@ def add_trips_to_feed(self, feed, data):
transport_hours = transporthours.main.Main()
default_hours = transport_hours.tagsToGtfs(self._DEFAULT_SCHEDULE)

default_service_period = self._init_service_period(feed, default_hours[0])
default_service_period = self._init_service_period(
feed, default_hours[0])
feed.SetDefaultServicePeriod(default_service_period)
default_hours_dict = self._group_hours_by_service_period(feed, default_hours)
default_hours_dict = self._group_hours_by_service_period(
feed, default_hours)

lines = data.routes

Expand All @@ -91,20 +95,24 @@ def add_trips_to_feed(self, feed, data):
for route_id, line in sorted(lines.iteritems()):
if not isinstance(line, Line):
continue
logging.info("Generating schedule for line: " + route_id)
logging.info("Generating schedule for line: %s", route_id)
if 'operator' in line.tags and line.tags['operator']:
try:
agency = feed.GetAgency(line.tags['operator'])
except KeyError:
agency = feed.AddAgency(line.tags['operator'],
default_agency.agency_url, default_agency.agency_timezone, agency_id=line.tags['operator'])
logging.info("Added agency: {}".format(agency.agency_name))
default_agency.agency_url,
default_agency.agency_timezone,
agency_id=line.tags['operator'])
logging.info("Added agency: %s", agency.agency_name)
if not agency.Validate():
logging.error("Agency data not valid for " + line.tags['operator'] + 'in line ')
logging.error("Agency data not valid for %s in line",
line.tags['operator'])
if 'operator:website' in line.tags and line.tags['operator:website']:
agency.agency_url = line.tags['operator:website']
if not agency.Validate():
logging.error('Url is not valid for agency: ' + agency.agency_url)
logging.error(
'Url is not valid for agency: %s', agency.agency_url)
else:
agency = default_agency

Expand All @@ -122,52 +130,71 @@ def add_trips_to_feed(self, feed, data):
itineraries = line.get_itineraries()

line_hours_list = transport_hours.tagsToGtfs(line.tags)
line_hours_dict = self._group_hours_by_service_period(feed, line_hours_list)
line_hours_dict = self._group_hours_by_service_period(
feed, line_hours_list)

for a_route in itineraries:
itinerary_hours_list = transport_hours.tagsToGtfs(a_route.tags)

if itinerary_hours_list:
itinerary_hours_dict = self._group_hours_by_service_period(feed, itinerary_hours_list)
itinerary_hours_dict = self._group_hours_by_service_period(
feed, itinerary_hours_list)
elif line_hours_dict:
itinerary_hours_dict = line_hours_dict
else:
itinerary_hours_dict = default_hours_dict
logging.warning("schedule is missing, using default")
logging.warning(" Please add opening_hours & interval tags in OSM - {}".format(line.osm_url ))
logging.warning(
" Add opening_hours & interval tags in OSM - %s", line.osm_url)

for service_id,itinerary_hours in itinerary_hours_dict.items():
for service_id, itinerary_hours in itinerary_hours_dict.items():
service_period = feed.GetServicePeriod(service_id)
trip_gtfs = line_gtfs.AddTrip(feed, service_period=service_period)
trip_gtfs.shape_id = self._add_shape_to_feed(feed, a_route.osm_id, a_route)
trip_gtfs = line_gtfs.AddTrip(
feed, service_period=service_period)
trip_gtfs.shape_id = self._add_shape_to_feed(
feed, a_route.osm_id, a_route)
trip_gtfs.direction_id = route_index % 2
route_index += 1

if a_route.fr and a_route.to:
trip_gtfs.trip_headsign = a_route.to
if line_gtfs.route_short_name:
# The line.name in the OSM data (route_long_name in the GTFS) is in the format
# '{transport mode} {route_short_name if there is one} : {A terminus} ↔ {The other terminus}'
# But it is good practice to not repeat the route_short_name in the route_long_name,
# so we abridge the route_long_name here if the line has a route_short_name
line_gtfs.route_long_name = a_route.fr + " ↔ ".decode('utf-8') + a_route.to
# The line.name in the OSM data (route_long_name in the GTFS)
# is in the following format:
# '{transport mode} {route_short_name if any} :
# {A terminus} ↔ {The other terminus}'
# But it is good practice to not repeat the route_short_name
# in the route_long_name,
# so we abridge the route_long_name here if needed
line_gtfs.route_long_name = a_route.fr + \
" ↔ ".decode('utf-8') + a_route.to

for itinerary_hour in itinerary_hours:
trip_gtfs.AddFrequency(itinerary_hour['start_time'], itinerary_hour['end_time'], itinerary_hour['headway'])
trip_gtfs.AddFrequency(
itinerary_hour['start_time'], itinerary_hour['end_time'],
itinerary_hour['headway'])

if 'duration' in a_route.tags:
try:
travel_time = int(a_route.tags['duration'])
if not travel_time > 0:
logging.warning("trip duration {} is invalid - {}".format(travel_time,a_route.osm_url ))
logging.warning(
"trip duration %s is invalid - %s",
travel_time,
a_route.osm_url)
travel_time = self._DEFAULT_TRIP_DURATION
except (ValueError, TypeError) as e:
logging.warning("trip duration {} is not a number - {}".format(travel_time,a_route.osm_url ))
logging.warning(
"trip duration %s is not a number - %s",
a_route.tags['duration'],
a_route.osm_url)
travel_time = self._DEFAULT_TRIP_DURATION
else:
travel_time = self._DEFAULT_TRIP_DURATION
logging.warning("trip duration is missing, using default ({} min)".format(travel_time ))
logging.warning(" Please add a duration tag in OSM - {}".format(a_route.osm_url ))
logging.warning(
"trip duration is missing, using default (%s min)", travel_time)
logging.warning(
" Add a duration tag in OSM - %s", a_route.osm_url)

for index_stop, a_stop in enumerate(a_route.stops):
stop_id = a_stop
Expand Down
5 changes: 3 additions & 2 deletions osm2gtfs/tests/creators/tests_ci_abidjan.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def _get_selector(self):
def _get_required_variables(self):
# Define required values for the tests of this provider
return {
'routes_count': 128,
'routes_count': 128,
'stops_count': 1879,
'stations_count': 0,
'stops_osm_count': 1879,
Expand All @@ -32,7 +32,8 @@ def _override_configuration(self):

def load_tests(loader, tests, pattern):
# pylint: disable=unused-argument
test_cases = ['test_refresh_routes_cache', 'test_refresh_stops_cache', 'test_gtfs_from_cache']
test_cases = ['test_refresh_routes_cache',
'test_refresh_stops_cache', 'test_gtfs_from_cache']
suite = unittest.TestSuite(map(TestCreatorsCiAbidjan, test_cases))
return suite

Expand Down

0 comments on commit 224f4d4

Please sign in to comment.