def _update_stop_schedule(self, stop_schedule, next_realtime_passages, groub_by_dest=False): """ Update the stopschedule response with the new realtime passages By default, all base schedule data is removed and replaced with realtime data. Each proxy can define its own way to merge passages. If next_realtime_passages is None (and not if it's []) it means that the proxy failed, so we use the base schedule """ if next_realtime_passages is None: return logging.getLogger(__name__).debug('next passages: : {}'.format( ["dt: {}".format(d.datetime) for d in next_realtime_passages])) # we clean up the old schedule pb_del_if(stop_schedule.date_times, self._filter_base_stop_schedule) direction_uri = stop_schedule.pt_display_informations.uris.stop_area for passage in next_realtime_passages: if groub_by_dest and not self._is_valid_direction( direction_uri, passage.direction_uri): continue add_direction = direction_uri != passage.direction_uri self._add_datetime(stop_schedule, passage, add_direction) stop_schedule.date_times.sort(key=lambda dt: dt.date + dt.time) if not len(stop_schedule.date_times) and not stop_schedule.HasField( 'response_status'): stop_schedule.response_status = type_pb2.no_departure_this_day
def _update_passages(self, passages, route_point, template, next_realtime_passages): if next_realtime_passages is None: return # filter passages with proxy method pb_del_if(passages, lambda passage: self._filter_base_passage(passage, route_point)) # append the realtime passages for rt_passage in next_realtime_passages: new_passage = deepcopy(template) new_passage.stop_date_time.arrival_date_time = date_to_timestamp(rt_passage.datetime) new_passage.stop_date_time.departure_date_time = date_to_timestamp(rt_passage.datetime) if rt_passage.is_real_time: new_passage.stop_date_time.data_freshness = type_pb2.REALTIME else: new_passage.stop_date_time.data_freshness = type_pb2.BASE_SCHEDULE # we also add the direction in the note if rt_passage.direction: new_passage.pt_display_informations.direction = rt_passage.direction # we add physical mode from route if len(route_point.pb_route.physical_modes) > 0: new_passage.pt_display_informations.physical_mode = route_point.pb_route.physical_modes[0].name passages.extend([new_passage])
def _update_passages(passages, route_point, template, next_realtime_passages): if next_realtime_passages is None: return # filter passages with entries of the asked route_point pb_del_if(passages, lambda p: RoutePoint(p.route, p.stop_point) == route_point) # append the realtime passages for rt_passage in next_realtime_passages: new_passage = deepcopy(template) new_passage.stop_date_time.arrival_date_time = date_to_timestamp( rt_passage.datetime) new_passage.stop_date_time.departure_date_time = date_to_timestamp( rt_passage.datetime) new_passage.stop_date_time.data_freshness = type_pb2.REALTIME # we also add the direction in the note if rt_passage.direction: new_passage.pt_display_informations.direction = rt_passage.direction # we add physical mode from route if len(route_point.pb_route.physical_modes) > 0: new_passage.pt_display_informations.physical_mode = route_point.pb_route.physical_modes[ 0].name passages.extend([new_passage])
def erase_journeys(self, resp, to_delete): """ remove a list of journeys from a response and delete all referenced objects like by example the tickets """ deleted_sections = set() for idx in sorted(to_delete, reverse=True): for section in resp.journeys[idx].sections: deleted_sections.add(section.id) del resp.journeys[idx] pb_del_if(resp.tickets, lambda t: set(t.section_id).issubset(deleted_sections))
def _update_stop_schedule(self, stop_schedule, next_realtime_passages): """ Update the stopschedule response with the new realtime passages By default we remove all base schedule data and replace them with the realtime Each proxy can define is own way to merge passages If next_realtime_passages is None (and not if it's []) it means that the proxy failed, so we use the base schedule """ if next_realtime_passages is None: return logging.getLogger(__name__).debug('next passages: : {}'.format( ["dt: {}".format(d.datetime) for d in next_realtime_passages])) # we clean up the old schedule pb_del_if(stop_schedule.date_times, self._filter_base_stop_schedule) for passage in next_realtime_passages: new_dt = stop_schedule.date_times.add() # the midnight is calculated from passage.datetime and it keeps the same timezone as passage.datetime midnight = passage.datetime.replace(hour=0, minute=0, second=0, microsecond=0) time = (passage.datetime - midnight).total_seconds() new_dt.time = int(time) new_dt.date = date_to_timestamp(midnight) if passage.is_real_time: new_dt.realtime_level = type_pb2.REALTIME else: new_dt.realtime_level = type_pb2.BASE_SCHEDULE # we also add the direction in the note if passage.direction: note = type_pb2.Note() note.note = passage.direction note_uri = hashlib.md5( note.note.encode('utf-8', 'backslashreplace')).hexdigest() note.uri = 'note:{md5}'.format( md5=note_uri ) # the id is a md5 of the direction to factorize them new_dt.properties.notes.extend([note]) stop_schedule.date_times.sort(key=lambda dt: dt.date + dt.time)
def delete_journeys(responses, request): if request.get('debug', False): return nb_deleted = 0 for r in responses: nb_deleted += pb_del_if(r.journeys, lambda j: to_be_deleted(j)) if nb_deleted: logging.getLogger(__name__).info('filtering {} journeys'.format(nb_deleted))
def _remove_non_pt_walk(self, journeys): nb_deleted = pb_del_if(journeys, lambda j: j.type == 'non_pt_walk') logger = logging.getLogger(__name__) logger.debug('remove %s non_pt_walk journey', nb_deleted)