a
    ߙfb5                     @   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lmZ ddl	m
Z
 ddlmZ ddlmZ g d	Zg d
Zedddddddejddddddf
ddZdd ZdddZejddfddZd ddZdd Zdd ZdS )!zP
This file contains a contains the high-level functions to read a
VOTable file.
    N   )
exceptions)tree)
iterparser)data)deprecated_renamed_argument)parseparse_single_table
from_tablewritetovalidatereset_vo_warnings)ignorewarn	exceptionZpedanticverifyz5.0)Zsincer   Fc              	   C   s  ddl m} | }|dvr$td|du rL|j }|dv rH|dk}n|}t|trd|r^dnd	}n|tvrtd
dt |	du ri }	||||||||	d}|du rt| t	r| |d< t
j| |
d&}tj|dd||W  d   S 1  s0    Y  dS )af  
    Parses a VOTABLE_ xml file (or file-like object), and returns a
    `~astropy.io.votable.tree.VOTableFile` object.

    Parameters
    ----------
    source : path-like or file-like
        Path or file-like object containing a VOTABLE_ xml file.
        If file, must be readable.

    columns : sequence of str, optional
        List of field names to include in the output.  The default is
        to include all fields.

    invalid : str, optional
        One of the following values:

            - 'exception': throw an exception when an invalid value is
              encountered (default)

            - 'mask': mask out invalid values

    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'.

        .. versionchanged:: 4.0
           ``verify`` replaces the ``pedantic`` argument, which will be
           deprecated in future.
        .. versionchanged:: 5.0
            The ``pedantic`` argument is deprecated.

    chunk_size : int, optional
        The number of rows to read before converting to an array.
        Higher numbers are likely to be faster, but will consume more
        memory.

    table_number : int, optional
        The number of table in the file to read in.  If `None`, all
        tables will be read.  If a number, 0 refers to the first table
        in the file, and only that numbered table will be parsed and
        read in.  Should not be used with ``table_id``.

    table_id : str, optional
        The ID of the table in the file to read in.  Should not be
        used with ``table_number``.

    filename : str, optional
        A filename, URL or other identifier to use in error messages.
        If *filename* is None and *source* is a string (i.e. a path),
        then *source* will be used as a filename for error messages.
        Therefore, *filename* is only required when source is a
        file-like object.

    unit_format : str, astropy.units.format.Base instance or None, optional
        The unit format to use when parsing unit attributes.  If a
        string, must be the name of a unit formatter. The built-in
        formats include ``generic``, ``fits``, ``cds``, and
        ``vounit``.  A custom formatter may be provided by passing a
        `~astropy.units.UnitBase` instance.  If `None` (default),
        the unit format to use will be the one specified by the
        VOTable specification (which is ``cds`` up to version 1.3 of
        VOTable, and ``vounit`` in more recent versions of the spec).

    datatype_mapping : dict, optional
        A mapping of datatype names (`str`) to valid VOTable datatype names
        (str). For example, if the file being read contains the datatype
        "unsignedInt" (an invalid datatype in VOTable), include the mapping
        ``{"unsignedInt": "long"}``.

    Returns
    -------
    votable : `~astropy.io.votable.tree.VOTableFile` object

    See also
    --------
    astropy.io.votable.exceptions : The exceptions this function may raise.
    r   )conf)r   maskzBaccepted values of ``invalid`` are: ``'exception'`` or ``'mask'``.N)Zfalsetruer   r   r   zverify should be one of /)columnsinvalidr   
chunk_sizetable_numberfilenameunit_formatdatatype_mappingr   )_debug_python_based_parser)r   r   )configpos) r   lower
ValueErrorr   
isinstanceboolVERIFY_OPTIONSjoinstrr   get_xml_iteratorr   VOTableFiler   )sourcer   r   r   r   r   table_idr   r   r   r   r   Zconf_verify_lowercaser   iterator r-   7lib/python3.9/site-packages/astropy/io/votable/table.pyr      sH    W


r   c                 K   s.   | ddu rd|d< t| fi |}| S )a  
    Parses a VOTABLE_ xml file (or file-like object), reading and
    returning only the first `~astropy.io.votable.tree.Table`
    instance.

    See `parse` for a description of the keyword arguments.

    Returns
    -------
    votable : `~astropy.io.votable.tree.Table` object
    r   Nr   )getr   Zget_first_table)r*   kwargsvotabler-   r-   r.   r	      s    r	   c                 C   sL   ddl m} t| |r$tj| } nt| tjs8td| j||dd dS )a  
    Writes a `~astropy.io.votable.tree.VOTableFile` to a VOTABLE_ xml file.

    Parameters
    ----------
    table : `~astropy.io.votable.tree.VOTableFile` or `~astropy.table.Table` instance.

    file : str or writable file-like
        Path or file object to write to

    tabledata_format : str, optional
        Override the format of the table(s) data to write.  Must be
        one of ``tabledata`` (text representation), ``binary`` or
        ``binary2``.  By default, use the format that was specified in
        each ``table`` object as it was created or read in.  See
        :ref:`astropy:astropy:votable-serialization`.
    r   )TablezPfirst argument must be astropy.io.vo.VOTableFile or astropy.table.Table instanceT)tabledata_formatr   N)Zastropy.tabler2   r#   r   r)   r
   	TypeErrorZto_xml)tablefiler3   r2   r-   r-   r.   r      s    
