B
    dO                 @   sX   d Z ddlZddlZddlmZ ddlmZ dd Zdd Z	d	d
 Z
dd Zdd ZdS )z'Utilities for the GW datafind service.
    N)segment)x509c               C   sL   y
t jd S  tk
rF   y
t jd S  tk
r@   tdY nX Y nX dS )a   Return the default host as stored in the ``${GWDATAFIND_SERVER}``.

    Returns
    -------
    host : `str`
        the URL of the default host

    Raises
    ------
    ValueError
        if the ``GWDATAFIND_SERVER`` environment variable is not set
    ZGWDATAFIND_SERVERZLIGO_DATAFIND_SERVERzuFailed to determine default gwdatafind host, please pass manually or set the `GWDATAFIND_SERVER` environment variableN)osenvironKeyError
ValueError r   r   ]/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwdatafind/utils.pyget_default_host   s    

r
   c             C   s   t dt tj| dd dS )a  Validate an X509 proxy certificate file.

    .. warning::

       This method is deprecated and will be removed in a future release.

    This function tests that the proxy certificate is
    `RFC 3820 <https://www.ietf.org/rfc/rfc3820.txt>` compliant and is
    not expired.

    Parameters
    ----------
    path : `str`
        the path of the X509 file on disk.

    Returns
    -------
    valid : `True`
        if the certificate validiates.

    Raises
    ------
    RuntimeError
        if the certificate cannot be validated.
    zthis method is deprecated and will be removed in gwdatafind-3.0.0, please update your workflow to use igwn_auth_utils.x509.validate_certificater   )timeleftT)warningswarnDeprecationWarning	igwn_x509Zvalidate_certificate)pathr   r   r	   validate_proxy9   s
    r   c              C   s.   t dt tjdd} t| tr&| S | | fS )a;  Locate X509 certificate and key files.

    .. warning::

       This method is deprecated and will be removed in a future release.

    This function checks the following paths in order:

    - ``${X509_USER_PROXY}``
    - ``${X509_USER_CERT}`` and ``${X509_USER_KEY}``
    - ``/tmp/x509up_u${UID}``

    Returns
    -------
    cert, key : `str`, `str`
        the paths of the certificate and key files, respectively.

    Raises
    ------
    RuntimeError
        if not certificate files can be found, or if the files found on
        disk cannot be validtted.
    zthis method is deprecated and will be removed in gwdatafind-3.0.0, please update your workflow to use igwn_auth_utils.x509.find_credentialsr   )r   )r   r   r   r   Zfind_credentials
isinstancetuple)credsr   r   r	   find_credential^   s    
r   c             C   sH   t j| d\}}}}t|}t|dd }||t||| fS )aS  Return metadata parsed from a filename following LIGO-T050017.

    Parameters
    ----------
    filename : `str`
        the path name of a file

    Returns
    -------
    obs : `str`
        the observatory metadata

    tag : `str`
        the file tag

    segment : `ligo.segments.segment`
        the GPS ``[start, stop)`` interval for this file

    Notes
    -----
    `LIGO-T050017 <https://dcc.ligo.org/LIGO-T050017>`__ declares a
    file naming convention that includes documenting the GPS start integer
    and integer duration of a file, see that document for more details.
    -.r   )r   r   basenamesplitintr   )filenameZobsdescstartendr   r   r	   filename_metadata   s    r   c             C   s   t | d S )a  Return the data segment for a filename following LIGO-T050017.

    Parameters
    ----------
    filename : `str`
        the path of a file.

    Returns
    -------
    segment : `~ligo.segments.segment`
        the ``[start, stop)`` GPS segment covered by the given file
       )r   )r   r   r   r	   file_segment   s    r!   )__doc__r   r   Zligo.segmentsr   Zigwn_auth_utilsr   r   r
   r   r   r   r!   r   r   r   r	   <module>   s   %'