B
    q\:                 @   s   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l	m
Z
 G dd	 d	ejd
ZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    N)OrderedDict   )core)Table)cparser)
set_localec               @   sf   e Zd ZdZdZdZdZdZdZdZ	i fddZ
dd	 Zd
d Zdd Zdd Zdd ZdddZdS )	FastBasicaF  
    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.
    Z
fast_basicz9Basic table with custom delimiter using the fast C engineTFc             K   s   | ddd k	r(| ddd kr(tdt|}|t| |dd}|d k	r^t|nd | _| dd| _|dd| _	| j	d k	rt| j	| _	t|d	d
| _
|dd| _| d| jd k	r| jd nd}|d|| _|| _d| _d| _d S )Nheader_startr   z2header_start cannot be set to None for this Reader	delimiter commentz# #	quotechar"
data_startr   T)get
ValueErrorcopydeepcopyupdatepopstrr
   write_commentr   r   r	   r   kwargsstrip_whitespace_linesstrip_whitespace_fields)selfdefault_kwargsZuser_kwargsr   r
   Zdata_start_default r   9lib/python3.7/site-packages/astropy/io/ascii/fastbasic.py__init__   s&    

zFastBasic.__init__c             C   s   | j   d S )N)engineread_header)r   r   r   r   _read_header<   s    zFastBasic._read_headerc       
   	   C   s6  | j dk	r&t| j dkr&tdn| jdkr<tdn| jdk	rf| jdk rft| tsftdn| jdk r|tdnt| jdkrtdnt| j	dkrtd	n|d
| j
krtdnfd| j
krtdnPd| j
krtdn:d| j
kr
tdn"d| j
ks"d| j
kr,td| j
dd| _| j
dd}t|tsZi }|dd |dd| _|| j
d< tj|| j| jf| j| j| j | j	| j| jd| j
| _|  }|   |dk	r|\}}}ni }i }i }td | j|||\}}W dQ R X | ||}	| jr2| jj|	jd< |	S )z
        Read input data (file-like object, filename, list of strings, or
        single string) into a Table and return the result.
        Nr   z-The C reader does not support a comment regexz1The C reader does not allow data_start to be Noner   zYThe C reader does not allow header_start to be negative except for commented-header filesz5The C reader does not allow data_start to be negativez,The C reader only supports 1-char delimitersz5The C reader only supports a length-1 quote characterZ
convertersz<The C reader does not support passing specialized convertersencodingz0The C reader does not use the encoding parameterZ	Outputterz1The C reader does not use the Outputter parameterZInputterz0The C reader does not use the Inputter parameterZdata_SplitterZheader_Splitterz*The C reader does not use a Splitter classstrict_namesFfast_readerTenablereturn_header_chars)r
   r	   r   r   r   fill_extra_colsCZ"__ascii_fast_reader_header_chars__)r   lenr   ZParameterErrorr   r	   
isinstanceFastCommentedHeaderr
   r   r   r   r%   r   dictr(   r   ZCParserr   r   r)   r!   r#   check_headerr   read
make_tableZheader_charsmeta)
r   tabler&   Zconversion_infotry_int	try_float
try_stringdatacommentsoutr   r   r   r0   A   sh    










zFastBasic.readc             C   s*   t  }|r||d< t|t| j |dS )z:Actually make the output table give the data and comments.r8   )namesr2   )r   r   listr!   	get_names)r   r7   r8   r2   r   r   r   r1      s    zFastBasic.make_tablec             C   s   | j  p| j  }| jrtddddddg}xH|D ]@}t|sbt|dksb|d |ksb|d |kr0td	|q0W | j	rt|d
krtd|d S )Nr   ,|	'r   r   z8Column name {0!r} does not meet strict name requirementsr   z;Table format guessing requires at least two columns, got {})
r!   Zget_header_namesr<   r%   r   Z
_is_numberr+   r   formatguessing)r   r:   Zbadsnamer   r   r   r/      s    

zFastBasic.check_headerc             C   s   |  ||i  dS )z
        Use a fast Cython method to write table data to output,
        where output is a filename or file-like object.
        N)_write)r   r3   outputr   r   r   write   s    zFastBasic.writec             C   sL   | j | j| j| jd}|| || j tj|f|}|||| d S )N)r
   r   Zstrip_whitespacer   )	r
   r   r   r   r   r   r   Z
FastWriterrG   )r   r3   rF   r   header_outputoutput_typesZwrite_kwargswriterr   r   r   rE      s    

