
    bi#                        U d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZ g Zded	<   g Zded
<   daded<   d%dZd&dZd'dZd'dZ G d de      Zd(dZd)dZ ej<                  dej>                        Z d*dZ!d*dZ" ej<                  dej>                        Z#d*dZ$d*dZ%d*dZ&d*dZ'd*dZ(ed+d,d       Z)ed-d        Z*ed-d!       Z+ed.d"       Z,ed/d#       Z-ed/d$       Z.y)0z-Utilities for identifying local IP addresses.    )annotationsN)CallableIterableMappingSequence)PIPEPopen)Any)warn	list[str]	LOCAL_IPS
PUBLIC_IPS str	LOCALHOSTc                z    t               }g }| D ])  }||vs|j                  |       |j                  |       + |S )zuniq_stable(elems) -> list

    Return from an iterable, a list of all the unique elements in the input,
    maintaining the order in which they first appear.
    )setappendadd)elemsseenvaluexs       Y/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jupyter_client/localinterfaces.py_uniq_stabler      sC     5DE D=LLOHHQK L    c                   d}t         j                  dk(  r7t        j                         }|xj                  t        j
                  z  c_        t        | t        t        |      }|j                         \  }}|j                  r-dj                  | |j                  dd            }t        |      |j                  dd      S )z4Get output of a command, raising IOError if it failsNnt)stdoutstderrstartupinfozFailed to run {}: {}utf8replace)osname
subprocessSTARTUPINFOdwFlagsSTARTF_USESHOWWINDOWr	   r   communicate
returncodeformatdecodeOSError)cmdr!   pr   r    msgs         r   _get_outputr2   %   s    K	ww$ ,,.z>>>c$tEA]]_NFF||$++Cvy1QRcl==++r   c                "     d _         d fd}|S )z%decorator to only run a function onceFc                 @    j                   ry  di | }d_         |S )NT called)kwargsretfs     r   wrappedz_only_once.<locals>.wrapped7   s$    88k&k
r   )r8   r
   returnr
   r6   )r:   r;   s   ` r   
_only_oncer=   3   s    AH Nr   c                     d fd}|S )z2decorator to ensure load_ips has been run before fc                 (    t                 | i |S )N)	_load_ips)argsr8   r:   s     r   
ips_loadedz!_requires_ips.<locals>.ips_loadedD   s    $!&!!r   )rA   r
   r8   r
   r<   r
   r5   )r:   rB   s   ` r   _requires_ipsrC   A   s    " r   c                      e Zd Zy)NoIPAddressesN)__name__
