U
    e»{jêC  ã                   @   sð   d Z ddlmZmZ ddlmZmZ G dd„ dejƒZG dd„ deƒZG dd	„ d	ej	ƒZ
G d
d„ dejƒZG dd„ dejƒ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G dd„ deƒZG dd„ deƒZdS )zßAn extensible ASCII table reader and writer.

fixedwidth.py:
  Read or write a table with fixed width columns.

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
é   )ÚbasicÚcore)ÚDefaultSplitterÚInconsistentTableErrorc                   @   s,   e Zd ZdZdZdZdZdd„ Zdd„ Zd	S )
ÚFixedWidthSplittera™  
    Split line based on fixed start and end positions for each ``col`` in
    ``self.cols``.

    This class requires that the Header class will have defined ``col.start``
    and ``col.end`` for each column.  The reference to the ``header.cols`` gets
    put in the splitter object by the base Reader.read() function just in time
    for splitting data lines by a ``data`` object.

    Note that the ``start`` and ``end`` positions are defined in the pythonic
    style so line[start:end] is the desired substring for a column.  This splitter
    class does not have a hook for ``process_lines`` since that is generally not
    useful for fixed-width input.

    Ú Fú|c                 #   sD   |D ]:‰ ‡ fdd„ˆj D ƒ}ˆjr8‡fdd„|D ƒV  q|V  qd S )Nc                    s   g | ]}ˆ |j |j… ‘qS © )ÚstartÚend©Ú.0Úx©Úliner	   úZ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/io/ascii/fixedwidth.pyÚ
<listcomp>'   s     z/FixedWidthSplitter.__call__.<locals>.<listcomp>c                    s   g | ]}ˆ   |¡‘qS r	   )Úprocess_valr   ©Úselfr	   r   r   )   s     )Úcolsr   )r   ÚlinesÚvalsr	   )r   r   r   Ú__call__%   s
    zFixedWidthSplitter.__call__c                 C   sf   | j pd}| jpd}|| | }| jr8|| }|| }nd}d}dd„ t||ƒD ƒ}|| |¡ | S )Nr   c                 S   s$   g | ]\}}d |t |ƒ  | ‘qS )ú ©Úlen)r   ÚvalÚwidthr	   r	   r   r   7   s     z+FixedWidthSplitter.join.<locals>.<listcomp>)Údelimiter_padÚ	delimiterÚbookendÚzipÚjoin)r   r   ÚwidthsÚpadr    Zpadded_delimZbookend_leftZbookend_rightr	   r	   r   r#   -   s    


