ó
šßÈ[c           @` s÷   d  Z  d d l m Z m Z m Z m 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 m Z d d d	 d
 g Z d	 g Z d „  Z e d d „ Z d e d „ Z e d „ Z d „  Z e j sê d „  Z n	 d „  Z d S(   u2   Functions related to Python runtime introspection.i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   six(   t   ranget   zipu   resolve_nameu
   minversionu   find_current_moduleu   isinstancemethodc         G` sh  d j  | ƒ } | r& |  d | }  n  g  |  j d ƒ D] } t | ƒ ^ q6 } t | ƒ d k ro d } g  } n t | ƒ d } | d g } | |  } xƒ | d k ry& t t d j  | ƒ ƒ d | ƒ} PWq™ t k
 r| d k rí ‚  n  | d 8} | |  } | | g } d } q™ Xq™ WxE | | D]9 } y t | | ƒ } Wq't k
 r_t |  ƒ ‚ q'Xq'W| S(   u#  Resolve a name like ``module.object`` to an object and return it.

    This ends up working like ``from module import object`` but is easier
    to deal with than the `__import__` builtin and supports digging into
    submodules.

    Parameters
    ----------

    name : `str`
        A dotted path to a Python object--that is, the name of a function,
        class, or other object in a module with the full path to that module,
        including parent modules, separated by dots.  Also known as the fully
        qualified name of the object.

    additional_parts : iterable, optional
        If more than one positional arguments are given, those arguments are
        automatically dotted together with ``name``.

    Examples
    --------

    >>> resolve_name('astropy.utils.introspection.resolve_name')
    <function resolve_name at 0x...>
    >>> resolve_name('astropy', 'utils', 'introspection', 'resolve_name')
    <function resolve_name at 0x...>

    Raises
    ------
    `ImportError`
        If the module or named object is not found.
    u   .i   iÿÿÿÿi    t   fromlistu    (   t   joint   splitt   strt   lent
   __import__t   ImportErrort   getattrt   AttributeError(   t   namet   additional_partst   partt   partst   cursorR   t   module_namet   ret(    (    s:   lib/python2.7/site-packages/astropy/utils/introspection.pyt   resolve_name   s6    "(	
!

u   __version__c   	      C` sA  t  |  t j ƒ r |  j } nU t  |  t j ƒ r^ |  } y t | ƒ }  Wqs t k
 rZ t SXn t	 d j
 |  ƒ ƒ ‚ d | k r‘ t |  | ƒ } n t |  j | ƒ } y d d l m } WnQ t k
 r
d d l m } d } t j | | ƒ } | r| j d ƒ } qn X| r'| | ƒ | | ƒ k S| | ƒ | | ƒ k Sd S(   u^  
    Returns `True` if the specified Python module satisfies a minimum version
    requirement, and `False` if not.

    By default this uses `pkg_resources.parse_version` to do the version
    comparison if available.  Otherwise it falls back on
    `distutils.version.LooseVersion`.

    Parameters
    ----------

    module : module or `str`
        An imported module of which to check the version, or the name of
        that module (in which case an import of that module is attempted--
        if this fails `False` is returned).

    version : `str`
        The version as a string that this module must have at a minimum (e.g.
        ``'0.12'``).

    inclusive : `bool`
        The specified version meets the requirement inclusively (i.e. ``>=``)
        as opposed to strictly greater than (default: `True`).

    version_path : `str`
        A dotted attribute path to follow in the module for the version.
        Defaults to just ``'__version__'``, which should work for most Python
        modules.

    Examples
    --------

    >>> import astropy
    >>> minversion(astropy, '0.4.4')
    True
    u^   module argument must be an actual imported module, or the import name of the module; got {0!r}u   .i    (   t   parse_version(   t   LooseVersionu*   ^([1-9]\d*!)?(0|[1-9]\d*)(\.(0|[1-9]\d*))*N(   t
   isinstancet   typest
   ModuleTypet   __name__R   t   string_typesR   R   t   Falset
   ValueErrort   formatR   t   pkg_resourcesR   t   distutils.versionR   t   ret   matcht   group(	   t   modulet   versiont	   inclusivet   version_pathR   t   have_versionR   t   exprt   m(    (    s:   lib/python2.7/site-packages/astropy/utils/introspection.pyt
   minversionc   s0    &	i   c   	      C` sA  t  j ƒ  } x- t |  ƒ D] } | j } | d k r d Sq W| r0t  j | ƒ } | t k ri | g } n‰ g  } x€ | D]x } t  j | ƒ r› | j | ƒ qv t	 | t
 j ƒ rÆ | j t j | ƒ ƒ qv | t k râ | j | ƒ qv t d ƒ ‚ qv WxH | r,| j } t  j | ƒ } | | k r#| S| } qõ Wn t  j | ƒ Sd S(   u 
  
    Determines the module/package from which this function is called.

    This function has two modes, determined by the ``finddiff`` option. it
    will either simply go the requested number of frames up the call
    stack (if ``finddiff`` is False), or it will go up the call stack until
    it reaches a module that is *not* in a specified set.

    Parameters
    ----------
    depth : int
        Specifies how far back to go in the call stack (0-indexed, so that
        passing in 0 gives back `astropy.utils.misc`).
    finddiff : bool or list
        If False, the returned ``mod`` will just be ``depth`` frames up from
        the current frame. Otherwise, the function will start at a frame
        ``depth`` up from current, and continue up the call stack to the
        first module that is *different* from those in the provided list.
        In this case, ``finddiff`` can be a list of modules or modules
        names. Alternatively, it can be True, which will use the module
        ``depth`` call stack frames up as the module the returned module
        most be different from.

    Returns
    -------
    mod : module or None
        The module object or None if the package cannot be found. The name of
        the module is available as the ``__name__`` attribute of the returned
        object (if it isn't None).

    Raises
    ------
    ValueError
        If ``finddiff`` is a list with an invalid entry.

    Examples
    --------
    The examples below assume that there are two modules in a package named
    ``pkg``. ``mod1.py``::

        def find1():
            from astropy.utils import find_current_module
            print find_current_module(1).__name__
        def find2():
            from astropy.utils import find_current_module
            cmod = find_current_module(2)
            if cmod is None:
                print 'None'
            else:
                print cmod.__name__
        def find_diff():
            from astropy.utils import find_current_module
            print find_current_module(0,True).__name__

    ``mod2.py``::

        def find():
            from .mod1 import find2
            find2()

    With these modules in place, the following occurs::

        >>> from pkg import mod1, mod2
        >>> from astropy.utils import find_current_module
        >>> mod1.find1()
        pkg.mod1
        >>> mod1.find2()
        None
        >>> mod2.find()
        pkg.mod2
        >>> find_current_module(0)
        <module 'astropy.utils.misc' from 'astropy/utils/misc.py'>
        >>> mod1.find_diff()
        pkg.mod1

    u   invalid entry in finddiffN(   t   inspectt   currentframeR   t   f_backt   Nonet	   getmodulet   Truet   ismodulet   appendR   R   R   t	   importlibt   import_moduleR    (	   t   deptht   finddifft   frmt   it   currmodt   diffmodst   fdt   frmbt   modb(    (    s:   lib/python2.7/site-packages/astropy/utils/introspection.pyt   find_current_module­   s2    N			c         ` sC  t  |  ƒ } t | d ƒ rG g  | j D] } | | j | f ^ q% } n< g  t | ƒ D]) } | d d k rT | | j | f ^ qT } t j } g  | D] \ } } | | ƒ s“ | ^ q“ } g  | D] \ } } | | ƒ s¾ | ^ q¾ } g  }	 xn t | | ƒ D]] \ }
 } t |
 d ƒ r@t |
 d ƒ r@|	 j |
 j	 d |
 j
 ƒ qø |	 j |  d | ƒ qø W| r6| t k rw|  g } n  g  |	 D]" ‰  t ‡  f d †  | Dƒ ƒ ^ q~} g  t | ƒ D] \ } } | | r³| ^ q³} g  t |	 ƒ D] \ } } | | râ| ^ qâ}	 g  t | ƒ D] \ } } | | r| ^ q} n  | |	 | f S(   u   Returns all the public attributes of a module referenced by name.

    .. note::
        The returned list *not* include subpackages or modules of
        ``modname``, nor does it include private attributes (those that
        begin with '_' or are not in `__all__`).

    Parameters
    ----------
    modname : str
        The name of the module to search.
    onlylocals : bool or list of str
        If `True`, only attributes that are either members of ``modname`` OR
        one of its modules or subpackages will be included. If it is a list
        of strings, those specify the possible packages that will be
        considered "local".

    Returns
    -------
    localnames : list of str
        A list of the names of the attributes as they are named in the
        module ``modname`` .
    fqnames : list of str
        A list of the full qualified names of the attributes (e.g.,
        ``astropy.utils.introspection.find_mod_objs``). For attributes that are
        simple variables, this is based on the local name, but for functions or
        classes it can be different if they are actually defined elsewhere and
        just referenced in ``modname``.
    objs : list of objects
        A list of the actual attributes themselves (in the same order as
        the other arguments)

    u   __all__i    u   _u
   __module__u   __name__u   .c         3` s   |  ] } ˆ  j  | ƒ Vq d  S(   N(   t
   startswith(   t   .0t   nm(   t   fqn(    s:   lib/python2.7/site-packages/astropy/utils/introspection.pys	   <genexpr>U  s    (   R   t   hasattrt   __all__t   __dict__t   dirR/   R5   R   R6   t
   __module__R   R4   t   anyt	   enumerate(   t   modnamet
   onlylocalst   modt   kt   pkgitemsR5   t   vt
   localnamest   objst   fqnamest   objt   lnmt   validsR<   t   e(    (   RF   s:   lib/python2.7/site-packages/astropy/utils/introspection.pyt   find_mod_objs  s(    #,<	++///2c         C` s   t  |  | ƒ S(   u*  
    Returns `True` if the given object is an instance method of the class
    it is defined on (as opposed to a `staticmethod` or a `classmethod`).

    This requires both the class the object is a member of as well as the
    object itself in order to make this determination.

    Parameters
    ----------
    cls : `type`
        The class on which this method was defined.
    obj : `object`
        A member of the provided class (the membership is not checked directly,
        but this function will always return `False` if the given object is not
        a member of the given class).

    Examples
    --------
    >>> from astropy.extern import six
    >>> class MetaClass(type):
    ...     def a_classmethod(cls): pass
    ...
    >>> @six.add_metaclass(MetaClass)
    ... class MyClass(object):
    ...     __metaclass__ = MetaClass
    ...     def an_instancemethod(self): pass
    ...     @classmethod
    ...     def another_classmethod(cls): pass
    ...     @staticmethod
    ...     def a_staticmethod(): pass
    ...
    >>> isinstancemethod(MyClass, MyClass.a_classmethod)
    False
    >>> isinstancemethod(MyClass, MyClass.another_classmethod)
    False
    >>> isinstancemethod(MyClass, MyClass.a_staticmethod)
    False
    >>> isinstancemethod(MyClass, MyClass.an_instancemethod)
    True
    (   t   _isinstancemethod(   t   clsRW   (    (    s:   lib/python2.7/site-packages/astropy/utils/introspection.pyt   isinstancemethod_  s    *c         C` sj   t  | t j ƒ s t S| j } x8 |  j ƒ  D]* } | | j k r, t  | j | t ƒ Sq, Wt | ƒ ‚ d  S(   N(	   R   R   t   FunctionTypeR   R   t   mroRI   t   staticmethodR   (   R]   RW   R   t   basecls(    (    s:   lib/python2.7/site-packages/astropy/utils/introspection.pyR\     s    	c         C` s   t  | t j ƒ o | j |  k S(   N(   R   R   t
   MethodTypet   im_class(   R]   RW   (    (    s:   lib/python2.7/site-packages/astropy/utils/introspection.pyR\   ž  s    (   t   __doc__t
   __future__R    R   R   R   R/   R$   R   R7   t   externR   t   extern.six.movesR   R   RH   t   __doctest_skip__R   R4   R.   R   RB   R[   R^   t   PY2R\   (    (    (    s:   lib/python2.7/site-packages/astropy/utils/introspection.pyt   <module>   s$   "				IJnD	-	