
    HR-e6                         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 g dZg d	Z e	d
dd          dddej        ddddddf
d            Zd ZddZej        ddfdZddZd Z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	exceptionpedanticverifyz5.0)sincer   Fc           	         ddl m} |                                }|dvrt          d          |&|j                                        }|dv r|dk    }n|}t          |t                    r|rdnd	}n3|t          vr*t          d
d                    t                               |	i }	||||||||	d}t          | t                    rt          j                            |           } |t          | t                    r| |d<   t          j        | |
          5 }t          j        |d                              ||          cddd           S # 1 swxY w 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)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   conf_verify_lowercaser#   iterators                  8lib/python3.11/site-packages/astropy/io/votable/table.pyr	   r	   "   s    @ mmooG+++P
 
 	
 ~ $ 1 1 3 3
 !$555*f4FF*F&$ P &2F	~	%	%NCHH^4L4LNNOOO  $",	 	F &# ,##F++Jvs33#z		$+E
 
 
 S	v6:::@@6RRS S S S S S S S S S S S S S S S S Ss   *EEEc                 x    |                     d          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	   get_first_table)r2   kwargsvotables      r6   r
   r
      sG     zz.!!)!"~F%%f%%G""$$$    c                     ddl m} t          | |          r t          j                            |           } n)t          | t          j                  st          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)astropy.tabler>   r(   r   r1   r   	TypeErrorto_xml)tablefiler?   r>   s       r6   r   r      s    $ $#####% 
 ++E22t/00 
+
 
 	
 
LL/D      r<   c                    ddl m}m} d}|t          j                    }d}g }d}t                       t          | t                    rt          j	        
                    |           } t          j        | d          5 }	|	                                }
ddd           n# 1 swxY w Y   t          j        |
          }|                    d           |Jt          | t                    r| }n2t!          | d          r| j        }nt!          | d	          r| j        }nd
}t'          j        d          5 }t'          j                     t'          j        dt.          j        d           	 t3          |d|          }n9# t4          $ r,}|                    t          |                     Y d}~nd}~ww xY wddd           n# 1 swxY w Y   d |D             |z   }|                    d           |                    d| d           t;          |          rt=          j        |          }|D ]}t/          j         |          }|d         s1|                    |d                    |                    d           n||d         dz
           }|d         }|d         rd}nd} ||d         ddd|pd|ddtC          j"        |d         dd !          #                                |"            |||d#         |"           |                    d$           n|                    d%           d}|rt          j	        $                    |          rdd&l%m&} |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                 t    g | ]5}t          |j        t          j                  !t	          |j                  6S  )
issubclasscategoryr   	VOWarningr,   message).0xs     r6   
<listcomp>zvalidate.<locals>.<listcomp>.  sE       aj*"677AI  r<   zValidation report for z

is_somethingrT   nliner   warning
is_warningyellowreddz: r%   EXCz
          z  )initial_indentsubsequent_indent)rD   nchar
z)astropy.io.votable found no violations.

)xmlutilz1.1zxmllint schema violations:

zutf-8zxmllint passed
)+astropy.utils.consolerF   rG   ioStringIOr   r(   r,   r-   r.   r/   r   get_readable_fileobjreadBytesIOseekhasattrrJ   rK   warningscatch_warningsresetwarningssimplefilterr   rS   r	   r'   rN   writelenr   xml_readlinesparse_vowarningtextwrapfilllstripexistsr%   rd   versionvalidate_schemadecodegetvalue)r2   outputxmllintr   rF   rG   return_as_strlinesr;   fdcontentcontent_bufferwarning_linese	xml_linesrZ   wlinecolorsuccessrd   ry   stdoutstderrs                           r6   r   r      s   @ CBBBBBBBM~EG&# ,##F++		"6H	=	=	= ''))              Z((Nfc"" 	#HHVV$$ 	#{HHVU## 	#zHH"H		 	-	-	- !   h
(<TJJJJ	!N6HMMMGG 	! 	! 	!LLQ        	!! ! ! ! ! ! ! ! ! ! ! ! ! ! !    		E 
LL8(888999
5zz !D,^<<	 	 	G*733A^$ ?Qy\***V$$$$ 7a0I,\? "$EE!Ez''''$uM)'3*.   fhh     ajv>>>>LL9	< 	BCCCG -27>>(++ -?GGoG")"9"9(G"L"La<<LL9:::LLw//0000LL+,,, !   u::?+w!|+sN   >BB#&B#45G*E=<G=
F3"F.)G.F33GG	Gc                 D    t           j                            | |          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
    )r3   )r   r1   r   )rC   r3   s     r6   r   r   o  s     $ &&ux&@@@r<   c                 f   t          | t                    rt          j                            |           } 	 t          j        |           5 }|D ]\  }}}}|dk    r ddd           dS  |D ]\  }}}}|dk    r ddd           dS  	 ddd           dS # 1 swxY w Y   dS # t          $ r Y dS w xY w)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.
    xmlNFVOTABLET)r(   r,   r-   r.   r/   r   r0   r'   )r2   r5   starttagr^   r$   s         r6   
is_votabler     sm    &# ,##F++(00 	H&.  "sAs%<< 	 	 	 	 	 	 	 	 &.  "sAs)## 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	    uusM   B" 
BB" (B9B" BB" BB" BB" "
B0/B0c                  b    ddl m} m} | t          t          |fD ]}	 |`# t          $ r Y w xY w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   )
convertersrd   N)r%   r   rd   r   r   __warningregistry__AttributeError)r   rd   modules      r6   r   r     sn     &%%%%%%% z49  	** 	 	 	D	 s   
,,)N)__doc__rf   r-   sysru   rm   astropy.utilsr   astropy.utils.decoratorsr   astropy.utils.xmlr   r%   r   r   __all__r*   DEFAULT_CHUNK_SIZEr	   r
   r   r   r   r   r   r   rP   r<   r6   <module>r      s    
			 				 



         @ @ @ @ @ @ ( ( ( ( ( (           100 Z??? &$NS NS NS @?NSb% % %(   @  J D, D, D, D,NA A A A*  D    r<   