
,]c        	   @   sw  d  Z  d d l Z d d l Z d d l Z d d l m Z e rS d d l m Z n  dR dS dT f Z	 i dU d 6dV d 6dW d 6dX d 6dY d 6dZ d 6d[ d# 6d\ d& 6d] d) 6d^ d, 6d_ d5 6d` d7 6da d: 6db d= 6Z
 dc Z i dd d 6Z dB   Z dC   Z dD   Z dE   Z d dF  Z dG   Z dH   Z dI   Z e dJ  Z dK   Z dL   Z dM   Z dN   Z dO   Z dP   Z dQ   Z d S(e   s   
Source code text utilities
iN(   t   PY2(   t   izips   
t   nts   
t   posixs   t   mact   pyt   pywt   pythont   ipyt   Pythont   pyxt   pxit   pxdt   Cythont   enamlt   Enamlt   ft   fort   f77t	   Fortran77t   f90t   f95t   f2kt   Fortrant   prot   Idlt   difft   patcht   rejt   Difft   pot   pott   GetTextt   nsit   nsht   Nsist   htmt   htmlt   Htmlt   ct   cct   cppt   cxxt   ht   hht   hppt   hxxt   Cppt   clt   OpenCLt   yamlt   ymlt   Yamlt   mdt   mdwt   Markdowns   #%%s   # %%s   # <codecell>s   # In[c         C   s4   x- t  D]% \ } } |  j |  d k r | Sq Wd S(   s   Get text EOL charactersiN(   t	   EOL_CHARSt   find(   t   textt	   eol_charst   _os_name(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   get_eol_chars,   s    c         C   s+   x$ t  D] \ } } |  | k r | Sq Wd S(   s"   Return OS name from EOL charactersN(   R8   (   R;   t   charst   os_name(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   get_os_name_from_eol_chars3   s    c         C   s+   x$ t  D] \ } } | |  k r | Sq Wd S(   s"   Return EOL characters from OS nameN(   R8   (   R?   R;   t   name(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   get_eol_chars_from_os_name:   s    c         C   sK   t  |   } | d k r t S| j |  | j    } t |  t |   k S(   s'   Detect if text has mixed EOL charactersN(   R=   t   Nonet   Falset   joint
   splitlinest   repr(   R:   R;   t   correct_text(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   has_mixed_eol_charsA   s
    c         C   s<   x5 t  D]- \ } } | | k r |  j | |  }  q q W|  S(   s   Use the same eol's in text(   R8   t   replace(   R:   t   eolt   eol_chart   _(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   normalize_eolsJ   s    c         C   s   |  j  d d  S(   s   Replace tabs by spacess   	t    i   s       (   RJ   (   R:   (    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   fix_indentationR   s    c         C   sH   d d l  m } |  g  t |  D]! } | j d  s  t |  ^ q  k S(   s<   Test if passed string is the name of a Python builtin objecti(   t   builtinsRM   (   t   spyder.py3compatRQ   t   dirt
   startswitht   str(   R:   RQ   RA   (    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt
   is_builtinW   s    c         C   s   d d l  } |  | j k S(   s5   Test if passed string is the name of a Python keywordiN(   t   keywordt   kwlist(   R:   RW   (    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt
   is_keyword^   s    c         C   s   d } t  j d |  |   } | r9 | d r9 | d } n  t  j d |  |  } | rp | d rp | | d 7} n  | r | d j   r d } n  | r | r | r |  | d d k r t |  | d d t S| S(	   s   Return Python object in *source_code* at *offset*
    Periods to the left of the cursor are carried forward 
      e.g. 'functools.par^tial' would yield 'functools.partial'
    Retry prevents infinite recursion: retry only once
    t    s   [^0-9a-zA-Z_.]is   \Wi    i   s   ([.t   retry(   t   ret   splitt   isdigitt   get_primary_atRD   (   t   source_codet   offsetR[   t   objt   leftt   right(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyR_   d   s    	'c         C   s*   t  |   } | r |  j |  S|  g Sd S(   s!   Split source code into lines
    N(   R=   R]   (   R`   R;   (    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   split_sourcey   s    c         C   sP   t  t j d |    } t j d  } g  | D] } t j | |  r. | ^ q. S(   s4   Split source code into python identifier-like tokenss   [^0-9a-zA-Z_.]s	   [a-zA-Z_](   t   setR\   R]   t   compilet   match(   R`   t   tokenst   validt   token(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   get_identifiers   s    c         C   si   g  } xE t  rM t j j |   \ } } | j |  | |  k rD Pn  | }  q	 W| j |  | j   | S(   s   
    Return the individual components of a given file path
    string (for the local operating system).

    Taken from https://stackoverflow.com/q/21498939/438386
    (   t   Truet   ost   pathR]   t   appendt   reverse(   Ro   t
   componentst   new_patht   tail(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   path_components   s    	

c         C   s  g  } t  } d } xa t t |  |   D]J \ } \ } } | | k r_ | d k r[ t } n  Pn | } | j |  q( Wt |  t |   d  } t j j	 |    | d  }	 t |  d k rt j j	 |   }
 t |
  d } |	 | d k rn| rnt
 |	 |  } | d d k ra| d d k rat |	 |  d k ra| j d |  t j j	 |   }	 q|	 | }	 q| s}| }	 qt j j d  r|	 d k rd }	 qn  |	 S(	   s   
    Return the differentiated prefix of the given two iterables. 
     
    Taken from https://stackoverflow.com/q/21498939/438386
    i   i   i    RZ   i   t   linuxt   /N(   RD   RC   t	   enumeratet   zipRm   Rp   t   lenRn   Ro   RE   Ru   t   insertt   syst   platformRT   (   t   path_components0t   path_components1t   longest_prefixt   root_comparisont   common_elmtt   indext   elmt0t   elmt1t   file_name_lengtht   path_0t   longest_path_prefixt   longest_prefix_lengtht   path_0_components(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   differentiate_prefix   s6    (	#	c         C   s9  t  j j |  } t |  |  } t |  d k r5t |  } | | k rm | j t |   t |  } n  t t |  t |   } t |  } t |  } | d k r$t |  d k r$| d d k r | d d k r | d d | d g } n | d d | d g } t  j j	 |   } n  | d	 | } n  | S(
   s"   Get tab title without ambiguation.i   i   i   i    Rw   RZ   s   ...is    - (
   Rn   Ro   t   basenamet   get_same_name_filesRz   t   shortest_patht   removeRu   R   RE   (   t   files_path_listt   filenamet   fnamet   same_name_filest   compare_patht	   diff_patht   diff_path_lengtht   path_component(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   disambiguate_fname   s(     

c         C   sI   g  } x< |  D]4 } | t  j j |  k r | j t |   q q W| S(   sB   Get a list of the path components of the files with the same name.(   Rn   Ro   R   Rp   Ru   (   R   R   R   R   (    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyR      s
    c         C   sx   t  |   d k rt |  d } t  |  d  } x5 |  D]- } t  |  | k  r3 t  |  } | } q3 q3 Wt j j |   Sd S(   s(   Shortest path between files in the list.i    N(   Rz   Rn   Ro   RE   (   R   R   t   shortest_path_lengtht
   path_elmts(    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyR      s    
(   s   
R   (   s   
R   (   s   R   (   R   R   R   R   (   R
   R   R   (   R   (   R   R   R   (   R   R   R   (   R   (   R   R   R   (   R   R   (   R!   R"   (   R$   R%   (   R'   R(   R)   R*   R+   R,   R-   R.   (   R0   (   R2   R3   (   R5   R6   (   R	   R   R   (   s   #%%s   # %%s   # <codecell>s   # In[(   t   __doc__R\   Rn   R|   RR   R    t	   itertoolsR   Ry   R8   t   ALL_LANGUAGESt   PYTHON_LIKE_LANGUAGESt   CELL_LANGUAGESR=   R@   RB   RI   RN   RP   RV   RY   Rm   R_   Re   Rl   Ru   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/spyder/utils/sourcecode.pyt   <module>	   sP   
									
			$		