
    ϳed                        d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZ  eej                            d ej                    dz                      Z eej                            d ej                    dz                      Zdd
Zd	efdZ e              e            Z e	eedz            Zded	dfdZddedededz  fdZd Zd Zd ZdS )    N)OptionalLiteral)Path)
VasttrafikStopLocationXDG_CONFIG_HOMEz.configXDG_CACHE_HOMEz.cachereturnc                  r    t                                           st                               d           dS dS )z/
    Create expected files and directories
    T)parentsN)CACHEDIRexistsmkdir     0/home/salvo/dev/vasttrafik-cli/vasttrafik/cli.pyinitr   !   s:     ?? %t$$$$$% %r   c                     t          j                    dz  t          dz  t          d          f} d}| D ]}|                                r|} n|dS i }t	          |d          5 }|D ]t}|                                }|r|                    d          r.|                    dd	          \  }}|                                }|                                }|||<   u	 ddd           n# 1 swxY w Y   |d
         S )aR  
    This function tries to load the API key from some configuration files.
    It will try, in the order:
        - /etc/vasttrafik-cli.conf
        - ~/.vasttrafik-cli

    If the files aren't found or they don't contain the key attribute then
    None will be returned, otherwise, a string containing the key will be
    returned.
    z.vasttrafik-clizvasttrafik-cli.confz/etc/vasttrafik-cli.confNLb2hWUmlXc192b0pYMENabTh6WmNfUlBFRGFZYTpjYjZ1U2MzSnp4ZlozRnU1V0tLZlVBSVlGNVFhrt#=   key)r   home	CONFIGDIRr   openstrip
startswithsplit)pathspathiconfigfliner   values           r   get_keyr)   )   s_    		''))'((E D  88:: 	DE	 |]]F	dD		  Q 	  	 D::<<D 4??3// C++JC))++CKKMMEF3KK	                               %=s   $A8C))C-0C-zvasttrafik-cli-tokennamec                 ^   dD ]"}|| v r|                      |d          d         } #|                                 } t          dz  }|                                r:|                    d          5 }d |D             }ddd           n# 1 swxY w Y   ng }| |v rdS |                    |            t          |          dk    r|                    d           |                    d	          5 }|                    d
	                    |                     ddd           dS # 1 swxY w Y   dS )z;
    Saves the name of the stop in the completion file
    z ,r   r   zvasttrafik-cli-stopsr   c                 6    g | ]}|                                 S r   )r   .0r$   s     r   
<listcomp>z#save_completion.<locals>.<listcomp>d   s     ***1QWWYY***r   Nd   wt
)
r!   lowerr   r   r   appendlenpopwritejoin)r*   charr#   r&   liness        r   save_completionr;   U   s   
  * *4<<::dA&&q)D::<<D ,,D{{}} YYt__ 	+*****E	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+  u}} 
LL 5zzC		! 
4 "A			%  !!!" " " " " " " " " " " " " " " " " "s$   -BB
B
,)D""D&)D&prompt
stops_onlypresetc                    |rJt                               |          }|rd |D             }t          |d         j                   |d         S 	 	 t	          |           }n+# t
          t          f$ r t          j        d           Y nw xY w|sd S t                               |          }t          t          |                    D ])}t          d|||         j        fz             |dk    r n*	 	 t	          d          }n+# t
          t          f$ r t          j        d           Y nw xY w	 t          |t          |                   j                   |t          |                   S #  Y nxY w#)Nc                 <    g | ]}t          |t                    |S r   )
