σ
ίΘ[c           @` s?  d  Z  d d l m Z m Z m Z m Z d d l m 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 d d d d g Z d d d e j d d d d d e d 
 Z d   Z d d  Z d e d d  Z d d  Z d   Z d   Z d S(   uP   
This file contains a contains the high-level functions to read a
VOTable file.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsi   (   t   sixNi   (   t
   exceptions(   t   tree(   t
   iterparser(   t   datau   parseu   parse_single_tableu
   from_tableu   writetou   validateu   reset_vo_warningsu	   exceptionc      
   C` s  d d l  m } | j   } | d k r7 t d   n  | d k rO | j } n  |	 d k rd i  }	 n  i | d 6| d 6| d 6| d	 6| d
 6| d 6| d 6|	 d 6} | d k rΝ t |  t j  rΝ |  | d <n  t	 j
 |  d |
 ) } t j d | d d  j | |  SWd QXd S(   u  
    Parses a VOTABLE_ xml file (or file-like object), and returns a
    `~astropy.io.votable.tree.VOTableFile` object.

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

    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

    pedantic : bool, optional
        When `True`, raise an error when the file violates the spec,
        otherwise issue a warning.  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.pedantic``, which defaults to False.

    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.2 of
        VOTable, and (probably) ``vounit`` in future versions of the
        spec).

    datatype_mapping : dict of str to str, optional
        A mapping of datatype names to valid VOTable datatype names.
        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.
    i   (   t   confu	   exceptionu   maskuB   accepted values of ``invalid`` are: ``'exception'`` or ``'mask'``.u   columnsu   invalidu   pedanticu
   chunk_sizeu   table_numberu   filenameu   unit_formatu   datatype_mappingt   _debug_python_based_parsert   configt   posN(   u	   exceptionu   mask(   i   i   (   t    R	   t   lowert
   ValueErrort   Nonet   pedantict
   isinstanceR   t   string_typesR   t   get_xml_iteratorR   t   VOTableFilet   parse(   t   sourcet   columnst   invalidR   t
   chunk_sizet   table_numbert   table_idt   filenamet   unit_formatt   datatype_mappingR
   R	   R   t   iterator(    (    s7   lib/python2.7/site-packages/astropy/io/votable/table.pyR      s0    P	
	c         K` s;   | j  d  d k r" d | d <n  t |  |  } | j   S(   u  
    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
    u   table_numberi    N(   t   getR   R   t   get_first_table(   R   t   kwargst   votable(    (    s7   lib/python2.7/site-packages/astropy/io/votable/table.pyt   parse_single_table   s    c         C` sr   d d l  m } t |  |  r4 t j j |   }  n! t |  t j  sU t d   n  |  j | d | d t d S(   uv  
    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 object
        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:`votable-serialization`.
    i   (   t   TableuP   first argument must be astropy.io.vo.VOTableFile or astropy.table.Table instancet   tabledata_formatR
   N(	   t   tableR&   R   R   R   t
   from_tablet	   TypeErrort   to_xmlt   True(   R(   t   fileR'   R&   (    (    s7   lib/python2.7/site-packages/astropy/io/votable/table.pyt   writeto₯   s    c         C` sή  d d l  m } m } | d k r. t j } n  t } | d k rO t j   } n  g  } d } t	   t
 j |  d d  }	 |	 j   }
 Wd QXt j |
  } | j d  | d k rt |  t j  rΟ |  } qt |  d  rκ |  j } qt |  d  r|  j } qd	 } n  t j d
 t  o } t j   t j d t j d t y t | d t d | } Wn& t k
 r} | j t |   n XWd QXg  | D]* } t  | j! t j  rt | j"  ^ q| } | j d  | j# d j$ |   t% |  rt& j' |  } x| D]ν } t j( |  } | d sQ| j# | d  | j# d  n  | | d d } | d } | d rd } n d } | d j$ | d  d | p₯d | d d t) j* | d d d d d  j+   d! | | | | d" d! | | j# d#  qWn | j# d$  d } | r²t, j- j. |  r²d d% l/ m0 } | d k rUd& } n	 | j1 } | j2 | |  \ } } } | d k r’| j# d'  | j# |  q²| j# d(  n  | rΒ| j3   St% |  d k oέ| d k S()   u  
    Prints a validation report for the given file.

    Parameters
    ----------
    source : str or readable file-like object
        Path to a VOTABLE_ xml file or pathlib.path
        object having Path to a VOTABLE_ xml file.

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

    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.
    i   (   t   print_code_linet   color_printt   encodingu   binaryNi    u   nameu   urlu	   <unknown>t   recordu   alwayst   appendR   R   u   Validation report for {0}

u   is_somethingu   messageu   

u   nlinei   u   warningu
   is_warningu   yellowu   redu   {0:d}: u    u   EXCu   : t   initial_indentu
             t   subsequent_indentu     R-   u   ncharu   
u)   astropy.io.votable found no violations.

(   t   validateu   1.1u   xmllint schema violations:

u   xmllint passed
(4   t   utils.consoleR/   R0   R   t   syst   stdoutt   Falset   iot   StringIOt   reset_vo_warningsR   t   get_readable_fileobjt   readt   BytesIOt   seekR   R   R   t   hasattrt   namet   urlt   warningst   catch_warningsR,   t   resetwarningst   simplefilterR   t	   VOWarningR   R   R3   t   strt
   issubclasst   categoryt   messaget   writet   formatt   lenR   t   xml_readlinest   parse_vowarningt   textwrapt   fillt   lstript   ost   patht   existst	   utils.xmlR6   t   versiont   validate_schemat   getvalue(   R   t   outputt   xmllintR   R/   R0   t   return_as_strt   linesR$   t   fdt   contentt   content_buffert   warning_linest   et   xt	   xml_linest   warningt   wt   linet   colort   successR6   RZ   R9   t   stderr(    (    s7   lib/python2.7/site-packages/astropy/io/votable/table.pyR6   Β   s    		
.


	
		
c         C` s   t  j j |  d | S(   uΖ  
    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)   (   R(   R   (    (    s7   lib/python2.7/site-packages/astropy/io/votable/table.pyR)   A  s    c      	   C` s   y| t  j |   g } x+ | D]# \ } } } } | d k r> t SPq Wx+ | D]# \ } } } } | d k rl t SPqJ Wt SWd QXWn t k
 r t SXd S(   u>  
    Reads the header of a file to determine if it is a VOTable file.

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

    Returns
    -------
    is_votable : bool
        Returns `True` if the given file is a VOTable file.
    u   xmlu   VOTABLEN(   R   R   R:   R,   R   (   R   R    t   startt   tagR   R   (    (    s7   lib/python2.7/site-packages/astropy/io/votable/table.pyt
   is_votableV  s    c          C` sO   d d l  m }  m } x2 |  t t | f D] } t | d  r) | ` q) q) Wd S(   uή   
    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.

    i   (   t
   converterst   xmlutilu   __warningregistry__N(   R   Rq   Rr   R   R   RB   t   __warningregistry__(   Rq   Rr   t   module(    (    s7   lib/python2.7/site-packages/astropy/io/votable/table.pyR=   u  s    (   t   __doc__t
   __future__R    R   R   R   t   externR   R;   RV   R8   RS   RE   R   R   R   RY   R   t   utilsR   t   __all__R   t   DEFAULT_CHUNK_SIZER:   R   R%   R.   R6   R)   Rp   R=   (    (    (    s7   lib/python2.7/site-packages/astropy/io/votable/table.pyt   <module>   s.   "				o		