ó
Ýà‹]c           @   sr  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z m Z d d l m Z g  Z	 g  Z
 d a d „  Z d „  Z d „  Z d	 „  Z d
 e f d „  ƒ  YZ d „  Z e j d e j ƒ Z d „  Z d „  Z e j d e j ƒ Z d „  Z d „  Z d „  Z d „  Z e e d „ ƒ Z e d „  ƒ Z e d „  ƒ Z  e d „  ƒ Z! e d „  ƒ Z" e d „  ƒ Z# d S(   s-   Utilities for identifying local IP addresses.iÿÿÿÿN(   t   Popent   PIPE(   t   warnt    c         C   s<   t  ƒ  } g  |  D]( } | | k r | j | ƒ r | ^ q S(   sÇ   uniq_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.
    
    From ipython_genutils.data
    (   t   sett   add(   t   elemst   seent   x(    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _uniq_stable   s    	c         C   s¤   d } t j d k r6 t j ƒ  } | j t j O_ n  t |  d t d t d | ƒ} | j	 ƒ  \ } } | j
 r” t d |  | j d d ƒ f ƒ ‚ n  | j d d ƒ S(	   s4   Get output of a command, raising IOError if it failst   ntt   stdoutt   stderrt   startupinfos   Failed to run %s: %st   utf8t   replaceN(   t   Nonet   ost   namet
   subprocesst   STARTUPINFOt   dwFlagst   STARTF_USESHOWWINDOWR    R   t   communicatet
   returncodet   IOErrort   decode(   t   cmdR   t   pR   R   (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _get_output    s    	%c            s   t  ˆ  _ ‡  f d †  } | S(   s%   decorator to only run a function oncec             s&   ˆ  j  r d  Sˆ  |    } t ˆ  _  | S(   N(   t   calledt   True(   t   kwargst   ret(   t   f(    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   wrapped/   s
    		(   t   FalseR   (   R"   R#   (    (   R"   s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt
   _only_once,   s    	c            s   ‡  f d †  } | S(   s2   decorator to ensure load_ips has been run before fc             s   t  ƒ  ˆ  |  | Ž  S(   N(   t	   _load_ips(   t   argsR    (   R"   (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt
   ips_loaded9   s    (    (   R"   R(   (    (   R"   s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _requires_ips7   s    t   NoIPAddressesc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyR*   ?   s   c         C   s·   |  s t  ‚ n  g  } g  } xI |  D]A } | j | ƒ | j d ƒ sT | j | ƒ q" t s" | a q" q" Wt s† d a | j d t ƒ n  | j d d g ƒ t | ƒ t (t | ƒ t (d S(   s7   populate local and public IPs from flat list of all IPss   127.s	   127.0.0.1i    s   0.0.0.0R   N(	   R*   t   appendt
   startswitht	   LOCALHOSTt   insertt   extendR	   t	   LOCAL_IPSt
   PUBLIC_IPS(   t   addrst
   public_ipst	   local_ipst   ip(    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _populate_from_listB   s     	s   inet\b.*?(\d+\.\d+\.\d+\.\d+)c          C   s›   y t  d ƒ }  Wn# t t f k
 r5 t  d ƒ }  n X|  j ƒ  } g  } xB | D]: } t j | j ƒ  ƒ } | rO | j | j d ƒ ƒ qO qO Wt	 | ƒ d S(   s0   load ip addresses from `ifconfig` output (posix)t   ifconfigs   /sbin/ifconfigi   N(
   R   R   t   OSErrort
   splitlinest   _ifconfig_ipv4_patt   matcht   stripR-   t   groupR8   (   t   outt   linesR4   t   linet   m(    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _load_ips_ifconfig]   s    c          C   sž   t  d d d d g ƒ }  |  j ƒ  } g  } xc | D][ } | j ƒ  j ƒ  } t | ƒ d k r1 | d d k r1 | j | d j d ƒ d ƒ q1 q1 Wt | ƒ d	 S(
   s/   load ip addresses from `ip addr` output (Linux)R7   s   -ft   inett   addri   i    i   t   /N(   R   R;   t   lowert   splitt   lenR-   R8   (   R@   RA   R4   RB   t   blocks(    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _load_ips_ipo   s    "%s   ipv4.*?(\d+\.\d+\.\d+\.\d+)$c          C   sq   t  d ƒ }  |  j ƒ  } g  } xB | D]: } t j | j ƒ  ƒ } | r% | j | j d ƒ ƒ q% q% Wt | ƒ d S(   s2   load ip addresses from `ipconfig` output (Windows)t   ipconfigi   N(   R   R;   t   _ipconfig_ipv4_patR=   R>   R-   R?   R8   (   R@   RA   R4   RB   RC   (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _load_ips_ipconfig}   s    c          C   s  d d l  }  g  } g  } x¨ |  j ƒ  D]š } |  j | ƒ j |  j g  ƒ } xs | D]k } | j d ƒ } | sq qP n  | j d ƒ pŒ | j d ƒ sŸ | j | ƒ n t s® | a n  | j | ƒ qP Wq% Wt sâ d a | j d t ƒ n  | j	 d d	 g ƒ t
 | ƒ t (t
 | ƒ t (d S(
   s    load ip addresses with netifacesiÿÿÿÿNRF   t   los   127.s	   127.0.0.1i    s   0.0.0.0R   (   t	   netifacest
   interfacest   ifaddressest   gett   AF_INETR.   R-   R/   R0   R1   R	   R2   R3   (   RQ   R6   R5   t   ifacet   ipv4st   entryRF   (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _load_ips_netifacesŠ   s(    	c          C   s  y t  j d ƒ d t (Wn t  j k
 r8 d g t (n Xz† yk t  j ƒ  }  t  j |  ƒ d t (|  j d ƒ r¦ t d „  t Dƒ ƒ r¦ t  j t  j ƒ  d ƒ d t (n  Wn t  j k
 r½ n XWd t t ƒ t (t j	 t ƒ Xt j	 d d g ƒ t t ƒ t (t d	 a
 d S(
   sN   load ip addresses with socket.gethostbyname_ex
    
    This can be slow.
    t	   localhosti   s	   127.0.0.1s   .localc         s   s   |  ] } | j  d  ƒ Vq d S(   t   127N(   R.   (   t   .0R7   (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pys	   <genexpr>·   s    Ns   0.0.0.0R   i    (   t   sockett   gethostbyname_exR2   t   errort   gethostnameR3   t   endswitht   allR	   R1   R/   (   t   hostname(    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _load_ips_gethostbyname§   s"     &%c           C   s!   d a  t  d d g t (g  t (d S(   s&   Fallback in case of unexpected failures	   127.0.0.1s   0.0.0.0R   N(   R/   R2   R3   (    (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   _load_ips_dumbÆ   s    c         C   sì   y± y t  ƒ  SWn t k
 r! n Xt j d k rY y t ƒ  SWq© t t f k
 rU q© XnP y t ƒ  SWn t t t f k
 r€ n Xy t	 ƒ  SWn t t t f k
 r¨ n Xt
 ƒ  SWn- t k
 rà } |  sÏ ‚  n  t d | ƒ n Xt ƒ  d S(   sO  load the IPs that point to this machine
    
    This function will only ever be called once.
    
    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
   s9   Unexpected error discovering local network interfaces: %sN(   RY   t   ImportErrorR   R   RO   R   R*   RL   R:   RD   Rd   t	   ExceptionR   Re   (   t   suppress_exceptionst   e(    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyR&   Í   s0    c           C   s   t  S(   s2   return the IP addresses that point to this machine(   R2   (    (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyR6   û   s    c           C   s   t  S(   sK   return the IP addresses for this machine that are visible to other machines(   R3   (    (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyR5      s    c           C   s   t  S(   s1   return ip for localhost (almost always 127.0.0.1)(   R/   (    (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyRZ     s    c         C   s
   |  t  k S(   s    does `ip` point to this machine?(   R2   (   R7   (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   is_local_ip
  s    c         C   s
   |  t  k S(   s#   is `ip` a publicly visible address?(   R3   (   R7   (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   is_public_ip  s    ($   t   __doc__R   t   reR]   R   R    R   t   warningsR   R2   R3   R/   R	   R   R%   R)   Rg   R*   R8   t   compilet
   IGNORECASER<   RD   RL   RN   RO   RY   Rd   Re   R   R&   R6   R5   RZ   Rj   Rk   (    (    (    s=   lib/python2.7/site-packages/jupyter_client/localinterfaces.pyt   <module>   s<   											-