a
    ߙfbv                     @   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
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lmZ g dZddgZd	Zd
Zdd Zd@ddZG dd dZejdd Zdd ZG dd dZdAddZdd Zej dkr ddl!Z!dd  Z"nd!d  Z"d"d# Z#dBd%d&Z$G d'd( d(ej%Z&d)d* Z'dCd-d.Z(d/Z)ed0e)G d1d dej*d2Z+ed0e)G d3d de,Z-e. Z/ed4d5 Z0ed6e0Z1d7e1_ d8d9 Z2d:d; Z3d<d= Z4dDd>d?Z5dS )Ezo
A "grab bag" of relatively small general-purpose utilities that don't have
a clear module/package to live in.
    N)contextmanager)defaultdictOrderedDict)
deprecated)
isiterablesilenceformat_exceptionNumpyRNGContextfind_api_pageis_path_hiddenwalk_skip_hiddenJsonCustomEncoderindentdtype_bytes_or_charsOrderedDescriptorOrderedDescriptorContainerr   r   zYFile {} already exists. If you mean to replace it then use the argument "overwrite=True".z[File .* already exists\. If you mean to replace it then use the argument "overwrite=True"\.c                 C   s(   zt |  W dS  ty"   Y dS 0 dS )z/Returns `True` if the given object is iterable.TFN)iter	TypeError)obj r   1lib/python3.9/site-packages/astropy/utils/misc.pyr   .   s
    r         c                    s6   d  fdd|  D }| d dkr2|d7 }|S )zAIndent a block of text.  The indentation is applied to each line.
c                 3   s&   | ]}|rd    | ndV  qdS )  Nr   ).0lshiftwidthr   r   	<genexpr>;   s   zindent.<locals>.<genexpr>)join
splitlines)sr   r    Zindentedr   r   r   r   8   s    r   c                   @   s   e Zd ZdZdd ZdS )
_DummyFilezA noop writeable object.c                 C   s   d S Nr   )selfr%   r   r   r   writeF   s    z_DummyFile.writeN)__name__
__module____qualname____doc__r)   r   r   r   r   r&   C   s   r&   c                  c   s2   t j} t j}t t _t t _dV  | t _|t _dS )z:A context manager that silences sys.stdout and sys.stderr.N)sysstdoutstderrr&   )Z
old_stdoutZ
old_stderrr   r   r   r   J   s    r   c                 O   s^   t jt d dd}t|dkr4|d \}}}}nd } } }}| j|||||d|S )a  
    Given an exception message string, uses new-style formatting arguments
    ``{filename}``, ``{lineno}``, ``{func}`` and/or ``{text}`` to fill in
    information about the exception that occurred.  For example:

        try:
            1/0
        except:
            raise ZeroDivisionError(
                format_except('A divide by zero occurred in {filename} at '
                              'line {lineno} of function {func}.'))

    Any additional positional or keyword arguments passed to this function are
    also used to format the message.

    .. note::
        This uses `sys.exc_info` to gather up the information needed to fill
        in the formatting arguments. Since `sys.exc_info` is not carried
        outside a handled exception, it's not wise to use this
        outside of an ``except`` clause - if it is, this will substitute
        '<unknown>' for the 4 formatting arguments.
       r   )limitr   z	<unknown>)filenamelinenofunctext)	traceback
extract_tbr.   exc_infolenformat)msgargskwargstbr3   r4   r5   r6   r   r   r   r   W   s    r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r	   a  
    A context manager (for use with the ``with`` statement) that will seed the
    numpy random number generator (RNG) to a specific value, and then restore
    the RNG state back to whatever it was before.

    This is primarily intended for use in the astropy testing suit, but it
    may be useful in ensuring reproducibility of Monte Carlo simulations in a
    science context.

    Parameters
    ----------
    seed : int
        The value to use to seed the numpy RNG

    Examples
    --------
    A typical use case might be::

        with NumpyRNGContext(<some seed value you pick>):
            from numpy import random

            randarr = random.randn(100)
            ... run your test using `randarr` ...

        #Any code using numpy.random at this indent level will act just as it
        #would have if it had been before the with statement - e.g. whatever
        #the default seed is.


    c                 C   s
   || _ d S r'   )seed)r(   r@   r   r   r   __init__   s    zNumpyRNGContext.__init__c                 C   s&   ddl m} | | _|| j d S Nr   )random)numpyrC   Z	get_state
