
    !۰i                     z    d Z ddlZddlZddlZddlZddlZ ej                  d      ZdZ	 G d d      Z
 e
       Zy)uE  Flight route cache for indirect flight-number lookups.

Maintains a mapping of flight_number → {origin, destination, last_seen}
so that sources which only support route queries (e.g. Google Flights)
can participate in flight-number lookups by first resolving the route
from the cache.

Cache file: cache/flight_routes.json
    Nzflyclaw.route_cachei ' c                   t    e Zd ZdZdefdedz  defdZd Zdede	dz  fd	Z
ded
edefdZdee	   fdZd Zy)
RouteCachez In-memory + on-disk route cache.N
cache_pathttlc                     |xsC t         j                  j                  t        j                  t        j
                  d   d   d      | _        || _        i | _        d| _	        | j                          y )Ncachedirzflight_routes.jsonF)ospathjoincfgPROJECT_DIRDEFAULTSr   r   _data_dirty_load)selfr   r   s      7/root/.openclaw/workspace/skills/flyclaw/route_cache.py__init__zRouteCache.__init__   sY    
 % 
OOS\\'259;O)
 &(


    c                 n   t         j                  j                  | j                        r=	 t	        | j                  dd      5 }t        j                  |      | _        ddd       yy# 1 sw Y   yxY w# t
        j                  t        f$ r'}t        j                  d|       i | _        Y d}~yd}~ww xY w)zLoad cache from disk.rutf-8encodingNzRoute cache load failed: %s)r
   r   existsr   openjsonloadr   JSONDecodeErrorOSErrorloggerwarning)r   fes      r   r   zRouteCache._load'   s    77>>$//* $//3A .Q!%1DJ. . +. .(('2  <a@

 s5   A4 A(A4 (A1-A4 1A4 4B4B//B4flight_numberreturnc                     |j                         j                         }| j                  j                  |      }|syt	        j                         |j                  dd      z
  | j
                  kD  ry|d   |d   dS )zLook up route for a flight number.

        Returns {"origin": "PEK", "destination": "JFK"} or None if
        missing or expired.
        N	last_seenr   origindestination)r*   r+   )stripupperr   gettimer   )r   r&   fnentrys       r   r.   zRouteCache.get1   sl       "((*

r"99;;22TXX=/%:NOOr   r*   r+   c                     |j                         j                         }|r|r|sy|j                         |j                         t        t        j                               d| j                  |<   d| _        y)zUpdate a single route entry.N)r*   r+   r)   T)r,   r-   intr/   r   r   )r   r&   r*   r+   r0   s        r   updatezRouteCache.update?   s^      "((*{lln&,,.TYY[)


2
 r   recordsc                     |D ]T  }|j                  dd      }|j                  dd      }|j                  dd      }|s<|s?|sB| j                  |||       V y)z-Batch-update cache from query result records.r&    origin_iatadestination_iataN)r.   r4   )r   r5   recr0   r*   dests         r   update_from_recordszRouteCache.update_from_recordsK   sY     	.C"-BWW]B/F77-r2DfB-	.r   c                 >   | j                   syt        j                  t        j                  j	                  | j
                        d       | j
                  dz   }	 t        |dd      5 }t        j                  | j                  |dd	
       ddd       t        j                  || j
                         d| _         t        j                  dt        | j                               y# 1 sw Y   ZxY w# t        $ r }t        j                  d|       Y d}~yd}~ww xY w)z+Write cache to disk (atomic via temp file).NT)exist_okz.tmpwr   r   F   )ensure_asciiindentzRoute cache saved (%d entries)zRoute cache save failed: %s)r   r
   makedirsr   dirnamer   r   r   dumpr   replacer"   debuglenr!   r#   )r   tmpr$   r%   s       r   savezRouteCache.saveT   s    {{
BGGOODOO4tDoo&	=c31 GQ		$**aeAFGJJsDOO,DKLL93tzz?K	G G
  	=NN8!<<	=s1   C3 *$C'AC3 'C0,C3 3	D<DD)__name__
__module____qualname____doc___DEFAULT_TTLstrr3   r   r   dictr.   r4   listr<   rJ    r   r   r   r      sv    * "&$J  P P P
C 
 
3 
.4: .=r   r   )rN   r   loggingr
   r/   configr   	getLoggerr"   rO   r   route_cacherS   r   r   <module>rX      sI      	  			0	1J= J=\ lr   