σ
ίΘ[c           @` s  d  d l  m Z m Z m Z m Z d  d l Z d d l m 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   Z d e d  Z d e d d  Z e j d e e  e j d e e  e j d e e  d S(   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   sixi   (   t   parset
   from_table(   t   VOTableFilet   Tablei   (   t   registry(   R   (   t
   BaseColumn(   t   Quantityc         O` s   d d l  m } |  d k r | d k	 rM z | |  } Wd | j d  X| S| d k	 rc | |  St | d t t f  r t St Sn t Sd S(   u>  
    Reads the header of a file to determine if it is a VOTable file.

    Parameters
    ----------
    origin : 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.
    i   (   t
   is_votableu   readNi    (	   t    R   t   Nonet   seekt
   isinstanceR   t   VOTablet   Truet   False(   t   origint   filepatht   fileobjt   argst   kwargsR   t   result(    (    s9   lib/python2.7/site-packages/astropy/io/votable/connect.pyR      s    
c         C` sΗ  t  |  t t f  s* t |  d | }  n  t   } g  } t  |  t  rx@ |  j   D]2 } | j d
 k	 rz | | | j <n  | j |  qU Wt	 |  d k rq| d
 k rΩ t
 d j d j | j    t	 |     qt  | t j  r| | k r| | } qnt
 d j |    qt  | t j  r| t	 |  k  rM| | } qnt d j | t	 |     qq·t	 |  d k r| d } q·t
 d   n t  |  t  r·|  } n  | j d	 |  S(   u,  
    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.
    t   table_idi   u   Multiple tables found: table id should be set via the table_id= argument. The available tables are {0}, or integers less than {1}.u   , u   No tables with id={0} foundu1   Table index {0} is out of range. {1} tables foundi    u   No table foundt   use_names_over_idsN(   R   R   R   R   t   dictt   iter_tablest   IDR   t   appendt   lent
   ValueErrort   formatt   joint   keysR   t   string_typest   integer_typest
   IndexErrort   to_table(   t   inputR   R   t   table_id_mappingt   tablest   table(    (    s9   lib/python2.7/site-packages/astropy/io/votable/connect.pyt   read_table_votable1   s>    	'	c   	      C` sΠ   |  j  j t t f  } | rU g  | D] } | j j ^ q% } t d j |    n  t | t	 j
  r§ t j j |  r§ | r t j |  q§ t d j |    n  t |  d | } | j | d | d S(   u°  
    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:`votable-serialization`.
    u6   cannot write table with mixin column(s) {0} to VOTableu   File exists: {0}R   t   tabledata_formatN(   t   columnst   not_isinstanceR
   R   t   infot   nameR!   R"   R   R   R%   t   ost   patht   existst   removet   IOErrorR   t   to_xml(	   R)   t   outputR   t	   overwriteR.   t   unsupported_colst   colt   unsupported_namest
   table_file(    (    s9   lib/python2.7/site-packages/astropy/io/votable/connect.pyt   write_table_votablev   s    	$u   votable(   t
   __future__R    R   R   R   R3   t   externR   R   R   R   t   treeR   R   R   R	   t   io_registryR,   t   table.columnR
   t   unitsR   R   R   R   R-   R?   t   register_readert   register_writert   register_identifier(    (    (    s9   lib/python2.7/site-packages/astropy/io/votable/connect.pyt   <module>   s   "	 E.