startstater@   )r(   rC   r   r   r   	__enter__   s    
zNumpyRNGContext.__enter__c                 C   s   ddl m} || j d S rB   )rD   rC   Z	set_staterE   )r(   exc_type	exc_valuer7   rC   r   r   r   __exit__   s    zNumpyRNGContext.__exit__N)r*   r+   r,   r-   rA   rF   rI   r   r   r   r   r	   y   s   r	   Tc                 C   sV  ddl }ddlm} ddlm} t| tsPt| drPt| drP| jd | j	 } nt
| r`| j	} |du rddlm} |jrd	|j }nd
}d|v r|dr|dd }q|dr|}q|d }n"|d
ks|dkrd}nd| d}|d }dd| i}	||d||	d}
|
 }d}g }tdD ]6}|}|d|d }|||d | d q&|\}}}}d|vrtd|d ||d d }W d   n1 s0    Y  ||d}d}|  D ]R}| }|d }|d }|dr|dd | }|| kr|| } q(q|du rBtd |  n|rR|| |S )!a  
    Determines the URL of the API page for the specified object, and
    optionally open that page in a web browser.

    .. note::
        You must be connected to the internet for this to function even if
        ``openinbrowser`` is `False`, unless you provide a local version of
        the documentation to ``version`` (e.g., ``file:///path/to/docs``).

    Parameters
    ----------
    obj
        The object to open the docs for or its fully-qualified name
        (as a str).
    version : str
        The doc version - either a version number like '0.1', 'dev' for
        the development/latest docs, or a URL to point to a specific
        location that should be the *base* of the documentation. Defaults to
        latest if you are on aren't on a release, otherwise, the version you
        are on.
    openinbrowser : bool
        If `True`, the `webbrowser` package will be used to open the doc
        page in a new web browser window.
    timeout : number, optional
        The number of seconds to wait before timing-out the query to
        the astropy documentation.  If not given, the default python
        stdlib timeout will be used.

    Returns
    -------
    url : str
        The loaded URL

    Raises
    ------
    ValueError
        If the documentation can't be found

    r   N)
decompress)get_readable_fileobjr+   r*   .)versionvZdevz://z
index.htmli/Zlatestzhttp://devdocs.astropy.org/zhttps://docs.astropy.org/en/zobjects.invz
User-AgentzAstropy/Zbinary)encodingZremote_timeoutZhttp_headersr"   r      
r   zutf-8z3The remainder of this file is compressed using zlibzjThe file downloaded from {} does not seem to bethe usual Sphinx objects.inv format.  Maybe it has changed?   $z'Could not find the docs for the object )
webbrowserzlibrJ   Zastropy.utils.datarK   
isinstancestrhasattrr+   r*   inspectZismoduleastropyrM   releaseendswithreadrangeindexappenddecode
ValueErrorr;   stripr$   splitopen)r   rM   ZopeninbrowserZtimeoutrT   rJ   rK   ZbaseurlZurlZheadersZufZoireadidxZheaderlines_ZoldidxZiversZprojZversZcomprZ
compressedZdecompressedZresurlr   ZlsnameZlocr   r   r   r
      sx    (




 
0



r
   c                 C   s$   t dd tj D }|| dS )zx
    Given an OS signal number, returns a signal name.  If the signal
    number is unknown, returns ``'UNKNOWN'``.
    c                 s   s$   | ]\}}| d r||fV  qdS )ZSIGN)
startswith)r   rN   kr   r   r   r!   (  s   

z(signal_number_to_name.<locals>.<genexpr>ZUNKNOWN)dictsignal__dict__itemsget)ZsignumZsignal_to_name_mapr   r   r   signal_number_to_name   s    rp   Zwin32c                 C   sZ   t | tr| t } z&tjj| }t	|d@ o:|dk}W n t
yT   d}Y n0 |S )z
        Returns True if the given filepath has the hidden attribute on
        MS-Windows.  Based on a post here:
        https://stackoverflow.com/questions/284115/cross-platform-hidden-file-detection
        r1   r"   F)rV   bytesra   r.   getfilesystemencodingctypesZwindllZkernel32ZGetFileAttributesWboolAttributeError)filepathattrsresultr   r   r   _has_hidden_attribute1  s    

ry   c                 C   s   dS )NFr   )rv   r   r   r   ry   @  s    c                 C   s@   t jt j| }t|tr*|d}n
|d}|p>t| S )z
    Determines if a given file or directory is hidden.

    Parameters
    ----------
    filepath : str
        The path to a file or directory

    Returns
    -------
    hidden : bool
        Returns `True` if the file is hidden
       .rL   )ospathbasenameabspathrV   rq   ri   ry   )rv   rh   Z	is_dottedr   r   r   r   D  s
    

r   Fc                 c   sZ   t j| d||dD ]B\}}}dd |D |dd< dd |D |dd< |||fV  qdS )a5  
    A wrapper for `os.walk` that skips hidden files and directories.

    This function does not have the parameter ``topdown`` from
    `os.walk`: the directories must always be recursed top-down when
    using this function.

    See also
    --------
    os.walk : For a description of the parameters
    T)topdownonerrorfollowlinksc                 S   s   g | ]}t |s|qS r   r   )r   dr   r   r   
<listcomp>k      z$walk_skip_hidden.<locals>.<listcomp>Nc                 S   s   g | ]}t |s|qS r   r   )r   fr   r   r   r   l  r   )r{   walk)topr   r   rootdirsfilesr   r   r   r   Z  s    r   c                   @   s   e Zd ZdZdd ZdS )r   a  Support for data types that JSON default encoder
    does not do.

    This includes:

        * Numpy array or number
        * Complex number
        * Set
        * Bytes
        * astropy.UnitBase
        * astropy.Quantity

    Examples
    --------
    >>> import json
    >>> import numpy as np
    >>> from astropy.utils.misc import JsonCustomEncoder
    >>> json.dumps(np.arange(3), cls=JsonCustomEncoder)
    '[0, 1, 2]'

    c                 C   s   ddl m} dd l}t||jr4t|j|j dS t||j	|j
frN| S t|trd|j|jgS t|trvt|S t|tr| S t||j|jfr||jkrd}n| S tj| |S )Nr   )units)valueunitZdimensionless_unit)rZ   r   rD   rV   ZQuantityrk   r   r   Z	to_stringZnumberZndarraytolistcomplexrealimagsetlistrq   ra   ZUnitBaseZFunctionUnitBaseZdimensionless_unscaledjsonJSONEncoderdefault)r(   r   uZnpr   r   r   r     s"    



zJsonCustomEncoder.defaultN)r*   r+   r,   r-   r   r   r   r   r   r   p  s   r   c                 C   s   d dd td| D S )uv   
    Remove accents from a Unicode string.

    This helps with matching "ångström" to "angstrom", for example.
    r   c                 s   s    | ]}t |d kr|V  qdS )ZMnN)unicodedatacategory)r   cr   r   r   r!     s   z strip_accents.<locals>.<genexpr>ZNFD)r#   r   	normalize)r%   r   r   r   strip_accents  s    

r   rR   皙?c                 C   s4  t | trt| } |  }i }|D ]&}| }||g  || | q"|drt|dd |v rt|dd g}	ntj||||d}	t	|	r0t
 }
|	D ]}|
||  q|
}	|durg }|	D ]}||| q|}	tt
|	}	t|	}	t	|	dkr|	d }	nd|	dd d |	d  }	d	|	 d
S dS )a  
    When a string isn't found in a set of candidates, we can be nice
    to provide a list of alternatives in the exception.  This
    convenience function helps to format that part of the exception.

    Parameters
    ----------
    s : str

    candidates : sequence of str or dict of str keys

    n : int
        The maximum number of results to include.  See
        `difflib.get_close_matches`.

    cutoff : float
        In the range [0, 1]. Possibilities that don't score at least
        that similar to word are ignored.  See
        `difflib.get_close_matches`.

    fix : callable
        A callable to modify the results after matching.  It should
        take a single string and return a sequence of strings
        containing the fixed matches.

    Returns
    -------
    message : str
        Returns the string "Did you mean X, Y, or Z?", or the empty
        string if no alternatives were found.
    r%   Nr"   )ncutoffr   r   z, z or zDid you mean ?r   )rV   rW   r   lower
setdefaultr`   r\   difflibZget_close_matchesr:   r   updateextendr   sortedr#   )r%   Z
candidatesr   r   ZfixZs_lowerZcandidates_lower	candidateZcandidate_lowerZmatchesZcapitalized_matchesmatchZmapped_matchesr   r   r   did_you_mean  s@     


