
    HR-e                         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mZ g dZ	ej
        dd            Zh dZdd	Zd
 ZdS )z-
Various utilities and cookbook-like things.
    N)Version)convert_to_writable_filelikestc_reference_framescoerce_range_list_paramFc              #     K   t          | t                    rt          j                            |           } |                     d          s|rst          j        | d          5 }t          j	        |d          }|V  |
                                 |
                                 	 ddd           dS # 1 swxY w Y   dS t          | dd          5 }|V  	 ddd           dS # 1 swxY w Y   dS t          | d          rt          | j                  sJ |rt          j        |           } d	}	 |                     d
           n# t          $ r d}Y nw xY wt          | d          r| j        d}|r  t#          j        d          |           V  n| V  | 
                                 t          | t          j                  r|                                  dS t          d          )a  
    Returns a writable file-like object suitable for streaming output.

    Parameters
    ----------
    fd : str or file-like
        May be:

            - a file path string, 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
    z.gzwbutf8)encodingNwwrite)fileobjF Tr
   zutf-8z/Can not be coerced to writable file-like object)
isinstancestrospath
expanduserendswithgzipGzipFileioTextIOWrapperflushopenhasattrcallabler   	TypeErrorr
   codecs	getwriterclose)fd
compressedreal_fd
encoded_fdneeds_wrappers        7lib/python3.11/site-packages/astropy/io/votable/util.pyr   r      s     . "c )KW##;;u 
	 
	r4(( G-gGGG
      """                  b#/// 7                  
W		 K!!!!! 	+r***B 	!HHRLLLL 	! 	! 	! MMM	! r:&& 	!"+*= M 	+&"7++B//////HHH



b$-(( 	HHJJJIJJJs7   #AB44B8;B8C&&C*-C*3E	 	EE>+   SMFK4FK5GSEGSMHCCHGCHGIHGSHPCHPRMAGBODYHEEQHRTNICRSAZ_ELGEO_CGEO_DLUNA_CLUNA_GMARS_CMARS_GPLUTO_CPLUTO_GVENUS_CVENUS_GECLIPTICGALACTIC	MERCURY_C	MERCURY_G
GALACTIC_IGALACTIC_IISATURN_C_IIISATURN_G_IIIUNKNOWNFrameURANUS_C_IIIURANUS_G_IIIJUPITER_C_IIIJUPITER_G_IIINEPTUNE_C_IIINEPTUNE_G_IIISUPER_GALACTICTc                    fdfdd }| dS t          | t          t          f          rt          |           dk    o || d                   }|r| dd         }n
| dd         }d                    fd	|D                       }t          |          }|r8|#| d         |vrt          d
| d          d          |d| d         z   z  }|dz  }||fS t          | t                    rd}sd|z   dz   }t          j        d|z   dz   |z   dz   |           }	|	t          d
|  d          |		                                d         }
||
|
|vrt          |
d          | | 
                    d          | 
                    d          z   dz   fS 	 t          |            t          |           dfS # t          $ r t          d
|  d          w xY w)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                 L    | dS rt          |           } t          |           S )Nr   )floatr   )xnumerics    r&   str_or_nonez,coerce_range_list_param.<locals>.str_or_none   s+    92 	aA1vv    c                     t          | t                    r8t          |           dk    r% | d                    d | d                    S  |           S )N   r   /   )r   tuplelen)rU   rW   s    r&   numeric_or_rangez1coerce_range_list_param.<locals>.numeric_or_range   sa    a 	"CFFaKK!k!A$''==++ad*;*;===;q>>!rX   c                 ,    t          | t                    S )N)r   r   )rU   s    r&   is_frame_of_referencez6coerce_range_list_param.<locals>.is_frame_of_reference   s    !S!!!rX   N)Nr   r\   ,c                 &    g | ]} |          S  re   ).0rU   r_   s     r&   
<listcomp>z+coerce_range_list_param.<locals>.<listcomp>   s%    <<<((++<<<rX   '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framez" is not a valid frame of reference)r   r]   listr^   join
ValueErrorr   rematch	groupdictcountrT   r   )pframesrV   ra   has_frame_of_referencepointsoutlengthnumberrq   rl   r_   rW   s     `        @@r&   r   r      sy   V    " " " " "" " " 	yw	At}	%	% 2!$Q!!L0E0Eae0L0L! 	ssVFFqqqTFhh<<<<V<<<==V! 	!ae6&9&9 !OQrU!O!O!OPPP32;CaKFF{	As		 2= 	3F]]2F&L8#f,/RRTU
 
 =????@@@!!'*%"3V8K8KKKKLLL!''#,,-111=a1vvqy = = =;Q;;;<<<=s   F6 6Gc                 N    d } ||           } ||          }||k    ||k     z
  S )z2
    Compare two VOTable version identifiers.
    c                 p    | d                                          dk    r
| dd          } t          |           S )Nr   vr\   )lowerr   )r}   s    r&   version_to_tuplez)version_compare.<locals>.version_to_tuple   s2    Q4::<<3!""AqzzrX   re   )abr   avbvs        r&   version_comparer      sF    
  
 
	!		B		!		BGR  rX   )F)NT)__doc__r   
contextlibr   r   r   rp   packaging.versionr   __all__contextmanagerr   r   r   r   re   rX   r&   <module>r      s           				 				 				 % % % % % %   ?K ?K ?K ?KF, , , ^c= c= c= c=L! ! ! ! !rX   