isinstancer   r-   s     r   r/   zget_stop.<locals>.<listcomp>|   s'    555qAt!4!45555r   r   Tz%d: %s   > )vastlocationr;   r*   inputKeyboardInterruptEOFErrorsysexitranger5   printint)r<   r=   r>   rr'   stopsr$   s          r   get_stoprP   x   s    MM&!! 	655A555A!	"""t	==DD!8, 	 	 	HQKKKKK	 	4d##s5zz"" 	 	A(aq//0001uu 		T{{%x0   c$ii 0 5666SYY''1s0   A %BB0D   %D('D(,;E( (E,c                 x   | r@t           j                             t           j        j                                                  S 	 t          d          }n1# t          $ r t          j        d           Y nt          $ r d}Y nw xY w|dk    r@t           j                             t           j        j                                                  S 	 t          d          }t          d          }n+# t          t          f$ r t          j        d           Y nw xY wt           j                             t           j        j                                                  }|
                    t          |          t          |                    }||z
                                  dk     r|t          j        d	          z  }|S )
NzInsert time? [N/y]r    yzHour: z	Minutes: minutehourr   days)datetimenowtimezoneutc
astimezonerF   rG   rI   rJ   rH   replacerM   total_seconds	timedelta)defaultr'   rV   rU   rZ   rN   s         r   get_timerb      s    I $$X%6%:;;FFHHH)**      s{{ $$X%6%:;;FFHHHX{##x(    


 1 5
6
6
A
A
C
CC3v;;SYY77A	C  1$$	XQ''''Hs*   A B4BBC+ +%DDc                     t          t          j                  dvrt          j        d           t          t          j                  dk    rt          j        d         nd } t          t          j                  dk    rt          j        d         nd }t	          dd|           }t	          dd|          }||d S t          t          j                  dk    rqt          j        d	         d
vr't          j        dt          j        d	                     t          j        d	         dk    rdnd}	 t          j        d                             dd                              dd          \  }}t          j                            t          j	        j
                                                  }|                    t          |          t          |                    }n8# t          $ r+ t          j        dt          j        d          d           Y nw xY w||z
                                  dk     r|t          j        d          z  }nd }d}t!          d|j        d|j                   t$                              ||||          D ]J}	t!          |	                                           t!          d|	j         d           t!          d           Kd S )N>   r         Invalid number of parametersr      zFROM: > FzTO: > re   rd   >   arrivedepartzInvalid parameter rh   arrival	departure   .:rT   zUnable to parse z	 as HH:MMr   rW   	u    → z
Duration: z minutesz=========================)r5   rI   argvrJ   rP   r^   r!   rY   rZ   r[   r\   r]   rM   	Exceptionr_   r`   rL   r*   rD   triptoTermduration)
origdestorigstopdeststoparrive_departrV   rU   rZ   timer$   s
             r   tripmainr{      s   
38}}I%%/000ch--!++38A;;Dch--!++38A;;D
E400H%..H8+ 38}}8A;222H7#(1+77888FIhqkU]F]F]cn	@8A;..sC88>>sAFFLD&#''(9(=>>IIKKC;;c&kkD		;BBDD 	@ 	@ 	@H>>>>?????	@3J%%''!++H&A....D! 
E8===(--
8999YYx=$?? + +ahhjj/1:///000)****+ +s   .B.G 2HHc                     t          t          j                  dk    rt          j        d           t          t          j                  dk    rt          j        d         nd} t	          dd|           }t
                              |dd	          }t          d
|j         d           d }|D ]L}||j	        k    rt          d|j	        z             |j	        }t          |
                                           Md S )Nrg   rf   r   rR   rC   Tx   rl   )	time_span
departuresz		r2   z   == Platform %s ==)r5   rI   rp   rJ   rP   rD   boardrL   r*   trackrs   )r>   stoptrams
prev_trackr$   s        r   	stopsmainr      s    
38}}q/000MMQ..SXa[[BFD$''DJJtsqJ99E	



J    (172333W
ahhjj	 r   )r   N)N)rI   rY   ostypingr   r   pathlibr   
vasttrafikr   r   r   environgetr   r   r   r   strr)   r   rD   r;   boolrP   rb   r{   r   r   r   r   <module>r      s  ( 


  				 $ $ $ $ $ $ $ $       1 1 1 1 1 1 1 1 1 1 D 1949;;3JKKLL	4
/x1GHHII% % % %$ $ $ $ $N giiz#x"8899 "#  "$  "  "  "  "F   S  d  CH        F  6$+ $+ $+N    r   