ó
šßÈ[c           @   så   d  Z  d d l Z d d l 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 i d
 g d 6Z d Z d Z d e	 j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e	 j f d „  ƒ  YZ d S(   s—   
Define the Enhanced Character-Separated-Values (ECSV) which allows for reading and
writing all the meta data associated with an astropy Table object.
iÿÿÿÿN(   t   OrderedDicti   (   t   sixi   (   t   coret   basic(   t   metat	   serialize(   t   serialize_context_ast   yamlt   Ecsvs   0.9t    t   ,t
   EcsvHeaderc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s–   Header class for which the column definition line starts with the
    comment character.  See the :class:`CommentedHeader` class  for an example.
    c         c   sy   t  j |  j ƒ } x` | D]X } | j ƒ  } | s7 q n  | j | ƒ } | rm | | j ƒ  } | rq | Vqq q d Sq Wd S(   s›   Return only non-blank lines that start with the comment regexp.  For these
        lines strip out the matching characters and leading/trailing whitespace.N(   t   ret   compilet   commentt   stript   matcht   end(   t   selft   linest
   re_commentt   lineR   t   out(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyt   process_lines   s    c         C   sH  |  j  j t k r! t d ƒ ‚ n  xM |  j D]B } t t | d d ƒ ƒ d k r+ t d j | j j	 ƒ ƒ ‚ q+ q+ Wi |  j d 6d d 6} |  j
 r¡ |  j
 | d <n  |  j  j d	 k rÆ |  j  j | d
 <n  d j t ƒ d g t j | ƒ } | j g  | D] } |  j | ^ qõ ƒ | j |  j  j g  |  j D] } | j j	 ^ q(ƒ ƒ d S(   s  
        Write header information in the ECSV ASCII format.  This format
        starts with a delimiter separated list of the column names in order
        to make this format readable by humans and simple csv-type readers.
        It then encodes the full table meta and column attributes and meta
        as YAML and pretty-prints this in the header.  Finally the delimited
        column names are repeated again, for humans and readers that look
        for the *last* comment line as defining the column names.
        s=   only space and comma are allowed for delimiter in ECSV formatt   shapei   s:   ECSV format does not support multidimensional column '{0}'t   colss   astropy-2.0t   schemaR   R	   t	   delimiters	   %ECSV {0}s   ---N(    (   t   splitterR   t
   DELIMITERSt
   ValueErrorR   t   lent   getattrt   formatt   infot   namet
   table_metat   ECSV_VERSIONR   t   get_yaml_from_headert   extendt   write_commentt   appendt   join(   R   R   t   colt   headert   header_yaml_linesR   t   x(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyt   write,   s     
		'c         C   s   d S(   s}   
        Override the default write_comments to do nothing since this is handled
        in the custom write method.
        N(    (   R   R   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyt   write_commentsO   s    c         C   s   d S(   s~   
        Override the default update_meta to do nothing.  This process is done
        in get_cols() for this reader.
        N(    (   R   R   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyt   update_metaV   s    c         C   s|  | } t  |  j | ƒ ƒ } d } d } | s? t j | ƒ ‚ n  t j | | d j ƒ  t j ƒ } | sy t j | ƒ ‚ n  y t j	 | ƒ } Wn# t j
 k
 r± t j d ƒ ‚ n Xd | k rÎ | d |  _ n  d | k r| d } | t k rÿ t d ƒ ‚ n  | |  j _ | |  j j _ n  t d „  | d	 Dƒ ƒ } g  | d	 D] }	 |	 d
 ^ qB|  _ t t t |  ƒ j | ƒ ƒ }
 t |  j |
 g ƒ ƒ } | |  j k r¾t d j |  j | ƒ ƒ ‚ n  |  j ƒ  x­ |  j D]¢ } x? d D]7 } | | | j k rßt | | | | j | ƒ qßqßW| | j d	 | _ t j rS| j d k rSd | _ n  | j j d ƒ rÒt d ƒ ‚ qÒqÒWd S(   s­   
        Initialize the header Column objects from the table ``lines``.

        Parameters
        ----------
        lines : list
            List of table lines

        s›   %ECSV [ ]
                             (?P<major> \d+)
                             \. (?P<minor> \d+)
                             \.? (?P<bugfix> \d+)? $se   ECSV header line like "# %ECSV <version>" not found as first line.  This is required for a ECSV file.i    s#   unable to parse yaml in meta headerR   R   s=   only space and comma are allowed for delimiter in ECSV formatc         s   s   |  ] } | d  | f Vq d S(   R#   N(    (   t   .0R.   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pys	   <genexpr>Ž   s    t   datatypeR#   sS   column names from ECSV header {} do not match names from header line of CSV data {}t   descriptionR!   t   unitt   stringt   strt   complexs1   ecsv reader does not support complex number typesN(   R4   R!   R5   R   (    t   listR   R   t   InconsistentTableErrorR   R   R   t   VERBOSER   t   get_header_from_yamlt   YamlParseErrorR$   R   R   R   R   t   dataR    t   namest   nextt   superR   R!   t   _set_cols_from_namesR   R#   t   setattrt   dtypeR   t   PY2t
   startswitht	   TypeError(   R   R   t	   raw_linest   ecsv_header_ret   no_header_msgR   R,   R   t   header_colsR.   t   header_linet   header_namesR+   t   attr(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyt   get_cols]   sL    "
$	
"(   t   __name__t
   __module__t   __doc__R   R/   R0   R1   RO   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyR      s   		#		t   EcsvOutputterc           B   s   e  Z d  Z g  Z d „  Z RS(   s  
    After reading the input lines and processing, convert the Reader columns
    and metadata to an astropy.table.Table object.  This overrides the default
    converters to be an empty list because there is no "guessing" of the
    conversion function.
    c         C   s.   t  t |  ƒ j | | ƒ } t j | ƒ } | S(   N(   RA   RS   t   __call__R   t   _construct_mixins_from_columns(   R   R   R   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyRT   ·   s    (   RP   RQ   RR   t   default_convertersRT   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyRS   ®   s   c           B   s/   e  Z d  Z d Z d Z e Z e Z d „  Z	 RS(   s%  
    Read a file which conforms to the ECSV (Enhanced Character Separated
    Values) format.  This format allows for specification of key table
    and column meta-data, in particular the data type and unit.  For details
    see: https://github.com/astropy/astropy-APEs/blob/master/APE6.rst.

    Examples
    --------

    >>> from astropy.table import Table
    >>> ecsv_content = '''# %ECSV 0.9
    ... # ---
    ... # datatype:
    ... # - {name: a, unit: m / s, datatype: int64, format: '%03d'}
    ... # - {name: b, unit: km, datatype: int64, description: This is column b}
    ... a b
    ... 001 2
    ... 004 3
    ... '''
    >>> Table.read(ecsv_content, format='ascii.ecsv')
    <Table length=2>
      a     b
    m / s   km
    int64 int64
    ----- -----
      001     2
      004     3
    t   ecsvs   Enhanced CSVc         C   s&   t  d ƒ  t j | ƒ } Wd QX| S(   sÿ  
        Update table columns in place if mixin columns are present.

        This is a hook to allow updating the table columns after name
        filtering but before setting up to write the data.  This is currently
        only used by ECSV and is otherwise just a pass-through.

        Parameters
        ----------
        table : `astropy.table.Table`
            Input table for writing

        Returns
        -------
        table : `astropy.table.Table`
            Output table for writing
        RW   N(   R   R   t   _represent_mixins_as_columns(   R   t   tableR   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyt   update_table_dataè   s    (
   RP   RQ   RR   t   _format_namet   _descriptionR   t   header_classRS   t   outputter_classRZ   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyR   Å   s   (   R	   R
   (   RR   R   t   collectionsR    t
   contextlibt   externR   t    R   R   RY   R   R   t   utils.data_infoR   t   __doctest_requires__R%   R   t   BasicHeaderR   t   TableOutputterRS   t   BasicR   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ecsv.pyt   <module>   s   —