zFixedWidthSplitter.joinN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r    r   r#   r	   r	   r	   r   r      s   r   c                   @   s   e Zd ZdZdZdS )ÚFixedWidthHeaderSplitterz$Splitter class that splits on ``|``.r   N©r&   r'   r(   r)   r    r	   r	   r	   r   r*   ;   s   r*   c                   @   s@   e Zd ZdZeZdZedƒZdd„ Z	dd„ Z
dd	„ Zd
d„ ZdS )ÚFixedWidthHeaderz*
    Fixed width table header reader.
    Nz`~!#$%^&*-_+=\|":'c                 C   s0   t |  |¡ƒD ]\}}||kr q,qtdƒ‚|S )NzNo header line found in table)Ú	enumerateÚprocess_linesr   )r   r   ÚindexÚir   r	   r	   r   Úget_lineL   s
    zFixedWidthHeader.get_linec                    sì  t ˆ ddgƒ}t ˆ jˆ  |¡¡}t ˆ jˆ  |¡¡}|dkrš|dk	rNtdƒ‚ˆ jj}|sbt	dƒ‚ˆ  
|d ¡\}}}‡ fdd„td	t|ƒd	 ƒD ƒˆ _n¶|dk	�r(ˆ  ||¡}	tt|	ƒˆ jjd
h ƒd	krÔt	dƒ‚ˆ j ˆ jjd
h¡}
t|	ƒ |
¡�st	d|
› �ƒ‚ˆ  
|	¡\}ˆ _}dd„ |D ƒˆ _ˆ  ||| d¡ ¡}	ˆ  
|	¡\ˆ _}}ˆ  ¡  t|ƒD ]\\}}|dk�r`ˆ  ||| ¡}	ˆ  
|	¡d }tˆ j|ƒD ]\}}|�rœt|||ƒ �qœ�q`tˆ jƒD ]\}}|| |_|| |_�qÈdS )a:  
        Initialize the header Column objects from the table ``lines``.

        Based on the previously set Header attributes find or create the column names.
        Sets ``self.cols`` with the list of Columns.

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

        Úheader_rowsÚnameNz:Cannot set position_line without also setting header_startz7No data lines found so cannot autogenerate column namesé    c                    s   g | ]}ˆ j  |¡‘qS r	   )Zauto_formatÚformat)r   r0   r   r	   r   r   |   s     z-FixedWidthHeader.get_cols.<locals>.<listcomp>r   r   z]Position line should only contain delimiters and one other character, e.g. "--- ------- ---".z,Characters in position line must be part of c                 S   s    g | ]}|d k	r|d nd ‘qS ©Nr   r	   r   r	   r	   r   r       s     )Úgetattrr   Z_get_line_indexÚ
start_liner.   Úposition_lineÚ
ValueErrorÚdataÚ
data_linesr   Úget_fixedwidth_paramsÚranger   Únamesr1   ÚsetÚsplitterr    Úset_of_position_line_charactersÚunionÚissubsetÚ
col_startsÚcol_endsr/   Z_set_cols_from_namesr-   r"   r   Úsetattrr
   r   )r   r   r2   r8   r9   r<   r   ÚstartsÚendsr   ÚcharsetrF   ÚiiÚattrÚcolr   r0   r	   r   r   Úget_colsT   s\     ÿÿÿ$
ÿ	
ÿÿ

zFixedWidthHeader.get_colsc                    s�  | j dk	rb| jdk	rbt| j ƒ}dd„ | jD ƒ}t|ƒt|ƒkrFtdƒ‚‡ fdd„t||ƒD ƒ}�n$| j dk�r"| jdk�r"ˆ  | jj¡}dg}g }|D ]B}|rÈ| 	|d t|ƒ ¡ | 	|d d ¡ q–|d  d7  < q–|dd… }d	d„ |D ƒ}t|ƒt|ƒk�st|ƒt|ƒk�r†t
d
ƒ‚nd| j dk	�rLt| j ƒ}|dd… dg }n"dd„ | jD ƒ}dg|dd…  }‡ fdd„t||ƒD ƒ}|||fS )a¾  
        Split ``line`` on the delimiter and determine column values and
        column start and end positions.  This might include null columns with
        zero length (e.g. for ``header row = "| col1 || col2 | col3 |"`` or
        ``header2_row = "----- ------- -----"``).  The null columns are
        stripped out.  Returns the values between delimiters and the
        corresponding start and end positions.

        Parameters
        ----------
        line : str
            Input line

        Returns
        -------
        vals : list
            List of values.
        starts : list
            List of starting indices.
        ends : list
            List of ending indices.

        Nc                 S   s    g | ]}|d k	r|d nd ‘qS r6   r	   r   r	   r	   r   r   Õ   s     z:FixedWidthHeader.get_fixedwidth_params.<locals>.<listcomp>z=Fixed width col_starts and col_ends must have the same lengthc                    s    g | ]\}}ˆ ||…   ¡ ‘qS r	   ©Ústrip©r   r
   r   r   r	   r   r   Ú   s     r4   éÿÿÿÿr   c                 S   s   g | ]}|r|  ¡ ‘qS r	   rO   r   r	   r	   r   r   ç   s      z Error parsing fixed width headerc                 S   s   g | ]}|d  ‘qS )r   r	   r   r	   r	   r   r   ð   s     c                    s    g | ]\}}ˆ ||…   ¡ ‘qS r	   rO   rQ   r   r	   r   r   ò   s     )rE   rF   Úlistr   r:   r"   ÚsplitrA   r    Úappendr   )r   r   rH   rI   r   r   r	   r   r   r=   µ   s8    
ÿ$

z&FixedWidthHeader.get_fixedwidth_paramsc                 C   s   d S ©Nr	   )r   r   r	   r	   r   Úwriteö   s    zFixedWidthHeader.write)r&   r'   r(   r)   r*   Úsplitter_classr9   r@   rB   r1   rN   r=   rW   r	   r	   r	   r   r,   A   s   aAr,   c                   @   s    e Zd ZdZeZdZdd„ ZdS )ÚFixedWidthDataz!
    Base table data reader.
    Nc                    sB  ˆj jd krg ndg}tˆd|ƒ}g ‰ˆ ¡ }t|Ž D ]}ˆ |¡ q6g }|D ]$‰ ‡ ‡fdd„ˆjD ƒ}| |¡ qN‡fdd„ttˆjƒƒD ƒ}|rÌttˆjƒƒD ](‰t	|ˆ t	‡fdd„|D ƒƒƒ|ˆ< q¢|D ]}| ˆj
 ||¡¡ qÐˆj jd k	�r‡fdd„|D ƒ}| ˆj
 ||¡¡ ˆD ]}| ˆj
 ||¡¡ �q"|S )	Nr3   r2   c                    s,   g | ]$}t |jˆ ƒ ‰d kr dntˆƒ‘qS )Nr   )r7   ÚinfoÚstr)r   rM   )Úcol_attrr   r	   r   r     s   ÿz(FixedWidthData.write.<locals>.<listcomp>c                    s"   g | ]‰ t ‡ fd d„ˆD ƒƒ‘qS )c                 3   s   | ]}t |ˆ  ƒV  qd S rV   r   ©r   r   ©Úi_colr	   r   Ú	<genexpr>  s     z2FixedWidthData.write.<locals>.<listcomp>.<genexpr>)Úmax)r   )Ú	vals_listr^   r   r     s   ÿc                 3   s   | ]}t |ˆ  ƒV  qd S rV   r   r]   r^   r	   r   r`   !  s     z'FixedWidthData.write.<locals>.<genexpr>c                    s   g | ]}ˆ j j| ‘qS r	   )ÚheaderÚposition_char)r   r   r   r	   r   r   )  s     )rc   r8   r7   Zstr_valsr"   rU   r   r>   r   ra   rA   r#   r9   )r   r   Zdefault_header_rowsr2   Zcol_str_itersr   Z	hdrs_listr$   r	   )r\   r_   r   r   rb   r   rW     s:    þ
