ó
‡ˆ\c           @   s  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m	 Z	 d d l
 m Z d d l m Z d d l m Z d	 Z e j d
 e ƒ Z d „  Z d „  Z e e d „ Z d „  Z d „  Z d „  Z d e ƒ  d „ Z d „  Z d „  Z d d „ Z d S(   s>   
My own variation on function-specific inspect-like features.
iÿÿÿÿ(   t   isliceNi   (   t   _basestring(   t   pformat(   t   open_py_source(   t   PY3_OR_LATERsA   args varargs varkw defaults kwonlyargs kwonlydefaults annotationst   FullArgSpecc      	   C   sH  d } yö |  j } | j } t j j | ƒ s¢ d j t j |  ƒ d ƒ } d } | j	 d ƒ r• t
 j d | ƒ j ƒ  \ } } t | ƒ } d | } n  | | | f St | ƒ , } | j } t t | | d d ƒ ƒ } Wd QXd j t j | ƒ ƒ | | f SWnE t |  d ƒ r-t |  j j ƒ  ƒ | d	 f St |  ƒ | d	 f Sn Xd S(
   sl   Attempts to retrieve a reliable function code hash.

        The reason we don't use inspect.getsource is that it caches the
        source, whereas we want this to be modified on the fly when the
        function is modified.

        Returns
        -------
        func_code: string
            The function code
        source_file: string
            The path to the file in which the function is defined.
        first_line: int
            The first line of the code in the source file.

        Notes
        ------
        This function does a bit more magic than inspect, and is thus
        more robust.
    t    i    i   s	   <doctest s   \<doctest (.*\.rst)\[(.*)\]\>s   <doctest %s>Nt   __code__iÿÿÿÿ(   t   NoneR   t   co_filenamet   ost   patht   existst   joint   inspectt   getsourcelinest
   startswitht   ret   matcht   groupst   intR   t   co_firstlinenot   listR    t   getblockt   hasattrt   strt   __hash__t   repr(   t   funct   source_filet   codet   source_codet   line_not   source_file_objt
   first_linet   source_lines(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   get_func_code   s*    			"#c         C   sl   d d l  } t | d ƒ r' | j } n d d l } | j j } x& d	 D] } |  j | | | ƒ ƒ }  qF W|  S(
   s2   Windows cannot encode some characters in filename.iÿÿÿÿNt   quotet   <t   >t   !t   :s   \(   R&   R'   R(   R)   s   \(   t   urllibR   R%   t   urllib.parset   parset   replace(   t   stringR*   R%   t   char(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   _clean_win_charsR   s    c   	      C   sR  t  |  d ƒ r |  j } nK y t j |  ƒ } Wn5 t k
 re t  |  d ƒ r\ |  j j } qf d } n X| d k r{ d } n  | d k r3y t j j	 t j
 |  ƒ ƒ } Wn d } n X| d k	 r3| j t j ƒ } | d j d ƒ rô d | d <n  d	 j | ƒ } | j d
 ƒ r| d  } n  | d	 | } q3n  | j d ƒ } t  |  d ƒ r]|  j } n! t  |  d ƒ rx|  j } n d } | rÈt  |  d ƒ rÈ| |  j k rÈ|  j | |  k	 rÅd | } qÅqÈn  t j |  ƒ rt  |  d ƒ r|  j } | j | j ƒ qn  t j d k rH| rHt | ƒ } g  | D] } t | ƒ ^ q-} n  | | f S(   sâ   Return the function import path (as a list of module names), and
        a name for the function.

        Parameters
        ----------
        func: callable
            The func to inspect
        resolv_alias: boolean, optional
            If true, possible local aliases are indicated.
        win_characters: boolean, optional
            If true, substitute special characters using urllib.quote
            This is useful in Windows, as it cannot encode some filenames
    t
   __module__t	   __class__t   unknownR   t   __main__iÿÿÿÿs   <ipython-inputs   __ipython-input__t   -s   .pyiýÿÿÿt   .t	   func_namet   __name__t   func_globalss   %s-aliast   im_classt   ntN(   R   R1   R   t	   getmodulet	   TypeErrorR2   R   R
   R   t   abspatht   getsourcefilet   splitt   sepR   R   t   endswithR7   R8   R9   t   ismethodR:   t   appendt   nameR0   (	   R   t   resolv_aliast   win_characterst   modulet   filenamet   partsRE   t   klasst   s(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   get_func_name`   sR    
	
	"c         C   sr   y t  j |  ƒ SWnZ t k
 rm t  j |  ƒ } t d | j d | j d | j d | j d g  d d d i  ƒ SXd S(	   s>  Compatibility function to provide inspect.getfullargspec in Python 2

    This should be rewritten using a backport of Python 3 signature
    once we drop support for Python 2.6. We went for a simpler
    approach at the time of writing because signature uses OrderedDict
    which is not available in Python 2.6.
    t   argst   varargst   varkwt   defaultst
   kwonlyargst   kwonlydefaultst   annotationsN(
   R   t   getfullargspect   AttributeErrort
   getargspect   full_argspec_typeRN   RO   t   keywordsRQ   R   (   R   t   arg_spec(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyRU   ¥   s    			c         C   s5   | t  r d n d  } t j | Œ  } d j |  | ƒ S(   s.   Helper function to output a function signaturei   i   s   {}{}(   R   R   t   formatargspect   format(   t   function_nameRZ   t   arg_spec_for_formatt   arg_spec_str(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   _signature_strº   s    c         C   sK   d } t  | ƒ d d !} d j d „  | j ƒ  Dƒ ƒ } | j |  | | ƒ S(   s)   Helper function to output a function calls   {0}({1}, {2})i   iÿÿÿÿs   , c         s   s%   |  ] \ } } d  | | f Vq d S(   s   %s=%sN(    (   t   .0t   kt   v(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pys	   <genexpr>É   s   (   R   R   t   itemsR\   (   R]   RN   t   kwargst   template_strt   args_strt
   kwargs_str(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   _function_called_strÄ   s    c      	      sI  t  | ƒ } t | t ƒ r: t d | t | ƒ f ƒ ‚ n  t j |  ƒ rŒ t j |  ƒ rŒ | rz t j	 d |  d d ƒn  i | d 6| d 6St
 |  ƒ ‰  ˆ  j ˆ  j } ˆ  j p´ d } ˆ  j ræ | t ‡  f d †  ˆ  j Dƒ ƒ } n  ˆ  j } ˆ  j } t j |  ƒ r|  j g | } n  t |  d t ƒ\ } }	 t ƒ  }
 d	 } xt | ƒ D]ó \ } } | t | ƒ k  r½| ˆ  j k rŒ| | |
 | <qAt d
 | t |	 ˆ  ƒ t |	 | | ƒ f ƒ ‚ qN| t | ƒ } | | k rê| | |
 | <qNy | | |
 | <WqNt t f k
 r@t d t |	 ˆ  ƒ t |	 | | ƒ f ƒ ‚ qNXqNWt ƒ  } xh t | j ƒ  ƒ D]T \ } } | |
 k r†| |
 | <qa| d k	 rŸ| | | <qat d |	 | f ƒ ‚ qaW| d k	 rÒ| |
 d <n  | d k	 rù| | d } | |
 d <n  xI | D]A } | |
 k r"|
 j  | ƒ q t d | t |	 ˆ  ƒ f ƒ ‚ q W|
 S(   s™   Filters the given args and kwargs using a list of arguments to
        ignore, and a function specification.

        Parameters
        ----------
        func: callable
            Function giving the argument specification
        ignore_lst: list of strings
            List of arguments to ignore (either a name of an argument
            in the function spec, or '*', or '**')
        *args: list
            Positional arguments passed to the function.
        **kwargs: dict
            Keyword arguments passed to the function

        Returns
        -------
        filtered_args: list
            List of filtered positional and keyword arguments.
    sH   ignore_lst must be a list of parameters to ignore %s (type %s) was givens4   Cannot inspect object %s, ignore list will not work.t
   stackleveli   t   *s   **c         3   s+   |  ]! } | ˆ  j  k r ˆ  j  | Vq d  S(   N(   RS   (   Ra   Rb   (   RZ   (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pys	   <genexpr>ô   s   RF   iÿÿÿÿsZ   Keyword-only parameter '%s' was passed as positional parameter for %s:
     %s was called.s5   Wrong number of arguments for %s:
     %s was called.sA   Ignore list for %s() contains an unexpected keyword argument '%s'i   s9   Ignore list: argument '%s' is not defined for function %s(    N(!   R   t
   isinstanceR   t
   ValueErrort   typeR   RC   t
   isfunctiont   warningst   warnRU   RN   RR   RQ   RS   t   tupleRO   RP   t   __self__RM   t   Falset   dictt	   enumeratet   lenR`   Ri   t
   IndexErrort   KeyErrort   sortedRd   R   R=   t   pop(   R   t
   ignore_lstRN   Re   t	   arg_namest   arg_defaultst   arg_varargst	   arg_varkwt   _RE   t   arg_dictt   arg_positiont   arg_namet   positiont	   varkwargst	   arg_valueRO   t   item(    (   RZ   sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   filter_argsÏ   s|     					!	c         C   s9   t  |  d d ƒ} t | ƒ d k r5 d | d  } n  | S(   Nt   indenti   iÜ  s   %s...i¼  (   R   Rw   (   t   argt   formatted_arg(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   _format_argA  s    c         O   s  t  |  ƒ \ } } g  | D] } | r | ^ q } | rV | j | ƒ d j | ƒ } n | } t ƒ  } d } xL | D]D }	 t |	 ƒ }
 | d k r d |
 }
 n  t |
 ƒ } | j |
 ƒ qr W| j g  | j ƒ  D]" \ } } d | t | ƒ f ^ qÍ ƒ d j | ƒ } d | | f } | | f S(   NR6   i    iP   s   
%ss   %s=%ss   , s   %s(%s)(   RM   RD   R   R   R   Rw   t   extendRd   (   R   RN   Re   RH   RE   t   mt   module_patht   arg_strt   previous_lengthR‹   RŒ   Rc   t   it	   signature(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   format_signatureH  s$    	<t   Memoryc         C   s6   t  |  | | Ž \ } } d d d | | | f } | S(   si    Returns a nicely formatted statement displaying the function
        call with the given arguments.
    s   %s
[%s] Calling %s...
%siP   R   (   R•   (   R   RN   Re   t   object_nameR   R”   t   msg(    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   format_call`  s    (    (   t   __doc__t	   itertoolsR    R   Rp   R   R
   t   collectionst   _compatR   t   loggerR   t   _memory_helpersR   R   t   full_argspec_fieldst
   namedtupleRX   R$   R0   t   TrueRM   RU   R`   Ri   Ru   R‰   R   R•   R™   (    (    (    sD   lib/python2.7/site-packages/sklearn/externals/joblib/func_inspect.pyt   <module>   s,   	8	E		
	r		