ó
GÿøTc           @   s
  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d d g Z d d „ Z i  a	 d „  Z
 d d „ Z d d	 „ Z d d
 „ Z d „  Z d d l m Z m Z d d l Z e j d j d ƒ ƒ Z e j d j d ƒ ƒ Z d „  Z d „  Z d „  Z d S(   sµ   Cache lines from files.

This is intended to read lines from modules imported -- hence if a filename
is not found, it will look down the module search path for a file by
that name.
iÿÿÿÿNt   getlinet
   clearcachet
   checkcachec         C   sE   t  |  | ƒ } d | k o, t | ƒ k n r= | | d Sd Sd  S(   Ni   t    (   t   getlinest   len(   t   filenamet   linenot   module_globalst   lines(    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyR       s    "c           C   s
   i  a  d S(   s   Clear the cache entirely.N(   t   cache(    (    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyR      s    c         C   sR   |  t  k rA t  |  } t | ƒ d k r5 t |  | ƒ St  |  d St |  | ƒ Sd S(   ss   Get the lines for a file from the cache.
    Update the cache if it doesn't contain an entry for this file already.i   i   N(   R
   R   t   updatecache(   R   R   t   entry(    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyR   &   s    
c         C   sñ   |  d k r! t t j ƒ  ƒ } n |  t k r9 |  g } n d Sx­ | D]¥ }  t |  } t | ƒ d k rl qD n  | \ } } } } | d k r qD n  y t j | ƒ } Wn t k
 rÀ t |  =qD n X| | j k sß | | j	 k rD t |  =qD qD Wd S(   sU   Discard cache entries that are out of date.
    (This is not checked upon each call!)Ni   (
   t   Nonet   listR
   t   keysR   t   ost   statt   OSErrort   st_sizet   st_mtime(   R   t	   filenamesR   t   sizet   mtimeR	   t   fullnameR   (    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyR   3   s&    
c         C   sA  |  t  k r/ t t  |  ƒ d k r/ t  |  =q/ n  |  sT |  j d ƒ rX |  j d ƒ rX g  S|  } y t j | ƒ } Wn9t k
 r¬|  } t |  | ƒ ry t  |  d ƒ  } Wn t t f k
 rÄ qX| d k rÕ g  St | ƒ d g  | j
 ƒ  D] } | d ^ qî | f t  |  <t  |  d Sn  t j j |  ƒ r3g  Sxw t j D]d } y t j j | | ƒ } Wn t t f k
 rxq=n Xy t j | ƒ } PWq=t k
 r q=Xq=Wg  Sn Xy% t | ƒ  } | j ƒ  }	 Wd QXWn t k
 ræg  SX|	 r|	 d j d ƒ r|	 d c d 7<n  | j | j }
 } |
 | |	 | f t  |  <|	 S(	   s”   Update a cache entry and return its list of lines.
    If something's wrong, print a message, discard the cache entry,
    and return an empty list.i   t   <t   >i    s   
i   Niÿÿÿÿ(   R
   R   t
   startswitht   endswithR   R   R   t	   lazycachet   ImportErrorR   t
   splitlinest   patht   isabst   syst   joint	   TypeErrort   AttributeErrort   _tokenize_opent	   readlinesR   R   (   R   R   R   R   t   basenamet   datat   linet   dirnamet   fpR	   R   R   (    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyR   P   sV    %-c         C   sÉ   |  t  k r- t t  |  ƒ d k r& t St Sn  |  sR |  j d ƒ rV |  j d ƒ rV t S| rÅ d | k rÅ | j d ƒ } | d } t | d d ƒ } | rÅ | rÅ t	 j
 | | ƒ } | f t  |  <t Sn  t S(   sí  Seed the cache for filename with module_globals.

    The module loader will be asked for the source only when getlines is
    called, not immediately.

    If there is an entry in the cache already, it is not altered.

    :return: True if a lazy load is registered in the cache,
        otherwise False. To register such a load a module loader with a
        get_source method must be found, the filename must be a cachable
        filename, and the filename must not be already cached.
    i   R   R   t
   __loader__t   __name__t
   get_sourceN(   R
   R   t   Truet   FalseR   R   t   gett   getattrR   t	   functoolst   partial(   R   R   t   namet   loaderR/   t	   get_lines(    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyR   ‘   s    %
(   t   lookupt   BOM_UTF8s%   ^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)t   utf8s   ^[ \t\f]*(?:[#\r\n]|$)c         C   sY   t  j |  d ƒ } t | j ƒ \ } } | j d ƒ t  j | | d t ƒ} d | _ | S(   sX   Open a file in read only mode using the encoding detected by
    detect_encoding().
    t   rbi    t   line_bufferingt   r(   t   iot   opent   _detect_encodingt   readlinet   seekt   TextIOWrapperR0   t   mode(   R   t   buffert   encodingR	   t   text(    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyR&   ¹   s    	c         C   s^   |  d  j  ƒ  j d d ƒ } | d k s7 | j d ƒ r; d S| d k sV | j d ƒ rZ d S|  S(   s(   Imitates get_normal_name in tokenizer.c.i   t   _t   -s   utf-8s   utf-8-s   latin-1s
   iso-8859-1s   iso-latin-1s   latin-1-s   iso-8859-1-s   iso-latin-1-(   s   latin-1s
   iso-8859-1s   iso-latin-1(   s   latin-1-s   iso-8859-1-s   iso-latin-1-(   t   lowert   replaceR   (   t   orig_enct   enc(    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyt   _get_normal_nameÅ   s    c            s'  y ˆ j  j ‰ Wn t k
 r) d ‰ n Xt ‰  d } d } ‡ f d †  } ‡  ‡ f d †  } | ƒ  } | j t ƒ rŽ t ‰  | d } d } n  | sž | g  f S| | ƒ } | r½ | | g f St j	 | ƒ sÙ | | g f S| ƒ  } | sõ | | g f S| | ƒ } | r| | | g f S| | | g f S(   s  
    The detect_encoding() function is used to detect the encoding that should
    be used to decode a Python source file.  It requires one argument, readline,
    in the same way as the tokenize() generator.

    It will call readline a maximum of twice, and return the encoding used
    (as a string) and a list of any lines (left as bytes) it has read in.

    It detects the encoding from the presence of a utf-8 bom or an encoding
    cookie as specified in pep-0263.  If both a bom and a cookie are present,
    but disagree, a SyntaxError will be raised.  If the encoding cookie is an
    invalid charset, raise a SyntaxError.  Note that if a utf-8 bom is found,
    'utf-8-sig' is returned.

    If no encoding is specified, then the default of 'utf-8' will be returned.
    s   utf-8c              s$   y ˆ  ƒ  SWn t  k
 r d SXd  S(   NR   (   t   StopIteration(    (   RB   (    s2   lib/python2.7/site-packages/linecache2/__init__.pyt   read_or_stopé   s    c            sB  y |  j  d ƒ } WnD t k
 rY d } ˆ d  k	 rJ d j | ˆ ƒ } n  t | ƒ ‚ n Xt j |  ƒ } | ss d  St | j d ƒ j  d ƒ ƒ } y t	 | ƒ } WnH t
 k
 rë ˆ d  k rÊ d | } n d j ˆ | ƒ } t | ƒ ‚ n Xˆ  r>| d k r1ˆ d  k rd } n d j ˆ ƒ } t | ƒ ‚ n  | d	 7} n  | S(
   Ns   utf-8s'   invalid or missing encoding declarations   {0} for {1!r}i   s   unknown encoding: s   unknown encoding for {!r}: {}s   encoding problem: utf-8s    encoding problem for {!r}: utf-8s   -sig(   t   decodet   UnicodeDecodeErrorR   t   formatt   SyntaxErrort	   cookie_ret   matchRO   t   groupR9   t   LookupError(   R*   t   line_stringt   msgRW   RG   t   codec(   t	   bom_foundR   (    s2   lib/python2.7/site-packages/linecache2/__init__.pyt   find_cookieï   s6    			i   s	   utf-8-sigN(
   t   __self__R6   R%   R   R1   R   R:   R0   t   blank_reRW   (   RB   RG   t   defaultRQ   R^   t   firstt   second(    (   R]   R   RB   s2   lib/python2.7/site-packages/linecache2/__init__.pyRA   Ñ   s8    
&	
	
	(   t   __doc__R4   R?   R"   R   t   tokenizet   __all__R   R    R
   R   R   R   R   R   t   codecsR9   R:   t   ret   compilet   encodeRV   R`   R&   RO   RA   (    (    (    s2   lib/python2.7/site-packages/linecache2/__init__.pyt   <module>   s(   	A	"		