r   aM  The {func} {obj_type} is deprecated and may be removed in a future version.

    You can replace its functionality with a combination of the
    __init_subclass__ and __set_name__ magic methods introduced in Python 3.6.
    See https://github.com/astropy/astropy/issues/11094 for recipes on how to
    replicate their functionality.
z4.3c                       sB   e Zd ZdZdZeejdd ZdZ	 fddZ
dd	 Z  ZS )
r   a  
    Base class for descriptors whose order in the class body should be
    preserved.  Intended for use in concert with the
    `OrderedDescriptorContainer` metaclass.

    Subclasses of `OrderedDescriptor` must define a value for a class attribute
    called ``_class_attribute_``.  This is the name of a class attribute on the
    *container* class for these descriptors, which will be set to an
    `~collections.OrderedDict` at class creation time.  This
    `~collections.OrderedDict` will contain a mapping of all class attributes
    that were assigned instances of the `OrderedDescriptor` subclass, to the
    instances themselves.  See the documentation for
    `OrderedDescriptorContainer` for a concrete example.

    Optionally, subclasses of `OrderedDescriptor` may define a value for a
    class attribute called ``_name_attribute_``.  This should be the name of
    an attribute on instances of the subclass.  When specified, during
    creation of a class containing these descriptors, the name attribute on
    each instance will be set to the name of the class attribute it was
    assigned to on the class.

    .. note::

        Although this class is intended for use with *descriptors* (i.e.
        classes that define any of the ``__get__``, ``__set__``, or
        ``__delete__`` magic methods), this base class is not itself a
        descriptor, and technically this could be used for classes that are
        not descriptors too.  However, use with descriptors is the original
        intended purpose.
    r   c                 C   s   dS )a  
        Subclasses should define this attribute to the name of an attribute on
        classes containing this subclass.  That attribute will contain the mapping
        of all instances of that `OrderedDescriptor` subclass defined in the class
        body.  If the same descriptor needs to be used with different classes,
        each with different names of this attribute, multiple subclasses will be
        needed.
        Nr   )r(   r   r   r   _class_attribute_'  s    z#OrderedDescriptor._class_attribute_Nc                    s$   t j| _t  jd7  _t   d S )Nr   )r   _nextid_OrderedDescriptor__ordersuperrA   )r(   r=   r>   	__class__r   r   rA   :  s    zOrderedDescriptor.__init__c                 C   sP   t | trHt |trHz| j|jk W S  tyD   td| |Y qL0 ntS dS )z
        Defined for convenient sorting of `OrderedDescriptor` instances, which
        are defined to sort in their creation order.
        zqCould not determine ordering for {} and {}; at least one of them is not calling super().__init__ in its __init__.N)rV   r   r   ru   RuntimeErrorr;   NotImplemented)r(   otherr   r   r   __lt__B  s    
zOrderedDescriptor.__lt__)r*   r+   r,   r-   r   propertyabcabstractmethodr   _name_attribute_rA   r   __classcell__r   r   r   r   r      s   #
)	metaclassc                       s$   e Zd ZdZdZ fddZ  ZS )r   a  
    Classes should use this metaclass if they wish to use `OrderedDescriptor`
    attributes, which are class attributes that "remember" the order in which
    they were defined in the class body.

    Every subclass of `OrderedDescriptor` has an attribute called
    ``_class_attribute_``.  For example, if we have

    .. code:: python

        class ExampleDecorator(OrderedDescriptor):
            _class_attribute_ = '_examples_'

    Then when a class with the `OrderedDescriptorContainer` metaclass is
    created, it will automatically be assigned a class attribute ``_examples_``
    referencing an `~collections.OrderedDict` containing all instances of
    ``ExampleDecorator`` defined in the class body, mapped to by the names of
    the attributes they were assigned to.

    When subclassing a class with this metaclass, the descriptor dict (i.e.
    ``_examples_`` in the above example) will *not* contain descriptors
    inherited from the base class.  That is, this only works by default with
    decorators explicitly defined in the class body.  However, the subclass
    *may* define an attribute ``_inherit_decorators_`` which lists
    `OrderedDescriptor` classes that *should* be added from base classes.
    See the examples section below for an example of this.

    Examples
    --------

    >>> from astropy.utils import OrderedDescriptor, OrderedDescriptorContainer
    >>> class TypedAttribute(OrderedDescriptor):
    ...     """
    ...     Attributes that may only be assigned objects of a specific type,
    ...     or subclasses thereof.  For some reason we care about their order.
    ...     """
    ...
    ...     _class_attribute_ = 'typed_attributes'
    ...     _name_attribute_ = 'name'
    ...     # A default name so that instances not attached to a class can
    ...     # still be repr'd; useful for debugging
    ...     name = '<unbound>'
    ...
    ...     def __init__(self, type):
    ...         # Make sure not to forget to call the super __init__
    ...         super().__init__()
    ...         self.type = type
    ...
    ...     def __get__(self, obj, objtype=None):
    ...         if obj is None:
    ...             return self
    ...         if self.name in obj.__dict__:
    ...             return obj.__dict__[self.name]
    ...         else:
    ...             raise AttributeError(self.name)
    ...
    ...     def __set__(self, obj, value):
    ...         if not isinstance(value, self.type):
    ...             raise ValueError('{0}.{1} must be of type {2!r}'.format(
    ...                 obj.__class__.__name__, self.name, self.type))
    ...         obj.__dict__[self.name] = value
    ...
    ...     def __delete__(self, obj):
    ...         if self.name in obj.__dict__:
    ...             del obj.__dict__[self.name]
    ...         else:
    ...             raise AttributeError(self.name)
    ...
    ...     def __repr__(self):
    ...         if isinstance(self.type, tuple) and len(self.type) > 1:
    ...             typestr = '({0})'.format(
    ...                 ', '.join(t.__name__ for t in self.type))
    ...         else:
    ...             typestr = self.type.__name__
    ...         return '<{0}(name={1}, type={2})>'.format(
    ...                 self.__class__.__name__, self.name, typestr)
    ...

    Now let's create an example class that uses this ``TypedAttribute``::

        >>> class Point2D(metaclass=OrderedDescriptorContainer):
        ...     x = TypedAttribute((float, int))
        ...     y = TypedAttribute((float, int))
        ...
        ...     def __init__(self, x, y):
        ...         self.x, self.y = x, y
        ...
        >>> p1 = Point2D(1.0, 2.0)
        >>> p1.x
        1.0
        >>> p1.y
        2.0
        >>> p2 = Point2D('a', 'b')  # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
            ...
        ValueError: Point2D.x must be of type (float, int>)

    We see that ``TypedAttribute`` works more or less as advertised, but
    there's nothing special about that.  Let's see what
    `OrderedDescriptorContainer` did for us::

        >>> Point2D.typed_attributes
        OrderedDict([('x', <TypedAttribute(name=x, type=(float, int))>),
        ('y', <TypedAttribute(name=y, type=(float, int))>)])

    If we create a subclass, it does *not* by default add inherited descriptors
    to ``typed_attributes``::

        >>> class Point3D(Point2D):
        ...     z = TypedAttribute((float, int))
        ...
        >>> Point3D.typed_attributes
        OrderedDict([('z', <TypedAttribute(name=z, type=(float, int))>)])

    However, if we specify ``_inherit_descriptors_`` from ``Point2D`` then
    it will do so::

        >>> class Point3D(Point2D):
        ...     _inherit_descriptors_ = (TypedAttribute,)
        ...     z = TypedAttribute((float, int))
        ...
        >>> Point3D.typed_attributes
        OrderedDict([('x', <TypedAttribute(name=x, type=(float, int))>),
        ('y', <TypedAttribute(name=y, type=(float, int))>),
        ('z', <TypedAttribute(name=z, type=(float, int))>)])

    .. note::

        Hopefully it is clear from these examples that this construction
        also allows a class of type `OrderedDescriptorContainer` to use
        multiple different `OrderedDescriptor` classes simultaneously.
    r   c                    sF  t t}t }d}i }| jD ]}|j D ]\}	}
