σ
ζNXc           @@  sΌ  d  Z  d d l m Z d d l Z d d l m Z m Z d d l Z d d l Z d d l	 m
 Z
 e j d e j  Z e j d e j  Z y d d	 l m Z Wn9 e k
 rΩ d d
 l m Z m Z d   Z d   Z n Xy d d l m Z Wn e k
 r
d   Z n Xd e d  Z d   Z e d  Z d e d  Z d   Z yH y d d l m Z m Z Wn' e k
 rd d l  m Z m Z n XWn& e k
 r·d   Z d d  Z n Xd S(   s¦   
Tools to open .py files as Unicode, using the encoding specified within the file,
as per PEP 263.

Much of the code is taken from the tokenize module in Python 3.2.
i    (   t   absolute_importN(   t   TextIOWrappert   BytesIOi   (   t   unicode_types   coding[:=]\s*([-\w.]+)s   ^\s*#.*coding[:=]\s*([-\w.]+)(   t   detect_encoding(   t   lookupt   BOM_UTF8c         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   replacet
   startswith(   t   orig_enct   enc(    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyt   _get_normal_name   s    c         @  sή   t    d } d }  f d   }   f d   } |   } | j t  ra t   | d } d } n  | sq | g  f S| |  } | r | | 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 argment, 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(   Nt    (   t   StopIteration(    (   t   readline(    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyt   read_or_stop:   s    c         @  sΊ   y |  j  d  } Wn t k
 r' d  SXt j |  } | sA d  St | d  } y t |  } Wn! t k
 r t d |   n X  rΆ | j	 d k r© t d   n  | d 7} n  | S(   Nt   asciii    s   unknown encoding: s   utf-8s   encoding problem: utf-8s   -sig(
   t   decodet   UnicodeDecodeErrort   Nonet	   cookie_ret   findallR   R   t   LookupErrort   SyntaxErrort   name(   t   linet   line_stringt   matchest   encodingt   codec(   t	   bom_found(    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyt   find_cookie@   s"    i   s	   utf-8-sigN(   t   FalseR   R   R   t   True(   R   R   t   defaultR   R"   t   firstt   second(    (   R!   R   sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyR   &   s,    	
	
	(   t   openc         C@  sV   t  j |  d  } t | j  \ } } | j d  t | | d t } d | _ | S(   s`   Open a file in read only mode using the encoding detected by
        detect_encoding().
        t   rbi    t   line_bufferingt   r(   t   ioR(   R   R   t   seekR   R$   t   mode(   t   filenamet   bufferR   t   linest   text(    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyR(   r   s    	R
   c         C@  sΒ   t  |  t  r |  St  |  t  r1 t |   } n |  } y t | j  \ } } Wn t k
 ri d } n X| j d  t | | d | d t	 } d | _
 | r΄ d j t |   S| j   Sd S(   s#  Converts a bytes string with python source code to unicode.

    Unicode strings are passed through unchanged. Byte strings are checked
    for the python source file encoding cookie to determine encoding.
    txt can be either a bytes buffer or a string containing the source
    code.
    R   i    t   errorsR*   R+   u    N(   t
   isinstanceR   t   bytesR   R   R   R   R-   R   R$   R.   t   joint   strip_encoding_cookiet   read(   t   txtR3   t   skip_encoding_cookieR0   R   R   R2   (    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyt   source_to_unicode}   s    
	c         c@  s   t  |   } yJ t |  } t j |  s2 | Vn  t |  } t j |  sU | Vn  Wn t k
 rj d SXx | D] } | Vqr Wd S(   s{   Generator to pull lines from a text-mode file, skipping the encoding
    cookie if it is found in the first two lines.
    N(   t   itert   nextt   cookie_comment_ret   matchR   (   t   fileliket   itR&   R'   R   (    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyR7      s    c         C@  s<   t  |   * } | r( d j t |   S| j   SWd QXd S(   s   Read a Python file, using the encoding declared inside the file.
    
    Parameters
    ----------
    filename : str
      The path to the file to read.
    skip_encoding_cookie : bool
      If True (the default), and the encoding declaration is found in the first
      two lines, that line will be excluded from the output - compiling a
      unicode string with an encoding declaration is a SyntaxError in Python 2.
    
    Returns
    -------
    A unicode string containing the contents of the file.
    R   N(   R(   R6   R7   R8   (   R/   R:   t   f(    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyt   read_py_file©   s    c         C@  si   y d d l  m } Wn! t k
 r7 d d l m } n X| |   } t j | j    } t | | |  S(   s₯  Read a Python file from a URL, using the encoding declared inside the file.
    
    Parameters
    ----------
    url : str
      The URL from which to fetch the file.
    errors : str
      How to handle decoding errors in the file. Options are the same as for
      bytes.decode(), but here 'replace' is the default.
    skip_encoding_cookie : bool
      If True (the default), and the encoding declaration is found in the first
      two lines, that line will be excluded from the output - compiling a
      unicode string with an encoding declaration is a SyntaxError in Python 2.
    
    Returns
    -------
    A unicode string containing the contents of the file.
    i    (   t   urlopen(   t   urllib.requestRD   t   ImportErrort   urllibR,   R   R8   R;   (   t   urlR3   R:   RD   t   responseR0   (    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyt   read_py_urlΏ   s    c         @  s   t         f d   } | S(   sb   Given a list, returns a readline() function that returns the next element
    with each call.
    c           @  s
   t     S(   N(   R=   (    (   t   x(    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyR   ΰ   s    (   R<   (   RK   R   (    (   RK   sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyt   _list_readlineΫ   s    (   t   source_from_cachet   cache_from_sourcec         C@  s>   t  j j |   \ } } | d k r6 t d |   n  | d S(   Ns   .pycs   .pyos"   Not a cached Python file extensions   .py(   s   .pycs   .pyo(   t   ost   patht   splitextt
   ValueError(   RP   t   basenamet   ext(    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyRM   ξ   s    c         C@  s?   | d  k r t } n  t j j |   \ } } | r; | d Sd S(   Ns   .pycs   .pyo(   R   t	   __debug__RO   RP   RQ   (   RP   t   debug_overrideRS   RT   (    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyRN   υ   s    	("   t   __doc__t
   __future__R    R,   R   R   t   os.pathRO   t   ret	   py3compatR   t   compilet   UNICODER   R>   t   tokenizeR   RF   t   codecsR   R   R   R(   R$   R;   R7   RC   RJ   RL   t   importlib.utilRM   RN   t   impR   (    (    (    sL   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/openpy.pyt   <module>   s>   	G			