U
    e»{jê6  ã                   @   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mZ dd	d
dddgZdddg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)Údata)Údeprecated_renamed_argument)Ú
iterparseré   )Ú
exceptionsÚtreeÚparseÚparse_single_tableÚ
from_tableÚwritetoÚvalidateÚreset_vo_warningsÚignoreÚwarnÚ	exceptionZpedanticÚverifyz5.0)ZsinceFc              
   C   s  ddl m} | ¡ }|dkr$tdƒ‚|dkrL|j ¡ }|dkrH|dk}n|}t|tƒrd|r^dnd	}n|tkr€td
d t¡› �ƒ‚|	dkrŒi }	||||||||	dœ}t| t	ƒr¸t
j | ¡} |dkrÒt| t	ƒrÒ| |d< tj| |
d��$}tj|dd� ||¡W  5 Q R £ S Q R X 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)Ú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ÚstrÚosÚpathÚ
expanduserr   Úget_xml_iteratorr   ÚVOTableFiler   )Úsourcer   r   r   r   r   Útable_idr   r   r   r   r   Zconf_verify_lowercaser   Úiterator© r1   úW/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/io/votable/table.pyr   "   sH    `ÿ


ø
 ÿc                 K   s*   |  d¡dkrd|d< t| f|Ž}| ¡ 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Úvotabler1   r1   r2   r	   ´   s    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.tabler6   r$   r   r-   r
   Ú	TypeErrorZto_xml)ÚtableÚfiler7   r6   r1   r1   r2   r   È   s    
ÿ  ÿc                 C   sè  ddl m}m} d}|dkr(t ¡ }d}g }d}tƒ  t| tƒrLtj	 
| ¡} tj| dd��}	|	 ¡ }
W 5 Q R X t |
¡}| d¡ |dkrÂt| tƒrš| }n(t| dƒr¬| j}nt| d	ƒr¾| j}nd
}tjdd��f}t ¡  tjdtjdd� zt|d|d�}W n2 tk
�r. } z| t|ƒ¡ W 5 d}~X Y nX W 5 Q R X dd„ |D ƒ| }| d¡ | d|› d�¡ t|ƒ�r>t |¡}|D ]º}t  |¡}|d �s²| |d ¡ | d¡ n|||d d  }|d }|d �rÚd}nd}||d d›d�d|�pød|ddt!j"|d d d!d"� #¡ |d#� |||d$ |d#� | d%¡ �q€n
| d&¡ d}|�rÄtj	 $|¡�rÄdd'l%m&} |dk�r|d(}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   )Úcolor_printÚprint_code_lineFNTÚbinary)ÚencodingÚnameÚurlz	<unknown>)ÚrecordÚalways)Úappendr   )r   r   c                 S   s$   g | ]}t |jtjƒrt|jƒ‘qS r1   )Ú
issubclassÚcategoryr   Ú	VOWarningr(   Úmessage)Ú.0Úxr1   r1   r2   Ú
<listcomp>/  s   þzvalidate.<locals>.<listcomp>zValidation report for z

Zis_somethingrG   Znliner   ÚwarningZ
is_warningÚyellowÚredÚdz: r!   ZEXCz
          z  )Úinitial_indentÚsubsequent_indent)r:   ZncharÚ
z)astropy.io.votable found no violations.

)Úxmlutilz1.1zxmllint schema violations:

zutf-8zxmllint passed
)+Zastropy.utils.consoler;   r<   ÚioÚStringIOr   r$   r(   r)   r*   r+   r   Zget_readable_fileobjÚreadÚBytesIOÚseekÚhasattrr?   r@   ÚwarningsÚcatch_warningsÚresetwarningsÚsimplefilterr   rF   r   r#   rC   ÚwriteÚlenr   Zxml_readlinesZparse_vowarningÚtextwrapÚfillÚlstripÚexistsr!   rR   ÚversionZvalidate_schemaÚdecodeÚgetvalue)r.   ÚoutputZxmllintr   r;   r<   Zreturn_as_strÚlinesr5   ÚfdÚcontentZcontent_bufferZwarning_linesÚeZ	xml_linesrK   ÚwÚlineÚcolorÚsuccessrR   rc   ÚstdoutÚstderrr1   r1   r2   r   è   sž    !





*þü





ýô




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
   )r9   r/   r1   r1   r2   r
   p  s    c              	   C   s¸   t | tƒrtj | ¡} z†t | ¡�r}|D ]*\}}}}|dkrN W 5 Q R £ W dS  qTq(|D ]*\}}}}|dkr~ W 5 Q R £ W dS  q„qXW 5 Q R £ W dS Q R X W n tk
r²   Y dS X 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.
    ÚxmlFZVOTABLETN)r$   r(   r)   r*   r+   r   r,   r#   )r.   r0   ÚstartÚtagrN   r    r1   r1   r2   Ú
is_votable…  s    
rt   c               	   C   sD   ddl m} m} | tt|fD ]"}z|`W q tk
r<   Y qX 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   )Ú
convertersrR   N)r!   ru   rR   r   r   Ú__warningregistry__ÚAttributeError)ru   rR   Úmoduler1   r1   r2   r   §  s    )N)N)Ú__doc__rS   r)   Úsysr_   rY   Zastropy.utilsr   Zastropy.utils.decoratorsr   Zastropy.utils.xmlr   r!   r   r   Ú__all__r&   ZDEFAULT_CHUNK_SIZEr   r	   r   ro   r   r
   rt   r   r1   r1   r1   r2   Ú<module>   sJ   ú	
õ 
  	
"