zFastBasic._writeN)TF)__name__
__module____qualname____doc___format_name_description_fastr)   rC   r%   r    r#   r0   r1   r/   rG   rE   r   r   r   r   r      s    Gr   )	metaclassc                   s8   e Zd ZdZdZdZdZdZ fddZdd Z	  Z
S )	FastCsva	  
    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.
    Zfast_csvz4Comma-separated values table using the fast C engineTc                s   t  jdd df| d S )Nr=   )r
   r   )superr    )r   r   )	__class__r   r   r       s    zFastCsv.__init__c             C   s   |  ||dtjdfgi dS )zs
        Override the default write method of `FastBasic` to
        output masked values as empty fields.
        Zfill_values N)rE   r   Zmasked)r   r3   rF   r   r   r   rG      s    zFastCsv.write)rK   rL   rM   rN   rO   rP   rQ   r)   r    rG   __classcell__r   r   )rU   r   rS      s   rS   c                   s,   e Zd ZdZdZdZdZ fddZ  ZS )FastTabzl
    A faster version of the ordinary :class:`Tab` reader that uses
    the optimized C parsing engine.
    Zfast_tabz2Tab-separated values table using the fast C engineTc                s$   t  jddif| d| _d| _d S )Nr
   r?   F)rT   r    r   r   )r   r   )rU   r   r   r       s    zFastTab.__init__)	rK   rL   rM   rN   rO   rP   rQ   r    rW   r   r   )rU   r   rX      s
   rX   c                   s4   e Zd ZdZdZdZdZ fddZdd Z  Z	S )	FastNoHeaderz
    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{}".
    Zfast_no_headerz3Basic table with no headers using the fast C engineTc                s   t  jd ddf| d S )Nr   )r	   r   )rT   r    )r   r   )rU   r   r   r       s    zFastNoHeader.__init__c             C   s   | j ||i dd dS )z
        Override the default writing behavior in `FastBasic` so
        that columns names are not included in output.
        N)rH   )rE   )r   r3   rF   r   r   r   rG      s    zFastNoHeader.write)
rK   rL   rM   rN   rO   rP   rQ   r    rG   rW   r   r   )rU   r   rY      s   rY   c                   sD   e Zd ZdZdZdZdZ fddZdd Zd	d
 Z	dd Z
  ZS )r-   z
    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.
    Zfast_commented_headerz8Columns name in a commented line using the fast C engineTc                s"   t  ji f| d|krd| _d S )Nr   r   )rT   r    r   )r   r   )rU   r   r   r       s    zFastCommentedHeader.__init__c             C   sj   t  }|rR| j}|dk r$t|| }|d| ||d d  |d< |d sR|d= t|t| j |dS )z
        Actually make the output table give the data and comments.  This is
        slightly different from the base FastBasic method in the way comments
        are handled.
        r   Nr   r8   )r:   r2   )r   r	   r+   r   r;   r!   r<   )r   r7   r8   r2   idxr   r   r   r1     s     zFastCommentedHeader.make_tablec             C   s   | j j}g }xP| D ]D}| }|r|d | jkr||dd   t|| jd krP qW t|| jkrvt	d| j 
|| j g d| j _| j   | j 
| d S )Nr   r   znot enough commented lines)r!   source
splitlineslstripr   appendr+   r	   r   ZCParserErrorsetup_tokenizerr"   )r   tmpZcommented_linesliner   r   r   r#     s    

z FastCommentedHeader._read_headerc             C   s   | j ||i dd dS )zr
        Override the default writing behavior in `FastBasic` so
        that column names are commented.
        r   )rH   N)rE   )r   r3   rF   r   r   r   rG   &  s    zFastCommentedHeader.write)rK   rL   rM   rN   rO   rP   rQ   r    r1   r#   rG   rW   r   r   )rU   r   r-      s   r-   c                   s<   e Zd ZdZdZdZdZ fddZdd Zd	d
 Z	  Z
S )FastRdbz
    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).
    Zfast_rdbz0Tab-separated with a type definition header lineTc                s&   t  jdddf| d| _d| _d S )Nr?      )r
   r   F)rT   r    r   r   )r   r   )rU   r   r   r    8  s    zFastRdb.__init__c             C   s  | j j}d}d}x`| D ]L}|sB| rB| d | jkrB|}q|s| r| d | jkr|}P qW td| j |g d| j _| j 	  | j 
 }| j |g | j g  | j 	  t| j 
 t|krtdtdd |D rtd|i }i }i }xbt| j 
 |D ]N\}	}
|
d  d	krVd||	< d||	< d
||	< nd
||	< d
||	< d||	< q"W | j | |||fS )NrV   r   zRDB header requires 2 lineszCRDB header mismatch between number of column names and column typesc             s   s    | ]}t d |t j V  qdS )z	\d*(N|S)$N)rematch
IGNORECASE).0xr   r   r   	<genexpr>X  s    z'FastRdb._read_header.<locals>.<genexpr>z5RDB type definitions do not all match [num](N|S): {0}rA   sr   )r!   r[   r\   stripr]   r   r   r_   r	   r"   r<   Z	set_namesr+   r   ZInconsistentTableErroranyrB   ziplower)r   r`   Zline1Zline2ra   typesr4   r5   r6   rD   Zcol_typer   r   r   r#   =  sF    




zFastRdb._read_headerc             C   s   | j ||i dd dS )z
        Override the default writing behavior in `FastBasic` to
        output a line with column types after the column name line.
        T)rI   N)rE   )r   r3   rF   r   r   r   rG   m  s    zFastRdb.write)rK   rL   rM   rN   rO   rP   rQ   r    r#   rG   rW   r   r   )rU   r   rb   .  s   0rb   )rd   r   collectionsr   rV   r   Zastropy.tabler   r   Zastropy.utilsr   ZMetaBaseReaderr   rS   rX   rY   r-   rb   r   r   r   r   <module>   s    *=