B
    q\N              ,   @   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mZ dddgZej	d8ddZ
ed	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3g+Zd9d5dZd6d7 ZdS ):z-
Various utilities and cookbook-like things.
    N)versionconvert_to_writable_filelikestc_reference_framescoerce_range_list_paramFc          	   c   s$  t | trx| ds|rVt| d*}tj|dd}|V  |  |  dS Q R X n t| ddd}|V  dS Q R X nt	| drt
| jst|rtj| d} d	}y| d
 W n tk
r   d}Y nX t	| dr| jdkrd}|rtd| V  |   n| V  |   dS tddS )a,  
    Returns a writable file-like object suitable for streaming output.

    Parameters
    ----------
    fd : file path string or writable file-like object
        May be:

            - a file path, in which case it is opened, and the file
              object is returned.

            - an object with a :meth:``write`` method, in which case that
              object is returned.

    compressed : bool, optional
        If `True`, create a gzip-compressed file.  (Default is `False`).

    Returns
    -------
    fd : writable file-like object
    z.gzwbutf8)encodingNZwtwrite)ZfileobjF Tr   zutf-8z/Can not be coerced to writable file-like object)
isinstancestrendswithgzipZGzipFileioTextIOWrapperflushopenhasattrcallabler	   AssertionError	TypeErrorr   codecs	getwriter)fdZ
compressedZreal_fdZ
encoded_fdZneeds_wrapper r   6lib/python3.7/site-packages/astropy/io/votable/util.pyr      s:    


ZFK4ZFK5ZECLIPTICZICRSZGALACTICZ
GALACTIC_IZGALACTIC_IIZSUPER_GALACTICZAZ_ELZBODYZGEO_CZGEO_DZMAGZGSEZGSMZSMZHGCZHGSZHEEQZHRTNZHPCZHPRZHCCZHGIZ	MERCURY_CZVENUS_CZLUNA_CZMARS_CZJUPITER_C_IIIZSATURN_C_IIIZURANUS_C_IIIZNEPTUNE_C_IIIZPLUTO_CZ	MERCURY_GZVENUS_GZLUNA_GZMARS_GZJUPITER_G_IIIZSATURN_G_IIIZURANUS_G_IIIZNEPTUNE_G_IIIZPLUTO_GZUNKNOWNFrameTc                s   fddfdddd }| dkr,dS t | ttfrt| d	koP|| d
 }|rd| dd
 }n| dd }dfdd|D }t|}|r|dk	r| d
 |krtd| d
 |d| d
  7 }|d	7 }||fS t | trd}  sd| d }t	d| d | d | }	|	dkr0td| |	
 d }
|dk	rh|
dk	rh|
|krhtd|
| | d| d d	 fS yt|  t| d	fS  tk
r   td| Y nX dS )a  
    Coerces and/or verifies the object *p* into a valid range-list-format parameter.

    As defined in `Section 8.7.2 of Simple
    Spectral Access Protocol
    <http://www.ivoa.net/Documents/REC/DAL/SSA-20080201.html>`_.

    Parameters
    ----------
    p : str or sequence
        May be a string as passed verbatim to the service expecting a
        range-list, or a sequence.  If a sequence, each item must be
        either:

            - a numeric value

            - a named value, such as, for example, 'J' for named
              spectrum (if the *numeric* kwarg is False)

            - a 2-tuple indicating a range

            - the last item my be a string indicating the frame of
              reference

    frames : sequence of str, optional
        A sequence of acceptable frame of reference keywords.  If not
        provided, the default set in ``set_reference_frames`` will be
        used.

    numeric : bool, optional
        TODO

    Returns
    -------
    parts : tuple
        The result is a tuple:
            - a string suitable for passing to a service as a range-list
              argument

            - an integer counting the number of elements
    c                s    | d krdS  rt | } t| S )Nr
   )floatr   )x)numericr   r   str_or_none   s
    z,coerce_range_list_param.<locals>.str_or_nonec                s>   t | tr2t| dkr2d | d  | d S  | S d S )N   z{}/{}r      )r   tuplelenformat)r   )r   r   r   numeric_or_range   s    z1coerce_range_list_param.<locals>.numeric_or_rangec             S   s
   t | tS )N)r   r   )r   r   r   r   is_frame_of_reference   s    z6coerce_range_list_param.<locals>.is_frame_of_referenceN)Nr   r!   ,c                s   g | ]} |qS r   r   ).0r   )r%   r   r   
<listcomp>   s    z+coerce_range_list_param.<locals>.<listcomp>z&'{}' is not a valid frame of reference;z)([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)?(z)|([A-Z_]+)^z([,/]z )+(;(?P<frame>[<A-Za-z_0-9]+))?$z'{}' is not a valid range listframe)r   r"   listr#   join
ValueErrorr$   r   rematch	groupdictcountr   r   )pZframesr   r&   Zhas_frame_of_referenceZpointsoutlengthZnumberr3   r.   r   )r   r%   r   r   r   c   sN    *
c             C   s(   dd }|| }||}||k||k  S )z2
    Compare two VOTable version identifiers.
    c             S   s&   | d   dkr| dd  } t| S )Nr   vr!   )lowerr   ZStrictVersion)r9   r   r   r   version_to_tuple   s    z)version_compare.<locals>.version_to_tupler   )abr;   avZbvr   r   r   version_compare   s    r?   )F)NT)__doc__r   
contextlibr   r2   r   Z	distutilsr   __all__contextmanagerr   setr   r   r?   r   r   r   r   <module>   s*   @



h