r   c                 C   s  ddl m}m} d}|du r(t }d}g }d}t  tj| dd}	|	 }
W d   n1 sb0    Y  t	|
}|
d |du rt| tr| }n(t| dr| j}nt| d	r| j}nd
}tjddr}t  tjdtjdd zt|d|d}W n4 ty. } z|t| W Y d}~n
d}~0 0 W d   n1 sF0    Y  dd |D | }|
d |d| d t|rTt|}|D ]}t|}|d s||d  |d n|||d d  }|d }|d rd}nd}||d ddd|pd|ddtj|d d d!d"  |d# |||d$ |d# |d% qn
|d& d}|rt!j"#|rdd'l$m%} |du rd(}n|j&}|'||\}}}|dkr|d) ||(d* n
|d+ |r|) S t|dko|dkS ),a:  
    Prints a validation report for the given file.

    Parameters
    ----------
    source : path-like or file-like
        Path to a VOTABLE_ xml file or `~pathlib.Path`
        object having Path to a VOTABLE_ xml file.
        If file-like object, must be readable.

    output : file-like, optional
        Where to output the report.  Defaults to ``sys.stdout``.
        If `None`, the output will be returned as a string.
        Must be writable.

    xmllint : bool, optional
        When `True`, also send the file to ``xmllint`` for schema and
        DTD validation.  Requires that ``xmllint`` is installed.  The
        default is `False`.  ``source`` must be a file on the local
        filesystem in order for ``xmllint`` to work.

    filename : str, optional
        A filename to use in the error messages.  If not provided, one
        will be automatically determined from ``source``.

    Returns
    -------
    is_valid : bool or str
        Returns `True` if no warnings were found.  If ``output`` is
        `None`, the return value will be a string.
    r   )print_code_linecolor_printFNTZbinary)encodingnameurlz	<unknown>)recordalways)appendr   )r   r   c                 S   s$   g | ]}t |jtjrt|jqS r-   )
issubclasscategoryr   	VOWarningr'   message).0xr-   r-   r.   
<listcomp>  s   zvalidate.<locals>.<listcomp>zValidation report for z

Zis_somethingrB   Znliner   warningZ
is_warningZyellowZreddz: r    ZEXCz
          z  )Zinitial_indentZsubsequent_indent)r6   Znchar
z)astropy.io.votable found no violations.

)xmlutilz1.1zxmllint schema violations:

zutf-8zxmllint passed
)*Zastropy.utils.consoler7   r8   ioStringIOr   r   Zget_readable_fileobjreadBytesIOseekr#   r'   hasattrr:   r;   warningscatch_warningsresetwarningssimplefilterr   rA   r   r"   r>   writelenr   Zxml_readlinesZparse_vowarningtextwrapZfilllstripospathexistsr    rI   versionZvalidate_schemadecodegetvalue)r*   outputZxmllintr   r7   r8   Zreturn_as_strlinesr1   fdZcontentZcontent_bufferZwarning_lineseZ	xml_linesrF   wlineZcolorZsuccessrI   r[   stdoutstderrr-   r-   r.   r      s    !&




D






	




r   c                 C   s   t jj| |dS )a  
    Given an `~astropy.table.Table` object, return a
    `~astropy.io.votable.tree.VOTableFile` file structure containing
    just that single table.

    Parameters
    ----------
    table : `~astropy.table.Table` instance

    table_id : str, optional
        If not `None`, set the given id on the returned
        `~astropy.io.votable.tree.Table` instance.

    Returns
    -------
    votable : `~astropy.io.votable.tree.VOTableFile` instance
    )r+   )r   r)   r
   )r5   r+   r-   r-   r.   r
   S  s    r
   c                 C   s   zt | x}|D ],\}}}}|dkr: W d   W dS  q@q|D ],\}}}}|dkrl W d   W dS  qrqDW d   W dS 1 s0    Y  W n ty   Y dS 0 dS )aW  
    Reads the header of a file to determine if it is a VOTable file.

    Parameters
    ----------
    source : path-like or file-like
        Path or file object containing a VOTABLE_ xml file.
        If file, must be readable.

    Returns
    -------
    is_votable : bool
        Returns `True` if the given file is a VOTable file.
    ZxmlNFZVOTABLET)r   r(   r"   )r*   r,   starttagrG   r   r-   r-   r.   
is_votableh  s    &rh   c               	   C   sB   ddl m} m} | tt|fD ] }z|`W q ty:   Y q0 qdS )z
    Resets all of the vo warning state so that warnings that
    have already been emitted will be emitted again. This is
    used, for example, by `validate` which must emit all
    warnings each time it is called.

    r   )
convertersrI   N)r    ri   rI   r   r   Z__warningregistry__AttributeError)ri   rI   moduler-   r-   r.   r     s    r   )N)N)__doc__rJ   rX   sysrV   rP   r    r   r   Zastropy.utils.xmlr   Zastropy.utilsr   Zastropy.utils.decoratorsr   __all__r%   ZDEFAULT_CHUNK_SIZEr   r	   r   rd   r   r
   rh   r   r-   r-   r-   r.   <module>   s2    

 