þ ÿ
zFixedWidthData.write)r&   r'   r(   r)   r   rX   r8   rW   r	   r	   r	   r   rY   ü   s   rY   c                       s2   e Zd ZdZdZdZeZeZ	d	‡ fdd„	Z
‡  ZS )
Ú
FixedWidtha_  Fixed width table with single header line defining column names and positions.

    Examples::

      # Bar delimiter in header and data

      |  Col1 |   Col2      |  Col3 |
      |  1.2  | hello there |     3 |
      |  2.4  | many words  |     7 |

      # Bar delimiter in header only

      Col1 |   Col2      | Col3
      1.2    hello there    3
      2.4    many words     7

      # No delimiter with column positions specified as input

      Col1       Col2Col3
       1.2hello there   3
       2.4many words    7

    See the :ref:`astropy:fixed_width_gallery` for specific usage examples.

    Zfixed_widthzFixed widthNr   Tc                    sh   |d krdg}t ƒ  ¡  || jj_|| jj_|| j_|| j_|| j_	|| j_	| jj
d krdt|ƒ| j_
d S )Nr3   )ÚsuperÚ__init__r;   rA   r   r!   rc   rE   rF   r2   r8   r   )r   rE   rF   r   r!   r2   ©Ú	__class__r	   r   rg   S  s    


