a
    ߙfb                     @   s   d dl Z ddlmZmZ ddlmZ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mZ d
d ZdddZdddZe
dee e
dee e
dee dS )    N   )parse
from_table)VOTableFileTable)registry)r   )
BaseColumn)Quantity)NOT_OVERWRITING_MSGc              	   O   sx   ddl m} | dkrp|durDz||}W |d n|d 0 |S |durT||S t|d ttfrjdS dS ndS dS )a7  
    Reads the header of a file to determine if it is a VOTable file.

    Parameters
    ----------
    origin : str or readable file-like
        Path or file object containing a VOTABLE_ xml file.

    Returns
    -------
    is_votable : bool
        Returns `True` if the given file is a VOTable file.
    r   )
is_votablereadNr   TF) r   seek
isinstancer   VOTable)originfilepathZfileobjargskwargsr   result r   9lib/python3.9/site-packages/astropy/io/votable/connect.pyr      s    
r   Fc                 K   s<  t | ttfs$t| f||d|} t }g }t | tr |  D ]"}|jdurZ|||j< || qBt|dkr|du rt	d
d| t|nbt |tr||v r|| }qt	d| dn4t |tr|t|k r|| }ntd
|t|n t|dkr|d	 }nt	d
nt | tr0| }|j|dS )a  
    Read a Table object from an VO table file

    Parameters
    ----------
    input : str or `~astropy.io.votable.tree.VOTableFile` or `~astropy.io.votable.tree.Table`
        If a string, the filename to read the table from. If a
        :class:`~astropy.io.votable.tree.VOTableFile` or
        :class:`~astropy.io.votable.tree.Table` object, the object to extract
        the table from.

    table_id : str or int, optional
        The table to read in.  If a `str`, it is an ID corresponding
        to the ID of the table in the file (not all VOTable files
        assign IDs to their tables).  If an `int`, it is the index of
        the table in the file, starting at 0.

    use_names_over_ids : bool, optional
        When `True` use the ``name`` attributes of columns as the names
        of columns in the `~astropy.table.Table` instance.  Since names
        are not guaranteed to be unique, this may cause some columns
        to be renamed by appending numbers to the end.  Otherwise
        (default), use the ID attributes as the column names.

    verify : {'ignore', 'warn', 'exception'}, optional
        When ``'exception'``, raise an error when the file violates the spec,
        otherwise either issue a warning (``'warn'``) or silently continue
        (``'ignore'``). Warnings may be controlled using the standard Python
        mechanisms.  See the `warnings` module in the Python standard library
        for more information. When not provided, uses the configuration setting
        ``astropy.io.votable.verify``, which defaults to ``'ignore'``.

    **kwargs
        Additional keyword arguments are passed on to
        :func:`astropy.io.votable.table.parse`.
    )table_idverifyNr   zMultiple tables found: table id should be set via the table_id= argument. The available tables are {}, or integers less than {}.z, zNo tables with id=z foundz/Table index {} is out of range. {} tables foundr   zNo table found)use_names_over_ids)r   r   r   r   dictZiter_tablesZIDappendlen
ValueErrorformatjoinkeysstrint
IndexErrorZto_table)inputr   r   r   r   Ztable_id_mappingZtablestabler   r   r   read_table_votable0   sH    &








r'   c                 C   s   | j ttf}|r0dd |D }td|t|trdtj	
|rd|rVt| ntt|t| |d}|j||d dS )a  
    Write a Table object to an VO table file

    Parameters
    ----------
    input : Table
        The table to write out.

    output : str
        The filename to write the table to.

    table_id : str, optional
        The table ID to use. If this is not specified, the 'ID' keyword in the
        ``meta`` object of the table will be used.

    overwrite : bool, optional
        Whether to overwrite any existing file without warning.

    tabledata_format : str, optional
        The format of table data to write.  Must be one of ``tabledata``
        (text representation), ``binary`` or ``binary2``.  Default is
        ``tabledata``.  See :ref:`astropy:votable-serialization`.
    c                 S   s   g | ]}|j jqS r   )infoname).0colr   r   r   
<listcomp>       z'write_table_votable.<locals>.<listcomp>z5cannot write table with mixin column(s) {} to VOTable)r   )tabledata_formatN)columnsZnot_isinstancer   r	   r   r   r   r"   ospathexistsremoveOSErrorr
   r   Zto_xml)r%   outputr   Z	overwriter.   Zunsupported_colsZunsupported_namesZ
table_filer   r   r   write_table_votable   s    r6   Zvotable)NFN)NFN)r0   r   r   r   Ztreer   r   r   Z
astropy.ior   Zio_registryZastropy.tableZastropy.table.columnr   Zastropy.unitsr	   Zastropy.utils.miscr
   r   r'   r6   Zregister_readerZregister_writerZregister_identifierr   r   r   r   <module>   s"      
R  
/