|	|v r<q*||	 t|
tr*|r`t|
|s`q*|
j	d urxt
|
|
j	|	 |
j|vr|
jjD ].}d|jv r|||
j< || |
|	f  qqq*||
j }|| |
|	f q*t|dds qq|j}q| D ]0\}}|  tdd |D }t
| |j| qtt| ||| d S )Nr   r   _inherit_descriptors_Fc                 s   s   | ]\}}||fV  qd S r'   r   )r   r   keyr   r   r   r!     r   z6OrderedDescriptorContainer.__init__.<locals>.<genexpr>)r   r   r   __mro__rm   rn   addrV   r   r   setattrr   r`   getattrr   sortr   r   r   r   rA   )clscls_namebasesmembersZdescriptorsseenZinherit_descriptorsZdescr_basesZmro_clsrh   r   Zobj_cls_baseZdescriptor_clsZ	instancesr   r   r   rA     sF    



	



z#OrderedDescriptorContainer.__init__)r*   r+   r,   r-   r   rA   r   r   r   r   r   r   U  s    c                 c   s   t | } td ttj}|| kr*dV  n8z&ttj|  dV  W ttj| nttj| 0 W d   n1 sv0    Y  dS )a2  
    Context manager to temporarily set the locale to ``name``.

    An example is setting locale to "C" so that the C strtod()
    function will use "." as the decimal point to enable consistent
    numerical string parsing.

    Note that one cannot nest multiple _set_locale() context manager
    statements as this causes a threading lock.

    This code taken from https://stackoverflow.com/questions/18593661/how-do-i-strftime-a-date-object-in-a-different-locale.

    Parameters
    ==========
    name : str
        Locale name, e.g. "C" or "fr_FR".
    N)rW   LOCALE_LOCKlocale	setlocaleLC_ALL)rh   Zsavedr   r   r   _set_locale!  s    r   z4.0zdDeprecated version of :func:`_set_locale` above.
