
dUc           @   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 l Z d d l Z d d l Z d d l	 Z	 d d l
 m Z m Z m Z m Z e j d  Z e j d  Z e e f Z d Z y e   e Z Wn9 e k
 ry d d l m Z Wqe k
 rqXn Xd e d	 d
 d d d  Z d   Z d   Z d d  Z d   Z d   Z  d   Z! d   Z" e Z# d   Z$ d   Z% d d  Z& d   Z' d   Z( d d  Z) d   Z* e+ e* _, d   Z- e+ e- _, d   Z. d   Z/ d   Z0 d    Z1 d! e2 f d"     YZ3 d#   Z4 d$   Z5 d% d&  Z6 d'   Z7 e8 d( k rZd d l9 Z9 e9 j:   n  d S()   s7   Utility functions and classes used by nose internally.
iN(   t	   ClassTypet   TypeTypet   isgeneratort   ismethodt   noses   ^[A-Za-z_][A-Za-z0-9_.]*$sE   (?:\.svn)|(?:[^.]+\.py[co])|(?:.*~)|(?:.*\$py\.class)|(?:__pycache__)(   t   Sett    s   |-- s   |   s   `-- s       c         C   s"   d j  t |  | | | | |   S(   Ns   
(   t   joint   _ls_tree_lines(   t   dir_patht   skip_patternt   indentt   branch_indentt   last_indentt   last_branch_indent(    (    s(   lib/python2.7/site-packages/nose/util.pyt   ls_tree   s    c         #   s   d k r t  j    n  g  } t  j   } | j   g  g  } }	 xd | D]\ }
 t j  |
  rl qN n  t  j j t  j j  |
   r | j	 |
  qN |	 j	 |
  qN Wt
 t j g  |	 D] }
 |
 t f ^ q g  | D] }
 |
 t f ^ q   }        f d   } x= | d  D]1 \ }
 } x" | |
 |     D] } | VqCWq!W| r| d \ }
 } x% | |
 |    D] } | VqWn  d  S(   NR   c         3   s~   | s | |  Vnh t  j j  |   } t  j j |  sz | |  Vt |        } x | D] } | | Vqd Wn  d  S(   N(   t   ost   pathR   t   islinkR   (   t   namet   is_dirt   indt
   branch_indR   t   subtreet   x(   R   R	   R   R   R   R
   (    s(   lib/python2.7/site-packages/nose/util.pyt   ls_entry>   s    		i(   R   t   getcwdt   listdirt   sortt   ret   matchR   t   isdirR   t   appendt   listt	   itertoolst   chaint   Falset   True(   R	   R
   R   R   R   R   t   linest   namest   dirst   nondirsR   t   entriesR   R   t   line(    (   R   R	   R   R   R   R
   s(   lib/python2.7/site-packages/nose/util.pyR   )   s,    
!%%c         C   so   t  j j |   sH t  j j t  j j t  j j t  j   |     }  n  |  d k sg t  j j |   rk d S|  S(   sU   Return absolute, normalized path to directory, if it exists; None
    otherwise.
    N(	   R   R   t   isabst   normpatht   abspathR   R   t   NoneR   (   R   (    (    s(   lib/python2.7/site-packages/nose/util.pyt   absdirS   s    $c         C   s  |  } | d k r! t j   } n  t | t  s? t | t  rs x- | D]% } t |  |  } | d k	 rF | SqF Wd St j j |   s t j j	 t j j
 t j j | |     }  n  |  d k s t j j |   r| t j   k rt j j	 t j j
 t j j t j   |    }  qn  |  d k s>t j j |   rBd St j j |   rt j j |  d  } t j j |  r| Sn t j j |   r|  Sd S(   s   Return absolute, normalized path to file (optionally in directory
    where), or None if the file can't be found either in where or the current
    working directory.
    s   __init__.pyN(   R/   R   R   t
   isinstanceR!   t   tuplet   absfileR   R,   R-   R.   R   t   existsR   t   isfile(   R   t   wheret   origt
   maybe_patht	   maybe_abst   init(    (    s(   lib/python2.7/site-packages/nose/util.pyR3   _   s0    0$c         C   s%   x | D] } |  |  r t  Sq Wt S(   N(   R%   R$   (   t	   predicatet   iterablet   item(    (    s(   lib/python2.7/site-packages/nose/util.pyt   anyp   s    c         C   sQ   t  j j |   pP t  j j |   pP |  j d  pP t j t  j j |   d  S(   s   A name is file-like if it is a path that exists, or it has a
    directory part, or it ends in .py, or it isn't a legal python
    identifier.
    s   .pyi    (   R   R   R4   t   dirnamet   endswitht   ident_reR   t   splitext(   R   (    (    s(   lib/python2.7/site-packages/nose/util.pyt	   file_like   s    c         C   sF   y |  j  SWn4 t k
 rA y |  j j SWqB t k
 r= d SXn Xd S(   sr   Get the line number of a function. First looks for
    compat_co_firstlineno, then func_code.co_first_lineno.
    iN(   t   compat_co_firstlinenot   AttributeErrort	   func_codet   co_firstlineno(   t   func(    (    s(   lib/python2.7/site-packages/nose/util.pyt   func_lineno   s    c         C   s%   t  |   } | t k p$ t | t   S(   s|   Is obj a class? Inspect's isclass is too liberal and returns True
    for objects that can't be subclasses of anything.
    (   t   typet   class_typest
   issubclass(   t   objt   obj_type(    (    s(   lib/python2.7/site-packages/nose/util.pyt   isclass   s    c         C   s   t  j j |   r t  j j |   } t j |  r x3 d D]+ } t  j j t  j j |  |   r: t Sq: Wt	 j
 j d  r t  j j t  j j |  d   r t Sq n  t S(   s   
    Is this path a package directory?

    >>> ispackage('nose')
    True
    >>> ispackage('unit_tests')
    False
    >>> ispackage('nose/plugins')
    True
    >>> ispackage('nose/loader.py')
    False
    s   __init__.pys   __init__.pycs   __init__.pyot   javas   __init__$py.class(   s   __init__.pys   __init__.pycs   __init__.pyo(   R   R   R   t   basenameRA   R   R5   R   R%   t   syst   platformt
   startswithR$   (   R   t   endR:   (    (    s(   lib/python2.7/site-packages/nose/util.pyt	   ispackage   s    !!
c         C   s   t  |   t k S(   s  
    Is this a property?

    >>> class Foo:
    ...     def got(self):
    ...         return 2
    ...     def get(self):
    ...         return 1
    ...     get = property(get)

    >>> isproperty(Foo.got)
    False
    >>> isproperty(Foo.get)
    True
    (   RJ   t   property(   RM   (    (    s(   lib/python2.7/site-packages/nose/util.pyt
   isproperty   s    c         C   s   | d k r t j   } n  t j j | t j j |  j d    } t j j | d  r_ | S| d } t j j |  r | Sd S(   s   Find the python source file for a package, relative to a
    particular directory (defaults to current working directory if not
    given).
    t   .s   /__init__.pys   .pyN(   R/   R   R   R   R   t   sept   splitR4   (   t   packaget
   relativeToR   t   filename(    (    s(   lib/python2.7/site-packages/nose/util.pyt   getfilename   s    *
c         C   s  t  |   } t j j |  s. | j d  r? t |  r? d St j j t j j |   \ } } | d k rx g  } n	 | g } t j j	 t j j	 |  d  \ } } xN | r t t j j
 | |   r | j |  n Pt j j	 |  \ } } q W| j   d j
 |  S(   s  
    Find the full dotted package name for a given python source file
    name. Returns None if the file is not a python source file.

    >>> getpackage('foo.py')
    'foo'
    >>> getpackage('biff/baf.py')
    'baf'
    >>> getpackage('nose/util.py')
    'nose.util'

    Works for directories too.

    >>> getpackage('nose')
    'nose'
    >>> getpackage('nose/plugins')
    'nose.plugins'

    And __init__ files stuck onto directories

    >>> getpackage('nose/plugins/__init__.py')
    'nose.plugins'

    Absolute paths also work.

    >>> path = os.path.abspath(os.path.join('nose', 'plugins'))
    >>> getpackage(path)
    'nose.plugins'
    s   .pyt   __init__i    RY   N(   t   srcR   R   R   R@   RV   R/   RB   RQ   R[   R   R    t   reverse(   R^   t   src_filet   baset   extt	   mod_partsR   t   part(    (    s(   lib/python2.7/site-packages/nose/util.pyt
   getpackage   s    /$		(	
c         C   sj   t  |   d } d | d } d d | |  d | f } d t  |  } | d k rf | d | } n  | S(   s   Draw a 70-char-wide divider, with label in the middle.

    >>> ln('hello there')
    '---------------------------- hello there -----------------------------'
    i   iF   s   %s %s %st   -i    (   t   len(   t   labelt	   label_lent   chunkt   outt   pad(    (    s(   lib/python2.7/site-packages/nose/util.pyt   ln  s    c         C   s   |  j  d  } | } | d k r x[ | r y* t j d |   t d j |   } PWq% t k
 r{ | d =| s|   q| q% Xq% W| d } n  | } t j d | |  | |  x | D] } t | |  } q W| S(   s8  Resolve a dotted name to a module and its parts. This is stolen
    wholesale from unittest.TestLoader.loadTestByName.

    >>> resolve_name('nose.util') #doctest: +ELLIPSIS
    <module 'nose.util' from...>
    >>> resolve_name('nose.util.resolve_name') #doctest: +ELLIPSIS
    <function resolve_name at...>
    RY   s   __import__ %sii   s   resolve: %s, %s, %s, %sN(   R[   R/   t   logt   debugt
   __import__R   t   ImportErrort   getattr(   R   t   modulet   partst
   parts_copyRM   Rg   (    (    s(   lib/python2.7/site-packages/nose/util.pyt   resolve_name)  s$    		c         C   s  t  j j } |  } d } d |  k rS t |   rC | |   d d f Sd |  d f Sn  t  j j |   \ } } | sy5 |  j d  \ } } t |  r |  d } } n  Wqgt k
 r|  j d  } t | d  d k r d j | d   | d } } qt d |  f   qgXnQ | s%|  } nB d | k rI| j d  \ } } n | } t  j	 j | | g  } | rt |  r| |  d | f Sd | | f Sn d d | f Sd S(   s3  Split a test name into a 3-tuple containing file, module, and callable
    names, any of which (but not all) may be blank.

    Test names are in the form:

    file_or_module:callable

    Either side of the : may be dotted. To change the splitting behavior, you
    can alter nose.util.split_test_re.
    t   :i    i   isa   Test name '%s' could not be parsed. Please format test names as path:callable or module:callable.N(
   R   R   R-   R/   RC   R[   t
   ValueErrorRj   R   RZ   (   t   testt   normt   file_or_modt   fnt   headt   tailRw   t	   file_part(    (    s(   lib/python2.7/site-packages/nose/util.pyt   split_test_nameF  s<    !	c         C   s  t  |  d  r |  j   St |   } d } } } | t j k ry t |  d d  } t |  d d  } t |  | | f S| t j k s t	 | t  s | t j
 k r,t |  d d  } | d k	 rt j | } t | d d  } | d k	 rt j j |  } qn  t |  d d  } t |  | | f S| t j k rvt |  j  } t | d  | d d | d |  j f f St |  t j  r:t  |  d	  st  |  d
  ry t |  j  SWqt k
 rt |  j  SXn  t |  j  } y |  j } Wn t k
 r|  j } n Xt | d  | d d | d | f f St  |  d  rh|  j j d k rht |  j  St d |  | f   d S(   sf   Find the test address for a test, which may be a module, filename,
    class, method or function.
    t   addresst   __file__t   __name__t
   __module__i    i   s   %s.%si   t   _FunctionTestCase__testFunct	   _testFunct	   __class__t   __builtin__t   builtinss   I don't know what %s is (%s)N(   R   R   (   t   hasattrR   RJ   R/   t   typest
   ModuleTypeRu   Ra   t   FunctionTypeRL   R    RR   t   modulesR   R   R.   t
   MethodTypet   test_addresst   im_classR   R1   t   unittestt   TestCaseR   RE   R   R   t   _TestCase__testMethodNamet   _testMethodNameR   t	   TypeError(   R|   t   tt   fileRv   t   callt   mt   cls_adrt   method_name(    (    s(   lib/python2.7/site-packages/nose/util.pyR     sN    
-c         C   sB  x;| D]3} t  |  | d  } | d k	 r t |   t j k r t | t j  rp t j |  \ } } } } n t	 | d  r t j
 |  r | j } n  y, t j |  \ } } } } | j d  Wn' t k
 r t d | |  f   n Xt |  r t j d |  | |   | |   Sn  t j d |  |  |   Sq Wd S(   s   Given a list of possible method names, try to run them with the
    provided object. Keep going until something works. Used to run
    setup/teardown methods for module, package, and function tests.
    t   __call__i    sa   Attribute %s of %r is not a python function. Only functions or callables may be used as fixtures.s   call fixture %s.%s(%s)s   call fixture %s.%sN(   Ru   R/   RJ   R   R   R1   R   t   inspectt
   getargspecR   R   R   t   popR   Rj   Rq   Rr   (   RM   R'   R   RH   t   argst   varargst   varkwt   defaults(    (    s(   lib/python2.7/site-packages/nose/util.pyt   try_run  s&    c         C   s   |  d	 k r |  St j j d  rH |  j d  rH d j |  d  d f  St j j |   \ } } | d
 k r d j | d f  S|  S(   s   Find the python source file for a .pyc, .pyo or $py.class file on
    jython. Returns the filename provided if it is not a python source
    file.
    RP   s	   $py.classRY   it   pys   .pycs   .pyos   .pyN(   s   .pycs   .pyos   .py(	   R/   RR   RS   RT   R@   R   R   R   RB   (   R^   Rd   Re   (    (    s(   lib/python2.7/site-packages/nose/util.pyRa     s    !c            s     f d   } | S(   s  Sort key function factory that puts items that match a
    regular expression last.

    >>> from nose.config import Config
    >>> from nose.pyversion import sort_list
    >>> c = Config()
    >>> regex = c.testMatch
    >>> entries = ['.', '..', 'a_test', 'src', 'lib', 'test', 'foo.py']
    >>> sort_list(entries, regex_last_key(regex))
    >>> entries
    ['.', '..', 'foo.py', 'lib', 'src', 'a_test', 'test']
    c            s#     j  |   r d |  f Sd |  f S(   Ni   i    (   t   search(   RM   (   t   regex(    s(   lib/python2.7/site-packages/nose/util.pyt   k  s    
(    (   R   R   (    (   R   s(   lib/python2.7/site-packages/nose/util.pyt   regex_last_key  s    c         C   sl   |  d k r d Sy |  j g   |  SWn t k
 r8 n Xy t j d |   SWn t k
 rg t |   SXd S(   sw  Convert a value that may be a list or a (possibly comma-separated)
    string into a list. The exception: None is returned as None, not [None].

    >>> tolist(["one", "two"])
    ['one', 'two']
    >>> tolist("hello")
    ['hello']
    >>> tolist("separate,values, with, commas,  spaces , are    ,ok")
    ['separate', 'values', 'with', 'commas', 'spaces', 'are', 'ok']
    s   \s*,\s*N(   R/   t   extendRE   R   R[   R   R!   (   t   val(    (    s(   lib/python2.7/site-packages/nose/util.pyt   tolist  s    t   odictc           B   st   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d d	  Z d
   Z d   Z RS(   sv   Simple ordered dict implementation, based on:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
    c         O   s&   g  |  _  t t |   j | |   d  S(   N(   t   _keyst   superR   R`   (   t   selft   argt   kw(    (    s(   lib/python2.7/site-packages/nose/util.pyR`     s    	c         C   s*   t  t |   j |  |  j j |  d  S(   N(   R   R   t   __delitem__R   t   remove(   R   t   key(    (    s(   lib/python2.7/site-packages/nose/util.pyR   !  s    c         C   s?   t  t |   j | |  | |  j k r; |  j j |  n  d  S(   N(   R   R   t   __setitem__R   R    (   R   R   R=   (    (    s(   lib/python2.7/site-packages/nose/util.pyR   %  s    c         C   s:   d d j  g  |  j   D] \ } } d | | f ^ q  S(   Ns   {%s}s   , s   %r: %r(   R   t   items(   R   R   t   v(    (    s(   lib/python2.7/site-packages/nose/util.pyt   __str__*  s    c         C   s    t  t |   j   g  |  _ d  S(   N(   R   R   t   clearR   (   R   (    (    s(   lib/python2.7/site-packages/nose/util.pyR   -  s    c         C   s&   t  t |   j   } |  j | _ | S(   N(   R   R   t   copyR   (   R   t   d(    (    s(   lib/python2.7/site-packages/nose/util.pyR   1  s    c         C   s   t  |  j |  j    S(   N(   t   zipR   t   values(   R   (    (    s(   lib/python2.7/site-packages/nose/util.pyR   6  s    c         C   s   |  j  S(   N(   R   (   R   (    (    s(   lib/python2.7/site-packages/nose/util.pyt   keys9  s    c         C   sA   t  t |   j | |  } | |  j k r= |  j j |  n  | S(   N(   R   R   t
   setdefaultR   R    (   R   R   t   failobjR=   (    (    s(   lib/python2.7/site-packages/nose/util.pyR   <  s    c         C   sS   t  t |   j |  x6 | j   D]( } | |  j k r# |  j j |  q# q# Wd  S(   N(   R   R   t   updateR   R   R    (   R   t   dictR   (    (    s(   lib/python2.7/site-packages/nose/util.pyR   B  s    c         C   s   t  |  j |  j  S(   N(   t   mapt   getR   (   R   (    (    s(   lib/python2.7/site-packages/nose/util.pyR   H  s    N(   R   R   t   __doc__R`   R   R   R   R   R   R   R   R/   R   R   R   (    (    (    s(   lib/python2.7/site-packages/nose/util.pyR     s   									c            s\   d d l  m } t    r.   f d   } n   f d   } |    |  } | | _ | S(   s  
    Make a function imported from module A appear as if it is located
    in module B.

    >>> from pprint import pprint
    >>> pprint.__module__
    'pprint'
    >>> pp = transplant_func(pprint, __name__)
    >>> pp.__module__
    'nose.util'

    The original function is not modified.

    >>> pprint.__module__
    'pprint'

    Calling the transplanted function calls the original.

    >>> pp([1, 2])
    [1, 2]
    >>> pprint([1,2])
    [1, 2]

    i(   t   make_decoratorc          ?   s#   x   |  |   D] } | Vq Wd  S(   N(    (   R   R   R   (   RH   (    s(   lib/python2.7/site-packages/nose/util.pyt   newfuncg  s    c             s     |  |   S(   N(    (   R   R   (   RH   (    s(   lib/python2.7/site-packages/nose/util.pyR   k  s    (   t
   nose.toolsR   R   R   (   RH   Rv   R   R   (    (   RH   s(   lib/python2.7/site-packages/nose/util.pyt   transplant_funcL  s    	c         C   s/   d |  f d     Y} | | _  |  j | _ | S(   sB  
    Make a class appear to reside in `module`, rather than the module in which
    it is actually defined.

    >>> from nose.failure import Failure
    >>> Failure.__module__
    'nose.failure'
    >>> Nf = transplant_class(Failure, __name__)
    >>> Nf.__module__
    'nose.util'
    >>> Nf.__name__
    'Failure'

    t   Cc           B   s   e  Z RS(    (   R   R   (    (    (    s(   lib/python2.7/site-packages/nose/util.pyR     s   (   R   R   (   t   clsRv   R   (    (    s(   lib/python2.7/site-packages/nose/util.pyt   transplant_classs  s    	s   utf-8c         C   sn   y t  |   SWnY t k
 ri t |  t  rV d j g  |  D] } t | |  ^ q:  St |   j |  SXd  S(   Nt    (   t   strt   UnicodeEncodeErrorR1   t	   ExceptionR   t   safe_strt   unicodet   encode(   R   t   encodingR   (    (    s(   lib/python2.7/site-packages/nose/util.pyR     s    	 c         C   sG   t  j j |   s t St  j |   } t | j t j t j Bt j	 B@ S(   N(
   R   R   R4   R$   t   statt   boolt   st_modet   S_IXUSRt   S_IXGRPt   S_IXOTH(   R   t   st(    (    s(   lib/python2.7/site-packages/nose/util.pyt   is_executable  s    t   __main__(;   R   R   R"   t   loggingR   R   R   RR   R   R   t   nose.pyversionR    R   R   R   t	   getLoggerRq   t   compileRA   RK   R
   t   sett	   NameErrort   setsR   Rt   R   R   R0   R/   R3   R>   RC   RI   RO   t   is_generatorRV   RX   R_   Rh   Rp   Ry   R   R$   t   __test__R   R   Ra   R   R   R   R   R   R   R   R   R   t   doctestt   testmod(    (    (    s(   lib/python2.7/site-packages/nose/util.pyt   <module>   sp   "
	*	!								1		<		1		!			4	'	
	