zFixedWidth.__init__)NNr   TN)r&   r'   r(   r)   Ú_format_nameÚ_descriptionr,   Úheader_classrY   Ú
data_classrg   Ú__classcell__r	   r	   rh   r   re   2  s        úre   c                   @   s   e Zd ZdZdZdS )ÚFixedWidthNoHeaderHeaderz7Header reader for fixed with tables with no header lineN©r&   r'   r(   r)   r8   r	   r	   r	   r   ro   h  s   ro   c                   @   s   e Zd ZdZdZdS )ÚFixedWidthNoHeaderDataz6Data reader for fixed width tables with no header liner4   Nrp   r	   r	   r	   r   rq   n  s   rq   c                       s2   e Zd ZdZdZdZeZeZ	d	‡ fdd„	Z
‡  ZS )
ÚFixedWidthNoHeadera?  Fixed width table which has no header line.

    When reading, column names are either input (``names`` keyword) or
    auto-generated.  Column positions are determined either by input
    (``col_starts`` and ``col_stops`` keywords) or by splitting the first data
    line.  In the latter case a ``delimiter`` is required to split the data
    line.

    Examples::

      # Bar delimiter in header and data

      |  1.2  | hello there |     3 |
      |  2.4  | many words  |     7 |

      # Compact table having no delimiter and column positions specified as input

      1.2hello there3
      2.4many words 7

    This class is just a convenience wrapper around the ``FixedWidth`` reader
    but with ``header_start=None`` and ``data_start=0``.

    See the :ref:`astropy:fixed_width_gallery` for specific usage examples.

    Zfixed_width_no_headerzFixed width with no headerNr   Tc                    s   t ƒ j||||g d� d S )N©r   r!   r2   )rf   rg   )r   rE   rF   r   r!   rh   r	   r   rg   •  s    ûzFixedWidthNoHeader.__init__)NNr   T)r&   r'   r(   r)   rj   rk   ro   rl   rq   rm   rg   rn   r	   r	   rh   r   rr   t  s   rr   c                   @   s   e Zd ZdZeZdS )ÚFixedWidthTwoLineHeaderzàHeader reader for fixed width tables splitting on whitespace.

    For fixed width tables with several header lines, there is typically
    a white-space delimited format line, so splitting on white space is
    needed.
    N)r&   r'   r(   r)   r   rX   r	   r	   r	   r   rt   Ÿ  s   rt   c                   @   s   e Zd ZdZdZdS )ÚFixedWidthTwoLineDataSplitterz5Splitter for fixed width tables splitting on ``' '``.r   Nr+   r	   r	   r	   r   ru   ª  s   ru   c                   @   s   e Zd ZdZeZdS )ÚFixedWidthTwoLineDataz8Data reader for fixed with tables with two header lines.N)r&   r'   r(   r)   ru   rX   r	   r	   r	   r   rv   °  s   rv   c                       s2   e Zd ZdZdZdZeZeZ	d	‡ fdd„	Z
‡  ZS )
ÚFixedWidthTwoLineaÕ  Fixed width table which has two header lines.

    The first header line defines the column names and the second implicitly
    defines the column positions.

    Examples::

      # Typical case with column extent defined by ---- under column names.

       col1    col2         <== header_start = 0
      -----  ------------   <== position_line = 1, position_char = "-"
        1     bee flies     <== data_start = 2
        2     fish swims

      # Pretty-printed table

      +------+------------+
      | Col1 |   Col2     |
      +------+------------+
      |  1.2 | "hello"    |
      |  2.4 | there world|
      +------+------------+

    See the :ref:`astropy:fixed_width_gallery` for specific usage examples.

    Zfixed_width_two_linez#Fixed width with second header lineNú-Fc                    sb   t |ƒdkrtd|› d�ƒ‚tƒ j|||d� |d krBt | jjƒ}|| j_|| j_|d | j_	d S )Nr   zPosition_char="z" must be a single characterrs   )
r   r:   rf   rg   rc   r2   r9   rd   r;   r8   )r   r9   rd   r   r!   r2   rh   r	   r   rg   ×  s    
ÿ  ÿzFixedWidthTwoLine.__init__)Nrx   NFN)r&   r'   r(   r)   rj   rk   rv   rm   rt   rl   rg   rn   r	   r	   rh   r   rw   ¶  s        úrw   N)r)   r   r   r   r   r   ZBaseSplitterr   r*   ZBasicHeaderr,   Z	BasicDatarY   ZBasicre   ro   rq   rr   rt   ru   rv   rw   r	   r	   r	   r   Ú<module>   s   
+ <66+