a
    ki4                     @   sj   d dl Z d dlmZ d dlmZ G dd dZG dd dZG dd	 d	ZG d
d dZG dd dZdS )    N)defaultdict)datetimec                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )GtfsStopsCachec                 C   s"   || _ |  | _| | j| _d S N)
_gtfs_root_get_stops_by_id_stops_by_id_map_stops_by_parent_id_stops_by_parent_idself	gtfs_root r   !/var/www/html/GtfsCacheHelpers.py__init__   s    
zGtfsStopsCache.__init__c                 C   s
   | j | S r   )r   r   idr   r   r   get_stop   s    zGtfsStopsCache.get_stopc                 C   s
   | j  S r   )r   valuesr   r   r   r   get_all_stops   s    zGtfsStopsCache.get_all_stopsc                 C   s
   | j | S r   )r
   )r   Z	parent_idr   r   r   get_all_quays_in_stop_place   s    z*GtfsStopsCache.get_all_quays_in_stop_placec                 C   s`   t  }t| jd dd4}tj|dd}|D ]}|||d < q,W d    n1 sR0    Y  |S )Nz
/stops.txt	utf-8-sigencoding,	delimiterstop_iddictopenr   csv
DictReader)r   stopscsv_filereaderrowr   r   r   r      s    ,zGtfsStopsCache._get_stops_by_idc                 C   s4   t t}| D ]}|d r||d  | q|S )Nparent_station)r   listr   append)r   Zstops_by_idZstops_by_parent_idstopr   r   r   r	      s
    z&GtfsStopsCache._map_stops_by_parent_idN)	__name__
__module____qualname__r   r   r   r   r   r	   r   r   r   r   r      s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )GtfsRoutesCachec                 C   s   || _ |  | _d S r   )r   _get_routes_by_id_routes_by_idr   r   r   r   r   &   s    zGtfsRoutesCache.__init__c                 C   s
   | j | S r   )r1   r   r   r   r   	get_route*   s    zGtfsRoutesCache.get_routec                 C   s`   t  }t| jd dd4}tj|dd}|D ]}|||d < q,W d    n1 sR0    Y  |S )Nz/routes.txtr   r   r   r   route_idr   )r   routesr%   r&   r'   r   r   r   r0   -   s    ,z!GtfsRoutesCache._get_routes_by_idN)r,   r-   r.   r   r2   r0   r   r   r   r   r/   %   s   r/   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )GtfsTripsCachec                 C   s   || _ |  | _d S r   )r   _get_trips_by_id_trips_by_idr   r   r   r   r   7   s    zGtfsTripsCache.__init__c                 C   s
   | j | S r   )r7   r   r   r   r   get_trip;   s    zGtfsTripsCache.get_tripc                 C   s`   t  }t| jd dd4}tj|dd}|D ]}|||d < q,W d    n1 sR0    Y  |S )Nz
/trips.txtr   r   r   r   trip_idr   )r   Ztripsr%   r&   r'   r   r   r   r6   >   s    ,zGtfsTripsCache._get_trips_by_idN)r,   r-   r.   r   r8   r6   r   r   r   r   r5   6   s   r5   c                   @   sV   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd ZdS )GtfsStopTimesCacheFc                 C   s:   || _ || _|s6|  | _| | j| _| | j| _d S r   )r   _reduce_memory_usage_get_stop_times_stop_times_map_stop_times_by_stop_id_stops_by_stop_id_map_stop_times_by_trip_id_stops_by_trip_id)r   r   reduce_memory_usager   r   r   r   H   s    
zGtfsStopTimesCache.__init__c                 C   s   | j S r   )r=   r   r   r   r   get_stop_timesP   s    z!GtfsStopTimesCache.get_stop_timesc                 C   s   | j s| j| S t }t| jd ddR}tj|dd}|D ].}|d |krNq<| |d |d< || q<W d    n1 s0    Y  |S d S )	N/stop_times.txtr   r   r   r   r9   departure_timedeparture_seconds)	r;   rA   r)   r!   r   r"   r#   get_seconds_since_midnightr*   )r   r9   
stop_timesr%   r&   r'   r   r   r   get_stop_times_for_tripS   s    
*z*GtfsStopTimesCache.get_stop_times_for_tripc                 C   s   | j s| j| S t }t| jd ddR}tj|dd}|D ].}|d |krNq<| |d |d< || q<W d    n1 s0    Y  |S d S )	NrD   r   r   r   r   r   rE   rF   )	r;   r?   r)   r!   r   r"   r#   rG   r*   )r   r   rH   r%   r&   r'   r   r   r   get_stop_times_for_stopb   s    
*z*GtfsStopTimesCache.get_stop_times_for_stopc                    s    j s& fdd|D }dd |D S t }t jd ddR}tj|dd}|D ].}|d	 |vrdqR |d
 |d< || qRW d    n1 s0    Y  |S d S )Nc                    s   g | ]}  |qS r   )rJ   ).0r   r   r   r   
<listcomp>s       z?GtfsStopTimesCache.get_stop_times_for_stops.<locals>.<listcomp>c                 S   s   g | ]}|D ]}|qqS r   r   )rK   Zsublistitemr   r   r   rL   t   rM   rD   r   r   r   r   r   rE   rF   )r;   r)   r!   r   r"   r#   rG   r*   )r   stop_idsZstop_time_listsrH   r%   r&   r'   r   r   r   get_stop_times_for_stopsq   s    *z+GtfsStopTimesCache.get_stop_times_for_stopsc                 C   sp   t  }t| jd ddD}tj|dd}|D ] }| |d |d< || q,W d    n1 sb0    Y  |S )NrD   r   r   r   r   rE   rF   )r)   r!   r   r"   r#   rG   r*   )r   rH   r%   r&   r'   r   r   r   r<      s    *z"GtfsStopTimesCache._get_stop_timesc                 C   s(   t t}|D ]}||d  | q|S )Nr   r   r)   r*   )r   r=   Zstop_times_by_stopr+   r   r   r   r>      s    z-GtfsStopTimesCache._map_stop_times_by_stop_idc                 C   s(   t t}|D ]}||d  | q|S )Nr9   rQ   )r   r=   Zstop_times_by_tripr+   r   r   r   r@      s    z-GtfsStopTimesCache._map_stop_times_by_trip_idc                 C   sD   |dd }|dd }|dd }t |d t |d  t | S )	z8Get Seconds from time, for faster calculations later on.r                  i  <   )int)r   time_strhmsr   r   r   rG      s    z-GtfsStopTimesCache.get_seconds_since_midnightN)F)r,   r-   r.   r   rC   rI   rJ   rP   r<   r>   r@   rG   r   r   r   r   r:   G   s   

r:   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )GtfsCalendarDatesCachec                 C   s8   || _ |  | _| | j| _| | j| _t | _d S r   )	r   _get_calendar_datesZ_calendar_dates_map_operating_days_by_service_dates_by_service_map_services_by_date_services_by_dater    _trip_serviced_on_date_cacher   r   r   r   r      s
    
zGtfsCalendarDatesCache.__init__c                 C   sv   t  }t| jd ddJ}tj|dd}|D ]&}t|d d |d< || q,W d    n1 sh0    Y  |S )Nz/calendar_dates.txtr   r   r   r   datez%Y%m%d)	r)   r!   r   r"   r#   r   strptimerd   r*   )r   datesr%   r&   r'   r   r   r   r^      s    *z*GtfsCalendarDatesCache._get_calendar_datesc                 C   s8   t t}|D ]&}|d dkr||d  |d  q|S )Nexception_type1
service_idrd   rQ   r   rf   Zdates_by_idr'   r   r   r   r_      s
    z5GtfsCalendarDatesCache._map_operating_days_by_servicec                 C   s8   t t}|D ]&}|d dkr||d  |d  q|S )Nrg   rh   rd   ri   rQ   rj   r   r   r   ra      s
    z,GtfsCalendarDatesCache._map_services_by_datec                 C   s
   | j | S r   )r`   )r   ri   r   r   r   get_service_operating_days   s    z1GtfsCalendarDatesCache.get_service_operating_daysc                 C   s
   | j | S r   )rb   )r   rd   r   r   r   get_operating_services   s    z-GtfsCalendarDatesCache.get_operating_servicesc                 C   s0   ||f}|| j vr&|| j| v | j |< | j | S r   )rc   rb   )r   ri   rd   keyr   r   r   is_serviced   s    
z"GtfsCalendarDatesCache.is_servicedN)
r,   r-   r.   r   r^   r_   ra   rk   rl   rn   r   r   r   r   r]      s   	r]   )	r"   collectionsr   r   r   r/   r5   r:   r]   r   r   r   r   <module>   s   X