
    IR-e;                         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mZ ddlm	Z	 ddl
mZ g dZdgZej        dd         d	k    rdd
lmZ nd Zd Z eddd          dd            ZddZd ZddZd Zd ZdS )z2Functions related to Python runtime introspection.    N)metadata)Version)deprecated_renamed_argument)resolve_name
minversionfind_current_moduleisinstancemethodr      )   
   )packages_distributionsc                  "   t          j        t                    } t          j                    D ]T}|                    d          pd                                D ](}| |                             |j        d                    )Ut          |           S )z
        Return a mapping of top-level packages to their distributions.
        Note: copied from https://github.com/python/importlib_metadata/pull/287.
        ztop_level.txt Name)	collectionsdefaultdictlistr   distributions	read_textsplitappenddict)pkg_to_distdistpkgs      ;lib/python3.11/site-packages/astropy/utils/introspection.pyr   r      s    
 "-d33*,, 	? 	?D77=2DDFF ? ?C ''f(=>>>>?K       c                 $   d                     |          }|r| dz   |z   } |                     d          }t          |          dk    rd}g }nt          |          dz
  }|d         g}|d|         }|dk    r]	 t          d                     |          |          }n7# t          $ r$ |dk    r |dz  }|d|         }||         g}d}Y nw xY w|dk    ]||d         D ]0}	 t          ||          }# t          $ r t	          |           w xY w|S )a!  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.
    .   Nr   )fromlistr   )joinr   len
__import__ImportErrorgetattrAttributeError)nameadditional_partspartscursorr"   module_nameretparts           r   r   r   $   si   > xx 011 -cz,,JJsOOE
5zzQUa"I;.K
1**		SXXk22XFFFC 	 	 	{{aKF.KfHCCC		 1** fgg $ $	$#t$$CC 	$ 	$ 	$d###	$ Js   9$B +CC"C33Dversion_pathz5.0T__version__c                 V   t          | t          j                  r| j        }t	          | dd          }nZt          | t
                    r&| }d}	 t          |          } n0# t          $ r Y dS w xY wt          dt          |                      |Y	 t          j        |          }nC# t          j        $ r1 t                      }t          j        ||         d                   }Y nw xY w|r t          |          t          |          k    S t          |          t          |          k    S )aa  
    Returns `True` if the specified Python module satisfies a minimum version
    requirement, and `False` if not.

    .. deprecated::
        ``version_path`` is not used anymore and is deprecated in
        ``astropy`` 5.0.

    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`).

    Examples
    --------
    >>> import astropy
    >>> minversion(astropy, '0.4.4')
    True
    r1   NFzYmodule argument must be an actual imported module, or the import name of the module; got r   )
isinstancetypes
ModuleType__name__r'   strr   r&   
ValueErrorreprr   versionPackageNotFoundErrorr   r   )moduler:   	inclusiver0   r-   module_version
dist_namess          r   r   r   i   s^   > &%*++ 
o ==	FC	 	  
	!+..FF 	 	 	55	 "<<" "
 
 	
 	J%-k::NN, 	J 	J 	J
 011J%-j.Ea.HIINNN	J  :~&&''*:*:::~&&)9)999s$   A 
A,+A,B& &=C&%C&r    Fc                 ,   t          j                    }t          |           D ]}|j        }| dS |rt	          |          }|du r|g}ng }|D ]}t          j        |          r|                    |           ,t          |t                    r(|                    t          j
        |                     i|du r|                    |           t          d          |r"|j        }t	          |          }||vr|S |}| dS dS t	          |          S )a 
  
    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

    NTzinvalid entry in finddiff)inspectcurrentframerangef_back_get_module_from_frameismoduler   r3   r7   	importlibimport_moduler8   )	depthfinddifffrmicurrmoddiffmodsfdfrmbmodbs	            r   r   r      si   Z 

 
 C5\\  j;44   +(--tyHHH B B#B'' BOOB''''C(( BOOI$;B$?$?@@@@4ZZOOG,,,,$%@AAA 	:D)$//D8##C  	 	 	 	 	 &c***r   c                    t          j        |           }||S d| j        v rd| j        v r| j        d         }|dd                                         dv r|dd         dz   }t          j                            t          j                            |                    }|t           j        v r/t          j
                            t           j        |                   S |                                t           j        v rAt          j
                            t           j        |                                                   S dS )aU  Uses inspect.getmodule() to get the module that the current frame's
    code is running in.

    However, this does not work reliably for code imported from a zip file,
    so this provides a fallback mechanism for that case which is less
    reliable in general, but more reliable than inspect.getmodule() for this
    particular case.
    N__file__r6   )z.pycz.pyoz.py)rA   	getmodule	f_globalslowerospathrealpathabspathmodulesbyfilesysmodulesget)rK   modfilenames      r   rE   rE     s    
C
 
 C

 S]""zS]'B'B=, BCC=  $444}u,H7##BGOOH$=$=>>w,,,;??7#8#BCCC >>w444;??7#89I9I#JKKK
 4r   c                   	
 t          |           	t          	d          r	fd	j        D             }n	fdt          	          D             }t          j        fd|D             }fd|D             }g }t          ||          D ]f\  }}t          |d          r6t          |d          r&|                    |j        dz   |j	        z              K|                    | dz   |z              grfd	u r| gfd
|D             

fdt          |          D             }
fdt          |          D             }
fdt          |          D             }|||fS )a  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)

    __all__c                 .    g | ]}|j         |         fS  __dict__.0kr`   s     r   