__module____qualname__r5   r   r   rE   rE   L   s    r   rE   c                    t        d| i       y)z7populate local and public IPs from flat list of all IPsallN)_populate_from_dict)addrss    r   _populate_from_listrM   P   s    'r   c                   | s
t               g }g }| j                         D ]|  \  }}|D ]r  }|j                  |       t        s$|j	                  d      s|j	                  d      r|a|j	                  d      rP|j	                  d      rb|j                  |       t ~ t        r	t        dk(  rda|j                  dt               |j                  ddg       t        |      t        dd t        |      t        dd y)	z8populate local and public IPs from dict of {'en0': 'ip'}lo127.)rP   z169.254.	127.0.0.1r   0.0.0.0r   N)
rE   itemsr   r   
startswithinsertextendr   r   r   )rL   
public_ips	local_ipsifaceip_listips         r   rK   rK   U   s    o JI++- &w 	&BR %"2"24"8BMM&<Q	##D)"--@T2U!!"%	&& 	[0	I&i_%	*IaL ,JqMr   zinet\b.*?(\d+\.\d+\.\d+\.\d+)c                 $   	 t        d      } | j                         }g }|D ]H  }t        j	                  |j                               }|s)|j                  |j                  d             J t        |       y# t        $ r t        d      } Y w xY w)z0load ip addresses from `ifconfig` output (posix)ifconfigz/sbin/ifconfig   N)	r2   r.   
splitlines_ifconfig_ipv4_patmatchstripr   grouprM   outlinesrL   linems        r   _load_ips_ifconfigri   t   s    ,*%
 NNEE %$$TZZ\2LL$%   ,*+,s   A8 8BBc                    t        g d      } | j                         }g }|D ]^  }|j                         j                         }t	        |      dk\  s0|d   dk(  s9|j                  |d   j                  d      d          ` t        |       y)z/load ip addresses from `ip addr` output (Linux))r[   z-finetaddr   r   rk   r^   /N)r2   r_   lowersplitlenr   rM   )re   rf   rL   rg   blockss        r   _load_ips_iprs      s    
2
3CNNEE 2##%K16!9#6LL-a012 r   zipv4.*?(\d+\.\d+\.\d+\.\d+)$c                     t        d      } | j                         }g }|D ]H  }t        j                  |j	                               }|s)|j                  |j                  d             J t        |       y)z2load ip addresses from `ipconfig` output (Windows)ipconfigr^   N)r2   r_   _ipconfig_ipv4_patra   rb   r   rc   rM   rd   s        r   _load_ips_ipconfigrw      sc    
j
!CNNEE %$$TZZ\2LL$% r   c                     ddl } i }| j                         j                         D ]?  \  }}|D cg c]+  }|j                  t        j
                  k(  r|j                  - c}||<   A t        |       yc c}w )zload ip addresses with psutilr   N)psutilnet_if_addrsrS   familysocketAF_INETaddressrK   )ry   	addr_dictrY   ifaddressesaddress_datas        r   _load_ips_psutilr      sx    &(I
 %11399; 
{ !,
""fnn4   
	%
 	"
s   0A3c                    ddl } i }| j                         D ]a  }g ||<   | j                  |      j                  | j                  g       }|D ]*  }|j                  d      }|s||   j                  |       , c t        |       y)z load ip addresses with netifacesr   Nrl   )	netifaces
interfacesr   getr}   r   rK   )r   r   rY   ipv4sentryrl   s         r   _load_ips_netifacesr      s    &(I %%' .	%%%e,001B1BBG 	.E99V$D% ''-	.. 	"r   c                    	 t        j                  d      d   t        dd 	 t        j                         } t        j                  |       d   t
        dd | j                  d      sJt        d t
        D              r4t        j                  t        j                         dz         d   t
        dd t        t
              t
        dd t        j                  t
               t        j                  ddg       t        t              t        dd t        d	   a
y# t        $ r dgt        dd Y w xY w# t        $ r Y w xY w# t        t
              t
        dd t        j                  t
               w xY w)
zJload ip addresses with socket.gethostbyname_ex

    This can be slow.
    	localhostrm   NrQ   z.localc              3  >   K   | ]  }|j                  d         yw)127N)rT   ).0r[   s     r   	<genexpr>z*_load_ips_gethostbyname.<locals>.<genexpr>   s     2]B2==3G2]s   rR   r   r   )r|   gethostbyname_exr   r.   gethostnamer   endswithrJ   r   rV   r   )hostnames    r   _load_ips_gethostbynamer      s'   %..{;A>	!

%%%'//9!<
1  *s2]R\2]/]"33F4F4F4H84STUVWJqM %Z0
1$ i_%	*IaL!I+  %#}	!%   %Z0
1$s5   D BD0 D-,D-0	D<9D? ;D<<D? ?1E0c                 :    da t         ddgt        dd g t        dd y)z&Fallback in case of unexpected failurerQ   rR   r   N)r   r   r   r5   r   r   _load_ips_dumbr      s"     Iy"-IaLJqMr   c                   	 	 t               S # t        $ r Y nw xY w	 t               S # t        $ r Y nw xY wt        j                  dk(  r)	 t               S # t        t        f$ r Y t               S w xY w	 t               S # t        t        f$ r Y nw xY w	 t               S # t        t        f$ r Y t               S w xY w# t        $ r'}| s t        d|z  d       Y d}~t                yd}~ww xY w)a  load the IPs that point to this machine

    This function will only ever be called once.

    If will use psutil to do it quickly if available.
    If not, it will use netifaces to do it quickly if available.
    Then it will fallback on parsing the output of ifconfig / ip addr / ipconfig, as appropriate.
    Finally, it will fallback on socket.gethostbyname_ex, which can be slow.
    r   z9Unexpected error discovering local network interfaces: %srm   )
stacklevelN)r   ImportErrorr   r$   r%   rw   r.   rE   rs   ri   r   	Exceptionr   r   )suppress_exceptionses     r   r@   r@      s   %\	#%% 			&(( 		
 77d?)++]+  '((#~%]+ )++]+  '((  \"H1LYZ[[\s   	 	B; B; 	' 	3B; 3B; 
	A A/#
B; .A//B; 3	A= =BB; BB; 	B B8,
B; 7B88B; ;	C+C&&C+c                     t         S )z2return the IP addresses that point to this machiner   r5   r   r   rX   rX   &  
     r   c                     t         S )zKreturn the IP addresses for this machine that are visible to other machinesr   r5   r   r   rW   rW   ,  s
     r   c                     t         S )z1return ip for localhost (almost always 127.0.0.1))r   r5   r   r   r   r   2  r   r   c                    | t         v S )z does `ip` point to this machine?r   r[   s    r   is_local_ipr   8  s     ?r   c                    | t         v S )z#is `ip` a publicly visible address?r   r   s    r   is_public_ipr   >  s     r   )r   r   r<   list)r/   zstr | Sequence[str]r<   r   )r:   r   r<   r   )rL   zSequence[str]r<   None)rL   zMapping[str, Sequence[str]]r<   r   )r<   r   )T)r   boolr<   r   )r<   r   )r<   r   )r[   r   r<   r   )/__doc__
__future__r   r$   rer|   r&   collections.abcr   r   r   r   r   r	   typingr
   warningsr   r   __annotations__r   r   r   r2   r=   rC   r   rE   rM   rK   compile
IGNORECASEr`   ri   rs   rv   rw   r   r   r   r   r@   rX   rW   r   r   r   r5   r   r   <module>r      sA   3 # 	 	   A A "  	9 
I 	3 ,	I 	(
-8  RZZ @"--P $
  RZZ ?O 
#&#&@ 1 1h  
  
  
  
  r   