ó
‹²,]c           @   s>  d  Z  d d l Z d d l Z d d l Z d d l j 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 m Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 d „ Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e e d „ Z  d „  Z! d „  Z" d „  Z# d S(   s   Miscellaneous utilitiesiÿÿÿÿN(   t   is_text_stringt   getcwd(   t   get_home_dirt   debug_printc         C   sX   t  j |  ƒ d d k rT x8 d D]- } t  j |  | ƒ r  t j |  | ƒ q  q  Wn  d S(   sC   Eventually remove .pyc and .pyo files associated to a Python scripti   s   .pyt   ct   oN(   R   R   (   t   ospt   splitextt   existst   ost   remove(   t   fnamet   ending(    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   __remove_pyc_pyo   s    c         C   s   t  j |  | ƒ t |  ƒ d S(   st   
    Rename file from *source* to *dest*
    If file is a Python script, also rename .pyc and .pyo files if any
    N(   R	   t   renameR   (   t   sourcet   dest(    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   rename_file   s    c         C   s   t  j |  ƒ t |  ƒ d S(   sd   
    Remove file *fname*
    If file is a Python script, also rename .pyc and .pyo files if any
    N(   R	   R
   R   (   R   (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   remove_file&   s    c         C   s*   d d l  } | j |  | ƒ t |  ƒ d S(   sr   
    Move file from *source* to *dest*
    If file is a Python script, also rename .pyc and .pyo files if any
    iÿÿÿÿN(   t   shutilt   copyR   (   R   R   R   (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt	   move_file/   s    c         C   s<   t  j | t  j ƒ s5 t  j | t j ƒ |  | ƒ n ‚  d S(   s  Error handler for `shutil.rmtree`.

    If the error is due to an access error (read-only file), it
    attempts to add write permission and then retries.
    If the error is for another reason, it re-raises the error.

    Usage: `shutil.rmtree(path, onerror=onerror)N(   R	   t   accesst   W_OKt   chmodt   statt   S_IWUSR(   t   functiont   patht   excinfo(    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   onerror9   s    i N  c         C   sŽ   d d l  } x{ t r‰ z] y5 | j  | j | j | j ƒ } | j d |  f ƒ Wn  | j k
 ro } |  d 7}  n XPWd | j ƒ  d } Xq W|  S(   s   Find and return a non used portiÿÿÿÿNs	   127.0.0.1i   (	   t   sockett   Truet   AF_INETt   SOCK_STREAMt   IPPROTO_TCPt   bindt   errort   closet   None(   t   default_portR   t   sockt   _msg(    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   select_portI   s    	 
c            sŠ  ˆ  d k rT d d d d d d d d d	 d
 d d d d d d d d d d d g ‰  n  | d k ru d d d d g } n  ‡  f d †  } d } d } t j |  ƒ rZxÞ t j |  ƒ D]¤ \ } } } x+ | D]" }	 |	 | k rÆ | j |	 ƒ qÆ qÆ W| d k st j | ƒ | k r¯ xC | D]8 }
 | t j | |
 ƒ ƒ \ } } | | 7} | | 7} qWq¯ q¯ Wn& | |  ƒ \ } } | | 7} | | 7} | | f S(   s°   Return number of source code lines for all filenames in subdirectories
    of *path* with names ending with *extensions*
    Directory names *excluded_dirnames* will be ignoreds   .pys   .pyws   .ipys   .enamls   .cs   .hs   .cpps   .hpps   .inct   .s   .hhs   .hxxs   .ccs   .cxxs   .cls   .fs   .fors   .f77s   .f90s   .f95s   .f2kt   buildt   dists   .hgs   .svnc            sn   d \ } } t  j |  ƒ d ˆ  k rd d } t |  d ƒ % } t | j ƒ  j ƒ  j ƒ  ƒ } Wd  QXn  | | f S(   Ni    i   t   rb(   i    i    (   R   R   t   opent   lent   readt   stript
   splitlines(   R   t   dfilest   dlinest   textfile(   t
   extensions(    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   get_filelinesg   s    'i    N(   R'   R   t   isdirR	   t   walkR
   t   dirnamet   join(   R   R8   t   excluded_dirnamesR9   t   linest   filest   dirpatht   dirnamest	   filenamest   dR   R5   R6   (    (   R8   s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   count_lines]   s0    


c         C   sf   t  j d k rb |  j d ƒ r; |  j d ƒ r; |  d  }  n  |  j d d ƒ }  |  j d d ƒ }  n  |  S(   sv  Remove backslashes in *path*

    For Windows platforms only.
    Returns the path unchanged on other platforms.

    This is especially useful when formatting path strings on
    Windows platforms for which folder paths may contain backslashes
    and provoke unicode decoding errors in Python 3 (or in Python 2
    when future 'unicode_literals' symbol has been imported).t   nts   \s   \\iÿÿÿÿt   /s   /'s   \'(   R	   t   namet   endswitht   replace(   R   (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   remove_backslashes‚   s    
c         C   s   d d l  } | j d |  ƒ S(   s   Return error matchiÿÿÿÿNs     File "(.*)", line (\d*)(   t   ret   match(   t   textRL   (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   get_error_match–   s    c          C   s1   t  j j d d ƒ }  |  j d ƒ r- d }  n  |  S(   s    Return path to Python executables   pythonw.exes
   python.exes
   spyder.exe(   t   syst
   executableRJ   RI   (   RQ   (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   get_python_executableœ   s    	c            s   ‡  ‡ f d †  } | S(   sB  
    Add the decorated method to the given class; replace as needed.

    If the named method already exists on the given class, it will
    be replaced, and a reference to the old method is created as
    cls._old<patch_name><name>. If the "_old_<patch_name>_<name>" attribute
    already exists, KeyError is raised.
    c            s˜   |  j  } t ˆ  | d  ƒ } | d  k	 r„ d ˆ | f } t ˆ  | d  ƒ } | d  k rh t ˆ  | | ƒ q„ t d ˆ  j  | f ƒ ‚ n  t ˆ  | |  ƒ |  S(   Ns
   _old_%s_%ss   %s.%s already exists.(   t   __name__t   getattrR'   t   setattrt   KeyError(   t   funcR   t   old_funct   old_reft   old_attr(   t   clst
   patch_name(    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt	   decorator²   s    	(    (   R[   R\   R]   (    (   R[   R\   s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   monkeypatch_method¥   s    c         C   s   t  j |  ƒ o |  j d ƒ S(   s   Is it a valid Python script?s   .pys   .pyws   .ipy(   s   .pys   .pyws   .ipy(   R   t   isfileRI   (   R   (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   is_python_scriptÄ   s    c         C   s   t  j t  j |  t j ƒ ƒ S(   s   Return absolute parent dir(   R   t   abspathR=   R	   t   pardir(   R   (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt	   abspardirÉ   s    c         C   s›   t  j t  j |  ƒ ƒ } t | ƒ d k r— t  j | ƒ sC t | ƒ SxQ |  D]9 } t  j t  j | | t | ƒ d ƒ ƒ sJ t | ƒ SqJ Wt  j | ƒ Sn  d S(   s,   Return common path for all paths in pathlisti   N(   R   t   normpatht   commonprefixR1   R:   Rc   R=   Ra   (   t   pathlistt   commonR   (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   get_common_pathÎ   s    
)c   
      C   sN  t  |  t ƒ s t ‚ t g  |  D] } t | ƒ ^ q ƒ s@ t ‚ d } t j j | ƒ } t j j	 | ƒ d  k	 r!| r!t j | } | sû xZ t |  ƒ D]K \ } }	 |	 j | d ƒ r˜ |	 j | d | d | t j ƒ |  | <q˜ q˜ W|  j d | ƒ qJi | t j | d 6| d 6} | Sn) | s?|  j | d | ƒ n i | d 6Sd  S(   Nt
   PYTHONPATHt   =s   OLD_PYTHONPATH=t   OLD_PYTHONPATH(   t
   isinstancet   listt   AssertionErrort   allR    R	   t   pathsepR=   t   environt   getR'   t	   enumeratet
   startswithRJ   t   append(
   t   envRf   t   drop_envt
   ipyconsoleR   t   pypatht   pathstrt
   old_pypatht   indext   var(    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   add_pathlist_to_PYTHONPATHÝ   s$    +#
c            s2   i  ‰  ˆ _  t j ˆ ƒ ‡  ‡ f d †  ƒ } | S(   sí   
    Memoize objects to trade memory for execution speed

    Use a limited size cache to store the value, which takes into account
    The calling args and kwargs

    See https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
    c             se   t  |  ƒ t  | ƒ } | ˆ  k r8 ˆ |  | Ž  ˆ  | <n  t ˆ  ƒ d k r] ˆ  j d t ƒ n  ˆ  | S(   Nid   t   last(   t   strR1   t   popitemt   False(   t   argst   kwargst   key(   t   cachet   obj(    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   memoizer  s    (   R†   t	   functoolst   wraps(   R‡   Rˆ   (    (   R†   R‡   s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   memoizeø   s    	!	c           C   s1   y t  ƒ  SWn t k
 r, t d ƒ t ƒ  SXd S(   s²   Safe version of getcwd that will fallback to home user dir.

    This will catch the error raised when the current working directory
    was removed for an external program.
    sN   WARNING: Current working directory was deleted, falling back to home dirertoryN(   R   t   OSErrorR   R   (    (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   getcwd_or_home  s
    
c         C   s5   y t  j |  ƒ Wn t  j k
 r0 } t | ƒ SXd S(   sy   
    Return None if the pattern is a valid regular expression or
    a string describing why the pattern is invalid.
    N(   RL   t   compileR%   R€   R'   (   t   patternt   e(    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   regexp_error_msg  s
    ($   t   __doc__t   codecsR‰   R	   t   os.pathR   R   RL   RP   R   t   spyder.py3compatR    R   t   spyder.config.baseR   R   R   R   R   R   R   R+   R'   RE   RK   RO   RR   R^   R`   Rc   Rh   R‚   R~   R‹   R   R‘   (    (    (    s0   lib/python2.7/site-packages/spyder/utils/misc.pyt   <module>   s8   						
	%										