<listcomp>z!find_mod_objs.<locals>.<listcomp>e  s$    >>>QQQ(>>>r   c                 F    g | ]}|d          dk    |j         |         fS )r   _rf   rh   s     r   rk   z!find_mod_objs.<locals>.<listcomp>g  s-    JJJQadckkQQ(kkkr   c                 0    g | ]\  }} |          |S re   re   ri   rj   vrF   s      r   rk   z!find_mod_objs.<locals>.<listcomp>k  s*    <<<1<!<<<r   c                 0    g | ]\  }} |          |S re   re   ro   s      r   rk   z!find_mod_objs.<locals>.<listcomp>l  s*    666$!Q((1++6A666r   
__module__r6   r   Tc                 H    g | ]t          fd D                       S )c              3   B   K   | ]}                     |          V  d S )N)
startswith)ri   nmfqns     r   	<genexpr>z+find_mod_objs.<locals>.<listcomp>.<genexpr>y  s/      >>RcnnR((>>>>>>r   )any)ri   rw   
onlylocalss    @r   rk   z!find_mod_objs.<locals>.<listcomp>y  s7    RRR3#>>>>:>>>>>RRRr   c                 *    g | ]\  }}|         |S re   re   ri   rL   evalidss      r   rk   z!find_mod_objs.<locals>.<listcomp>z  s&    GGGDAqVAYGaGGGr   c                 *    g | ]\  }}|         |S re   re   r|   s      r   rk   z!find_mod_objs.<locals>.<listcomp>{  s&    AAAAvayA1AAAr   c                 *    g | ]\  }}|         |S re   re   r|   s      r   rk   z!find_mod_objs.<locals>.<listcomp>|  s&    ;;;da;;;;r   )r   hasattrrc   dirrA   rF   zipr   rr   r6   	enumerate)modnamerz   pkgitems
localnamesobjsfqnamesobjlnmrF   r`   r~   s    `      @@@r   find_mod_objsr   @  s   D w

CsI K>>>>#+>>>JJJJ#c((JJJ H<<<<<<<J6666(666D Gj)) 0 0S3%% 	0'#z*B*B 	0NN3>C/#,>????NN7S=3.//// <!JRRRR'RRRGGGGIj$9$9GGG
AAAA7!3!3AAA;;;;ioo;;;w$$r   c                 "    t          | |          S )a  
    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
    --------
    >>> class MetaClass(type):
    ...     def a_classmethod(cls): pass
    ...
    >>> class MyClass(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
    )_isinstancemethod)clsr   s     r   r	   r	     s    P S#&&&r   c                     t          |t          j                  sdS |j        }|                                 D ].}||j        v r#t          |j        |         t                     c S /t          |          )NF)r3   r4   FunctionTyper6   mrorg   staticmethodr(   )r   r   r)   baseclss       r   r   r     s    c5-.. u <D7799 H H7###!'"24"8,GGGGGG $
 

r   )Tr1   )r    F)F)__doc__r   rG   rA   rX   r]   r4   r   packaging.versionr   astropy.utils.decoratorsr   rc   __doctest_skip__version_infoimportlib.metadatar   r   r   r   rE   r   r	   r   re   r   r   <module>r      sw   8 8          				 



        % % % % % % @ @ @ @ @ @
S
S
S)* BQB7""9999999	! 	! 	!B B BJ ^T599=: =: =: :9=:@j+ j+ j+ j+Z& & &R>% >% >% >%F(' (' ('V    r   