B
    ~d-                 @   s  d 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dlmZ ddlZddlZddlmZmZ dd	lmZ d
dlmZ ejejedZejedZejedZedddd Zedddd Z edddd Z!edddd Z"dd Z#ej$j%e#  ddZ&d d! Z'd"d# Z(efd$d%Z)d&d' Z*d(d) Z+d>d,d-Z,d.d/ Z-d0d1d2d3gfd4d5Z.d6d7 Z/d?d8d9Z0eed:d; Z1dd*g i g i ei fd<d=Z2dS )@z!Utilties for the GWpy test suite
    N)contextmanager)LooseVersion)import_module)zip_longest)Path)assert_array_equalassert_allclose)Time   )deprecated_functiondatazHLV-HW100916-968654552-1.gwfzHLV-HW100916-968654552-1.hdf)zFgwpy.testing.utils.has is deprecated and will be removed in GWpy 3.1.0)messagec             C   s*   yt |  W n tk
r    dS X dS dS )zkTest whether a module is available

    Returns `True` if `import module` succeeded, otherwise `False`
    FTN)r   ImportError)module r   _/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/testing/utils.pyhas1   s
    	r   )zgwpy.testing.utils.skip_missing_dependency is deprecated and will be removed in GWpy 3.1.0, please update your code to use pytest.mark.requires from the pytest-requires packagec             C   s   t jjt|  d|  dS )zReturns a mark generator to skip a test if the dependency is missing

    .. deprecated:: 3.0.0
       Use `pytest.mark.requires` from pytest-requires instead.
    zNo module named %s)reason)pytestmarkskipifr   )r   r   r   r   skip_missing_dependencyB   s    r   )zTgwpy.testing.utils.module_older_than is deprecated and will be removed in GWpy 3.1.0c             C   s   t | }t|jt|k S )N)r   r   __version__)r   
minversionmodr   r   r   module_older_thanQ   s    r   )zWgwpy.testing.utils.skip_minimum_version is deprecated and will be removed in GWpy 3.1.0c             C   s   t jjt| |d| |dS )zdReturns a mark generator to skip a test if the dependency is too old

    .. deprecated:: 3.0.0
    zrequires {} >= {})r   )r   r   r   r   format)r   r   r   r   r   skip_minimum_versionZ   s    	r   c            	   C   s<   yt jddgt jt jd W n t jtfk
r6   dS X dS )zReturn `True` if the current user has a valid kerberos credential

    This function just calls ``klist -s`` and returns `True` if the
    command returns a zero exit code, and `False` if it doesn't, or
    the call fails in any other way.
    Zklistz-s)stdoutstderrFT)
subprocess
check_callDEVNULLCalledProcessErrorFileNotFoundErrorr   r   r   r   _has_kerberos_credentialh   s    r%   zno kerberos credential)r   c             C   s   t | |td dS )zCAssert that two `~astropy.units.Quantity` objects are the same
    )array_assertionN)_assert_quantityr   )q1q2r   r   r   assert_quantity_equal   s    r*   c             C   s   t | |td dS )zAssert that two `~astropy.units.Quantity` objects are almost the same

    This method asserts that the units are the same and that the values are
    equal within precision.
    )r&   N)r'   r   )r(   r)   r   r   r   assert_quantity_almost_equal   s    r+   c             C   s2   | j |j ks td| j |j f || j|j d S )Nz%r != %r)unitAssertionErrorvalue)r(   r)   r&   r   r   r   r'      s     r'   c                s   | ddrt}nt}s  j| dg fddD  fdd}|d |d	 t f  | jj d
S )a  Assert that two `~gwpy.types.Array` objects are the same (or almost)

    Parameters
    ----------
    a, b : `~gwpy.types.Array`
        the arrays two be tested (can be subclasses)

    *attrs
        the list of attributes to test, defaults to all

    almost_equal : `bool`, optional
        allow the numpy array's to be 'almost' equal, default: `False`,
        i.e. require exact matches

    exclude : `list`, optional
        a list of attributes to exclude from the test
    almost_equalFexcludec                s   g | ]}| kr|qS r   r   ).0attr)r0   r   r   
<listcomp>   s    z-assert_quantity_sub_equal.<locals>.<listcomp>c                sH   d | }d| }|krDt |ddkrDt|ddkrD| d S )Nz{}index_-)r   getattrremove)dimindex_index)aattrsbr   r   _check_index   s    
z/assert_quantity_sub_equal.<locals>._check_indexxyN)popr   r   Z_metadata_slotsassert_attributesr.   )r;   r=   r<   kwargsassert_arrayr>   r   )r;   r<   r=   r0   r   assert_quantity_sub_equal   s    	rE   c             G   s   xz|D ]r}t | |d}t ||d}t|tjrFt|tjrFt|| qt|trlt|trl|j|jksxtq||kstqW dS )zAssert that the attributes for two objects match

    `attrs` should be `list` of attribute names that can be accessed
    with `getattr`
    N)r6   
