ó
šßÈ[c        ,   @` sQ  d  Z  d d l m Z m Z m Z m Z d d l m 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 e 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/ d0 d1 d2 d3 d4 d5 g+ ƒ Z d e d6 „ Z d7 „  Z d S(8   u-   
Various utilities and cookbook-like things.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsi   (   t   sixN(   t   versionu   convert_to_writable_filelikeu   stc_reference_framesu   coerce_range_list_paramc         c` s‰  t  |  t j ƒ r¤ |  j d ƒ s' | rw t j |  d ƒ 9 } t j | d d ƒ} | V| j ƒ  | j ƒ  d SWd QXq…t j	 |  d d d ƒ } | Vd SWd QXná t
 |  d ƒ ryt j |  j ƒ sË t ‚ | ræ t j d |  ƒ }  n  t } y |  j d	 ƒ Wn t k
 rt } n Xt
 |  d
 ƒ s6|  j d k r?t } n  | rft j d ƒ |  ƒ V|  j ƒ  n |  V|  j ƒ  d St d ƒ ‚ d S(   u,  
    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
    u   .gzu   wbt   encodingu   utf8Nu   wtu   writet   fileobju    u   encodingu   utf-8u/   Can not be coerced to writable file-like object(   t
   isinstanceR   t   string_typest   endswitht   gzipt   GzipFilet   iot   TextIOWrappert   flusht   opent   hasattrt   callablet   writet   AssertionErrort   Falset	   TypeErrort   TrueR   t   Nonet   codecst	   getwriter(   t   fdt
   compressedt   real_fdt
   encoded_fdt   needs_wrapper(    (    s6   lib/python2.7/site-packages/astropy/io/votable/util.pyt   convert_to_writable_filelike   s:    


	
u   FK4u   FK5u   ECLIPTICu   ICRSu   GALACTICu
   GALACTIC_Iu   GALACTIC_IIu   SUPER_GALACTICu   AZ_ELu   BODYu   GEO_Cu   GEO_Du   MAGu   GSEu   GSMu   SMu   HGCu   HGSu   HEEQu   HRTNu   HPCu   HPRu   HCCu   HGIu	   MERCURY_Cu   VENUS_Cu   LUNA_Cu   MARS_Cu   JUPITER_C_IIIu   SATURN_C_IIIu   URANUS_C_IIIu   NEPTUNE_C_IIIu   PLUTO_Cu	   MERCURY_Gu   VENUS_Gu   LUNA_Gu   MARS_Gu   JUPITER_G_IIIu   SATURN_G_IIIu   URANUS_G_IIIu   NEPTUNE_G_IIIu   PLUTO_Gu   UNKNOWNFramec         ` sS  ‡  f d †  ‰ ‡ f d †  } d „  } |  d k r7 d St |  t t f ƒ r#t |  ƒ d k ok | |  d ƒ } | r |  d  } n |  } d j g  | D] } | | ƒ ^ q• ƒ } t | ƒ }	 | r| d k	 rú |  d | k rú t d j |  d ƒ ƒ ‚ n  | d	 |  d 7} |	 d 7}	 n  | |	 f St |  t j	 ƒ rd
 }
 ˆ  sRd |
 d }
 n  t
 j d |
 d |
 d |  ƒ } | d k r˜t d j |  ƒ ƒ ‚ n  | j ƒ  d } | d k	 rä| d k	 rä| | k rät d j | ƒ ƒ ‚ n  |  |  j d ƒ |  j d	 ƒ d f Sy t |  ƒ t |  ƒ d f SWn& t k
 rNt d j |  ƒ ƒ ‚ n Xd S(   u©  
    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 |  ƒ }  n  t |  ƒ S(   Nu    (   R   t   floatt   str(   t   x(   t   numeric(    s6   lib/python2.7/site-packages/astropy/io/votable/util.pyt   str_or_none   s
    c         ` sS   t  |  t ƒ rE t |  ƒ d k rE d j ˆ  |  d ƒ ˆ  |  d ƒ ƒ Sˆ  |  ƒ Sd  S(   Ni   u   {}/{}i    i   (   R   t   tuplet   lent   format(   R#   (   R%   (    s6   lib/python2.7/site-packages/astropy/io/votable/util.pyt   numeric_or_range–   s    !$c         S` s   t  |  t j ƒ S(   N(   R   R   R	   (   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/votable/util.pyt   is_frame_of_referenceœ   s    i    i   iÿÿÿÿu   ,u&   '{}' is not a valid frame of referenceu   ;u)   ([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)?u   (u   )|([A-Z_]+)u   ^u   ([,/]u    )+(;(?P<frame>[<A-Za-z_0-9]+))?$u   '{}' is not a valid range listu   frameN(   Ni    (   R   R   R&   t   listR'   t   joint
   ValueErrorR(   R   R	   t   ret   matcht	   groupdictt   countR!   R"   R   (   t   pt   framesR$   R)   R*   t   has_frame_of_referencet   pointsR#   t   outt   lengtht   numberR/   t   frame(    (   R$   R%   s6   lib/python2.7/site-packages/astropy/io/votable/util.pyt   coerce_range_list_parame   sN    *	"(
	$$
c         C` s5   d „  } | |  ƒ } | | ƒ } | | k | | k  S(   u2   
    Compare two VOTable version identifiers.
    c         S` s0   |  d j  ƒ  d k r# |  d }  n  t j |  ƒ S(   Ni    u   vi   (   t   lowerR   t   StrictVersion(   t   v(    (    s6   lib/python2.7/site-packages/astropy/io/votable/util.pyt   version_to_tupleÑ   s    (    (   t   at   bR>   t   avt   bv(    (    s6   lib/python2.7/site-packages/astropy/io/votable/util.pyt   version_compareÍ   s    	(   t   __doc__t
   __future__R    R   R   R   t   externR   R   t
   contextlibR   R.   R   t	   distutilsR   t   __all__t   contextmanagerR   R    t   sett   stc_reference_framesR   R   R:   RC   (    (    (    s6   lib/python2.7/site-packages/astropy/io/votable/util.pyt   <module>   s.   "	@h