o
    .hi                      @   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   _C:\Users\computer\Desktop\notes\tskylt4.0\server\20260128 - new GTFS loader\GtfsCacheHelpers.py__init__   s   
zGtfsStopsCache.__init__c                 C   
   | j | S r   )r   r   idr   r   r   get_stop      
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   r   r   )r
   )r   Z	parent_idr   r   r   get_all_quays_in_stop_place   r   z*GtfsStopsCache.get_all_quays_in_stop_placec                 C   b   t  }t| jd dd}tj|dd}|D ]}|||d < qW d    |S 1 s*w   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         
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                   @   $   e Zd Zdd Zdd Zdd ZdS )GtfsRoutesCachec                 C      || _ |  | _d S r   )r   _get_routes_by_id_routes_by_idr   r   r   r   r   '      zGtfsRoutesCache.__init__c                 C   r   r   )r7   r   r   r   r   	get_route+   r   zGtfsRoutesCache.get_routec                 C   r   )Nz/routes.txtr   r   r   r   route_idr"   )r   routesr(   r)   r*   r   r   r   r6   .   r+   z!GtfsRoutesCache._get_routes_by_idN)r0   r1   r2   r   r9   r6   r   r   r   r   r4   &       r4   c                   @   r3   )GtfsTripsCachec                 C   r5   r   )r   _get_trips_by_id_trips_by_idr   r   r   r   r   8   r8   zGtfsTripsCache.__init__c                 C   r   r   )r?   r   r   r   r   get_trip<   r   zGtfsTripsCache.get_tripc                 C   r   )Nz
/trips.txtr   r   r   r   trip_idr"   )r   Ztripsr(   r)   r*   r   r   r   r>   ?   r+   zGtfsTripsCache._get_trips_by_idN)r0   r1   r2   r   r@   r>   r   r   r   r   r=   7   r<   r=   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>   || _ || _|s|  | _| | j| _| | j| _d S 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   I   s   
zGtfsStopTimesCache.__init__c                 C   s   | j S r   )rE   r   r   r   r   get_stop_timesQ   s   z!GtfsStopTimesCache.get_stop_timesc                 C      | j s| j| S t }t| jd dd*}tj|dd}|D ]}|d |kr'q| |d |d< || qW d    |S 1 sAw   Y  |S )	N/stop_times.txtr   r   r   r   rA   departure_timedeparture_seconds)	rC   rI   r-   r$   r   r%   r&   get_seconds_since_midnightr.   )r   rA   
stop_timesr(   r)   r*   r   r   r   get_stop_times_for_tripT      

z*GtfsStopTimesCache.get_stop_times_for_tripc                 C   rL   )	NrM   r   r   r   r   r!   rN   rO   )	rC   rG   r-   r$   r   r%   r&   rP   r.   )r   r!   rQ   r(   r)   r*   r   r   r   get_stop_times_for_stopc   rS   z*GtfsStopTimesCache.get_stop_times_for_stopc                    s    j s fdd|D }dd |D S t }t jd dd*}tj|dd}|D ]}|d	 |vr2q) |d
 |d< || q)W d    |S 1 sLw   Y  |S )Nc                    s   g | ]}  |qS r   )rT   ).0r!   r   r   r   
<listcomp>t   s    z?GtfsStopTimesCache.get_stop_times_for_stops.<locals>.<listcomp>c                 S   s   g | ]	}|D ]}|qqS r   r   )rU   Zsublistitemr   r   r   rV   u   s    rM   r   r   r   r   r!   rN   rO   )rC   r-   r$   r   r%   r&   rP   r.   )r   stop_idsZstop_time_listsrQ   r(   r)   r*   r   r   r   get_stop_times_for_stopsr   s    
z+GtfsStopTimesCache.get_stop_times_for_stopsc                 C   sr   t  }t| jd dd#}tj|dd}|D ]}| |d |d< || qW d    |S 1 s2w   Y  |S )NrM   r   r   r   r   rN   rO   )r-   r$   r   r%   r&   rP   r.   )r   rQ   r(   r)   r*   r   r   r   rD      s   
z"GtfsStopTimesCache._get_stop_timesc                 C   (   t t}|D ]}||d  | q|S )Nr!   r   r-   r.   )r   rE   Zstop_times_by_stopr/   r   r   r   rF         z-GtfsStopTimesCache._map_stop_times_by_stop_idc                 C   rZ   )NrA   r[   )r   rE   Zstop_times_by_tripr/   r   r   r   rH      r\   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   rP      s    z-GtfsStopTimesCache.get_seconds_since_midnightN)F)r0   r1   r2   r   rK   rR   rT   rY   rD   rF   rH   rP   r   r   r   r   rB   H   s    

rB   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   sx   t  }t| jd dd&}tj|dd}|D ]}t|d d |d< || qW d    |S 1 s5w   Y  |S )Nz/calendar_dates.txtr   r   r   r   datez%Y%m%d)	r-   r$   r   r%   r&   r   strptimero   r.   )r   datesr(   r)   r*   r   r   r   ri      s   
z*GtfsCalendarDatesCache._get_calendar_datesc                 C   8   t t}|D ]}|d dkr||d  |d  q|S )Nexception_type1
service_idro   r[   r   rq   Zdates_by_idr*   r   r   r   rj         z5GtfsCalendarDatesCache._map_operating_days_by_servicec                 C   rr   )Nrs   rt   ro   ru   r[   rv   r   r   r   rl      rw   z,GtfsCalendarDatesCache._map_services_by_datec                 C   r   r   )rk   )r   ru   r   r   r   get_service_operating_days   r   z1GtfsCalendarDatesCache.get_service_operating_daysc                 C   r   r   )rm   )r   ro   r   r   r   get_operating_services   r   z-GtfsCalendarDatesCache.get_operating_servicesc                 C   s0   ||f}|| j vr|| j| v | j |< | j | S r   )rn   rm   )r   ru   ro   keyr   r   r   is_serviced   s   

z"GtfsCalendarDatesCache.is_servicedN)
r0   r1   r2   r   ri   rj   rl   rx   ry   r{   r   r   r   r   rh      s    	rh   )	r%   collectionsr   r   r   r4   r=   rB   rh   r   r   r   r   <module>   s     X