isinstancenumpyZndarrayr   r	   Zgpsr-   )r;   r=   r<   r2   r?   r@   r   r   r   rB      s    
rB   TFc       
      C   s   t | jt |jkst|r,| j|jks,t|r6t}nt}x<| jD ]2}| | }|| }	|rj|j|	jksjt|||	 qBW x*| jD ] }t| | || |k	stqW dS )z6Assert that two tables store the same information
    N)	sortedZcolnamesr-   metar   r   ZdtyperG   Zmay_share_memory)
r;   r=   Zis_copyrI   Zcheck_typesr/   rD   nameZcolaZcolbr   r   r   assert_table_equal   s    rK   c             C   s(   x"t | |D ]\}}||kstqW dS )z9Assert that two `SegmentList`s contain the same data
    N)r   r-   )r;   r=   ZasegZbsegr   r   r   assert_segmentlist_equal   s    rL   rJ   Zifotagversionc             C   sF   t | j|j t | j|j x$|D ]}t| |t||ks"tq"W dS )z=Assert that two `DataQualityFlag`s contain the same data
    N)rL   activeZknownr6   r-   )r;   r=   r<   r2   r   r   r   assert_flag_equal   s    
rP   c             O   s@   |   |  kstx&| D ]}|| | || f|| qW dS )a.  Assert that two `dict`s contain the same data

    Parameters
    ----------
    a, b
        two objects to compare

    assert_value : `callable`
        method to compare that two dict entries are the same

    *args, **kargs
        positional and keyword arguments to pass to ``assert_value``
    N)keysr-   )r;   r=   Zassert_valueargsrC   keyr   r   r   assert_dict_equal  s    
rT   c             C   s4   |r
t }nt}x t| |D ]\}}||| qW d S )N)r   r   zip)r;   r=   r/   rD   r?   r@   r   r   r   assert_zpk_equal  s
    rV   c           	   o   s4   t j| |}z
|V  W dtj|r.t| X dS )a  Create and return a temporary filename

    Calls `tempfile.mktemp` to create a temporary filename, and deletes
    the named file (if it exists) when the context ends.

    This method **does not create the named file**.

    Examples
    --------
    >>> with TemporaryFilename(suffix='.txt') as tmp:
    ...     print(tmp)
    '/var/folders/xh/jdrqg2bx3s5f4lkq0rf2903c0000gq/T/tmpnNxivL.txt'
    N)tempfilemktempospathisfiler7   )rR   rC   rJ   r   r   r   TemporaryFilename$  s
    
r\   c
          	   C   s   |dkr|}d| d }t| }
t }t|d| }| j|f|d|i| |rr| jt|f|| |
j|f|d|i|}||| f|	 |r|
jt|f||}||| f|	 W dQ R X dS )ak  Test that data can be written to and read from a file in some format

    Parameters
    ----------
    data : some type with `.read()` and `.write()` methods
        the data to be written

    format : `str`
        the name of the file format (as registered with `astropy.io.registry`

    extension : `str`, optional
        the name of the file extension, defaults to ``.<format>``

    autoidenfity : `bool`, optional
        attempt to auto-identify when reading writing by not specifying
        ``format``

    read_args : `list`, optional
        positional arguments to pass to ``type(data).read()``

    read_kwargs : `dict`, optional
        keyword arguments to pass to ``type(data).read()``

    write_args : `list`, optional
        positional arguments to pass to ``data.write()``

    write_kwargs : `dict`, optional
        keyword arguments to pass to ``data.write()``

    assert_equal : `callable`, optional
        the function to assert that the object read back from file matches
        the original ``data``

    assert_kwargs : `dict`, optional
        keyword arguments to pass to ``assert_equal``
    Nz.%s.ztest.{}r   )	lstriptyperW   TemporaryDirectoryr   r   writestrread)r   r   	extensionZautoidentifyZ	read_argsZread_kwZ
write_argsZwrite_kwZassert_equalZ	assert_kwZ	DataClassZtmpdirtmpnewr   r   r   test_read_write<  s    *
rg   )TFTF)F)3__doc__os.pathrY   r    rW   
contextlibr   Zdistutils.versionr   	importlibr   	itertoolsr   pathlibr   r   rG   Znumpy.testingr   r   Zastropy.timer	   Zutils.decoratorsr   rZ   joindirname__file__ZTEST_DATA_DIRZTEST_GWF_FILEZTEST_HDF5_FILEr   r   r   r   r%   r   r   Zskip_kerberos_credentialr*   r+   r'   rE   rB   rK   rL   rP   rT   rV   r\   rg   r   r   r   r   <module>   sZ   	0 
	
