ó
šßÈ[c           @   s  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 l m	 Z	 d d	 l
 m Z d d
 l m Z e j e j ƒ d e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   OrderedDicti   (   t   corei   (   t   six(   t   Table(   t   cparser(   t   zip(   t
   set_localet	   FastBasicc           B   sq   e  Z d  Z d Z d Z e Z e Z e Z	 e Z
 i  d „ Z d „  Z d „  Z d „  Z d „  Z e e d „ Z RS(	   sF  
    This class is intended to handle the same format addressed by the
    ordinary :class:`Basic` writer, but it acts as a wrapper for underlying C
    code and is therefore much faster. Unlike the other ASCII readers and
    writers, this class is not very extensible and is restricted
    by optimization requirements.
    t
   fast_basics9   Basic table with custom delimiter using the fast C enginec         K   sk  | j  d d ƒ d  k	 r? | j  d d ƒ d  k r? t d ƒ ‚ n  | j ƒ  } | j | ƒ | j d d ƒ } | d  k	 r‚ t | ƒ n d  |  _ | j  d d ƒ |  _ | j d d ƒ |  _	 |  j	 d  k	 rÙ t |  j	 ƒ |  _	 n  t | j d	 d
 ƒ ƒ |  _
 | j d d ƒ |  _ | j  d |  j d  k	 r.|  j d n d ƒ } | j d | ƒ |  _ | |  _ t |  _ t |  _ d  S(   Nt   header_starti    s2   header_start cannot be set to None for this Readert	   delimitert    t   comments   # t   #t	   quotechart   "t
   data_starti   (   t   gett   Nonet
   ValueErrort   copyt   updatet   popt   strR
   t   write_commentR   R   R	   R   t   kwargst   Truet   strip_whitespace_linest   strip_whitespace_fields(   t   selft   default_kwargst   user_kwargsR   R
   t   data_start_default(    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyt   __init__   s&    !	%		c         C   s   |  j  j ƒ  d  S(   N(   t   enginet   read_header(   R   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyt   _read_header9   s    c   	      C   sä  |  j  d k	 r6 t |  j  ƒ d k r6 t j d ƒ ‚ n„|  j d k rW t j d ƒ ‚ nc|  j d k	 r— |  j d k  r— t |  t ƒ r— t j d ƒ ‚ n#|  j d k  r¸ t j d ƒ ‚ nt |  j	 ƒ d k rß t j d ƒ ‚ nÛ t |  j
 ƒ d k rt j d ƒ ‚ n´ d	 |  j k r't j d
 ƒ ‚ n“ d |  j k rHt j d ƒ ‚ nr d |  j k rit j d ƒ ‚ nQ d |  j k rŠt j d ƒ ‚ n0 d |  j k s¨d |  j k rºt j d ƒ ‚ n  |  j j d t ƒ |  _ t j | |  j |  j d |  j	 d |  j d |  j  d |  j
 d |  j d |  j |  j |  _ |  j ƒ  } |  j ƒ  | d k	 r`| \ } } } n i  } i  } i  } t d ƒ # |  j j | | | ƒ \ } } Wd QXt ƒ  } | r¿| | d <n  t | d t |  j j ƒ  ƒ d | ƒS(    s   
        Read input data (file-like object, filename, list of strings, or
        single string) into a Table and return the result.
        i   s-   The C reader does not support a comment regexs1   The C reader does not allow data_start to be Nonei    sY   The C reader does not allow header_start to be negative except for commented-header filess5   The C reader does not allow data_start to be negatives,   The C reader only supports 1-char delimiterss5   The C reader only supports a length-1 quote charactert
   converterss<   The C reader does not support passing specialized converterst   encodings0   The C reader does not use the encoding parametert	   Outputters1   The C reader does not use the Outputter parametert   Inputters0   The C reader does not use the Inputter parametert   data_Splittert   header_Splitters*   The C reader does not use a Splitter classt   strict_namesR
   R	   R   R   R   t   fill_extra_colst   CNt   commentst   namest   meta(   R   R   t   lenR   t   ParameterErrorR   R	   t
   isinstancet   FastCommentedHeaderR
   R   R   R   t   FalseR+   R   t   CParserR   R   R,   R"   R$   t   check_headerR   t   readR    R   t   listt	   get_names(	   R   t   tablet   conversion_infot   try_intt	   try_floatt
   try_stringt   dataR.   R0   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR8   >   s\    $						
$	c         C   sÚ   |  j  j ƒ  p |  j  j ƒ  } |  j r¬ d d d d d d g } xj | D]_ } t j | ƒ s t | ƒ d k s | d | k s | d | k rF t d	 j | ƒ ƒ ‚ qF qF Wn  |  j	 rÖ t | ƒ d
 k rÖ t d ƒ ‚ n  d  S(   NR   t   ,t   |s   	t   'R   i    iÿÿÿÿs8   Column name {0!r} does not meet strict name requirementsi   s2   Strict name guessing requires at least two columns(
   R"   t   get_header_namesR:   R+   R   t
   _is_numberR1   R   t   formatt   guessing(   R   R/   t   badst   name(    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR7   y   s    		c         C   s   |  j  | | i  ƒ d S(   s   
        Use a fast Cython method to write table data to output,
        where output is a filename or file-like object.
        N(   t   _write(   R   R;   t   output(    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyt   write‰   s    c         C   st   i |  j  d 6|  j d 6|  j d 6|  j d 6} | j | ƒ | j |  j ƒ t j | |  } | j | | | ƒ d  S(   NR
   R   t   strip_whitespaceR   (	   R
   R   R   R   R   R   R   t
   FastWriterRL   (   R   R;   RK   R   t   header_outputt   output_typest   write_kwargst   writer(    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyRJ      s    

(   t   __name__t
   __module__t   __doc__t   _format_namet   _descriptionR   t   _fastR5   R,   RG   R+   R!   R$   R8   R7   RL   RJ   (    (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR      s   		;		t   FastCsvc           B   s8   e  Z d  Z d Z d Z e Z e Z d „  Z d „  Z	 RS(   s	  
    A faster version of the ordinary :class:`Csv` writer that uses the
    optimized C parsing engine. Note that this reader will append empty
    field values to the end of any row with not enough columns, while
    :class:`FastBasic` simply raises an error.
    t   fast_csvs4   Comma-separated values table using the fast C enginec         K   s+   t  t |  ƒ j i d d 6d  d 6|  d  S(   NRA   R
   R   (   t   superRY   R!   R   (   R   R   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR!   «   s    c         C   s*   |  j  | | i t j d f g d 6ƒ d S(   ss   
        Override the default write method of `FastBasic` to
        output masked values as empty fields.
        t    t   fill_valuesN(   RJ   R   t   masked(   R   R;   RK   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyRL   ®   s    (
   RS   RT   RU   RV   RW   R   RX   R,   R!   RL   (    (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyRY   Ÿ   s   	t   FastTabc           B   s)   e  Z d  Z d Z d Z e Z d „  Z RS(   sl   
    A faster version of the ordinary :class:`Tab` reader that uses
    the optimized C parsing engine.
    t   fast_tabs2   Tab-separated values table using the fast C enginec         K   s6   t  t |  ƒ j i d d 6|  t |  _ t |  _ d  S(   Ns   	R
   (   R[   R_   R!   R5   R   R   (   R   R   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR!   ¿   s     	(   RS   RT   RU   RV   RW   R   RX   R!   (    (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR_   ¶   s
   t   FastNoHeaderc           B   s2   e  Z d  Z d Z d Z e Z d „  Z d „  Z RS(   sª   
    This class uses the fast C engine to read tables with no header line. If
    the names parameter is unspecified, the columns will be autonamed with
    "col{}".
    t   fast_no_headers3   Basic table with no headers using the fast C enginec         K   s+   t  t |  ƒ j i d  d 6d d 6|  d  S(   NR	   i    R   (   R[   Ra   R!   R   (   R   R   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR!   Ï   s    c         C   s   |  j  | | i  d d ƒd S(   s€   
        Override the default writing behavior in `FastBasic` so
        that columns names are not included in output.
        RO   N(   RJ   R   (   R   R;   RK   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyRL   Ò   s    (	   RS   RT   RU   RV   RW   R   RX   R!   RL   (    (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyRa   Å   s   	R4   c           B   sD   e  Z d  Z d Z d Z e Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sá   
    A faster version of the :class:`CommentedHeader` reader, which looks for
    column names in a commented line. ``header_start`` denotes the index of
    the header line among all commented lines and is 0 by default.
    t   fast_commented_headers8   Columns name in a commented line using the fast C enginec         K   s5   t  t |  ƒ j i  |  d | k r1 d |  _ n  d  S(   NR   i    (   R[   R4   R!   R   (   R   R   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR!   ä   s    c         C   s¢   t  t |  ƒ j | ƒ } d | j k rž |  j } | d k  rV t | j d ƒ | } n  | j d |  | j d | d | j d <| j d sž | j d =qž n  | S(   s   
        Read input data (file-like object, filename, list of strings, or
        single string) into a Table and return the result.
        R.   i    i   (   R[   R4   R8   R0   R	   R1   (   R   R;   t   outt   idx(    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR8   ë   s    	+c         C   sí   |  j  j } g  } xj | j ƒ  D]\ } | j ƒ  } | r | d |  j k r | j | d ƒ t | ƒ |  j d k r{ Pq{ q q Wt | ƒ |  j k r¦ t j	 d ƒ ‚ n  |  j  j
 | |  j g ƒ d |  j  _ |  j  j ƒ  |  j  j
 | ƒ d  S(   Ni    i   s   not enough commented lines(   R"   t   sourcet
   splitlinest   lstripR   t   appendR1   R	   R   t   CParserErrort   setup_tokenizerR#   (   R   t   tmpt   commented_linest   line(    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR$   þ   s    c         C   s   |  j  | | i  d d ƒd S(   sr   
        Override the default writing behavior in `FastBasic` so
        that column names are commented.
        RO   R   N(   RJ   (   R   R;   RK   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyRL     s    (   RS   RT   RU   RV   RW   R   RX   R!   R8   R$   RL   (    (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR4   Ú   s   			t   FastRdbc           B   s;   e  Z d  Z d Z d Z e Z d „  Z d „  Z d „  Z	 RS(   så   
    A faster version of the :class:`Rdb` reader. This format is similar to
    tab-delimited, but it also contains a header line after the column
    name line denoting the type of each column (N for numeric, S for string).
    t   fast_rdbs0   Tab-separated with a type definition header linec         K   s=   t  t |  ƒ j i d d 6d d 6|  t |  _ t |  _ d  S(   Ns   	R
   i   R   (   R[   Ro   R!   R5   R   R   (   R   R   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR!   #  s    '	c         C   s  |  j  j } d } d } x‹ | j ƒ  D]q } | r` | j ƒ  r` | j ƒ  d |  j k r` | } q% | r% | j ƒ  r% | j ƒ  d |  j k r% | } Pq% q% Wt d ƒ ‚ |  j  j | g ƒ d |  j  _ |  j  j	 ƒ  |  j  j
 ƒ  } |  j  j | g ƒ |  j  j g  ƒ |  j  j	 ƒ  t |  j  j
 ƒ  ƒ t | ƒ k rAt d ƒ ‚ n  t d „  | Dƒ ƒ rot d j | ƒ ƒ ‚ n  i  } i  } i  } x{ t |  j  j
 ƒ  | ƒ D]a \ }	 }
 |
 d j ƒ  d k rÝd | |	 <d | |	 <d	 | |	 <qšd	 | |	 <d	 | |	 <d | |	 <qšW|  j  j | ƒ | | | f S(
   NR\   i    s   RDB header requires 2 linessC   RDB header mismatch between number of column names and column typesc         s   s(   |  ] } t  j d  | t  j ƒ Vq d S(   s	   \d*(N|S)$N(   t   ret   matcht
   IGNORECASE(   t   .0t   x(    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pys	   <genexpr>C  s    s5   RDB type definitions do not all match [num](N|S): {0}iÿÿÿÿt   si   (   R"   Rf   Rg   t   stripRh   R   R   Rk   R	   R#   R:   t	   set_namesR1   t   anyRF   R   t   lower(   R   Rl   t   line1t   line2Rn   t   typesR=   R>   R?   RI   t   col_type(    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyR$   (  sF    ,	,!	%



c         C   s   |  j  | | i  d t ƒd S(   s   
        Override the default writing behavior in `FastBasic` to
        output a line with column types after the column name line.
        RP   N(   RJ   R   (   R   R;   RK   (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyRL   X  s    (
   RS   RT   RU   RV   RW   R   RX   R!   R$   RL   (    (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyRo     s   		0(   Rq   t   collectionsR    R\   R   t   externR   R;   R   R   t   extern.six.movesR   t   utilsR   t   add_metaclasst   MetaBaseReadert   objectR   RY   R_   Ra   R4   Ro   (    (    (    s9   lib/python2.7/site-packages/astropy/io/ascii/fastbasic.pyt   <module>   s   ?