See https://github.com/astropy/astropy/issues/9196
c                 C   s(   t d| j}|r t|dnd}|S )a  
    Parse the number out of a dtype.str value like '<U5' or '<f8'.

    See #5819 for discussion on the need for this function for getting
    the number of characters corresponding to a string dtype.

    Parameters
    ----------
    dtype : numpy dtype object
        Input dtype

    Returns
    -------
    bytes_or_chars : int or None
        Bits (for numeric types) or characters (for string types)
    z(\d+)$r   N)researchrW   intgroup)Zdtyper   outr   r   r   r   I  s    r   c                 C   s   ddl }|d|  d dS )z
    Open browser loaded with ``option`` options near you.

    *Disclaimers: Payments not included. Astropy is not
    responsible for any liability from using this function.*

    .. note:: Accuracy depends on your browser settings.

    r   Nz https://www.google.com/search?q=z+near+me)rT   re   )optionrT   r   r   r   _hungry_for_  s    
r   c                   C   s   t d dS )z
``/pizza``pizzaN)r   r   r   r   r   r   m  s    r   c                 C   s4   | r|rt d| rd}n|r$d}nd}t| dS )z``/coffee``zThere can be only one!zfresh+third+wave+coffeezdecent+espressocoffeeN)rb   r   )Zis_adamZis_brigittar   r   r   r   r   r  s    r   )r   r   )NTN)NF)rR   r   N)FF)6r-   r   
contextlibr   rY   r   r{   rl   r.   r7   r   r   Z	threadingr   r   collectionsr   r   Zastropy.utils.decoratorsr   __all__Z__doctest_skip__ZNOT_OVERWRITING_MSGZ_NOT_OVERWRITING_MSG_MATCHr   r   r&   r   r   r	   r
   rp   platformrs   ry   r   r   r   r   r   r   Z'_ordered_descriptor_deprecation_messageABCMetar   typer   ZLockr   r   Z
set_localer   r   r   r   r   r   r   r   <module>   sj   


"/
x

-
N
T I
!