U
    e»{j¥A  ã                   @   s  d Z ddlZddlmZ dddddœddd	d	dœd
ddddddddddddœdœdœZe d¡Zdd„ Zdd„ 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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dS )0zÑAn extensible ASCII table reader and writer.

latex.py:
  Classes to read and write LaTeX tables

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
é    Né   )ÚcoreÚtablez\hline \hlinez\hline)Ú	tabletypeÚheader_startÚ
header_endÚdata_endz\hline\hliner   ÚcaptionÚ
tablealignÚ	col_alignÚpreambler   r   Ú
data_startr   Ú	tablefootzunit of col1zunit of col2)Zcol1Zcol2)r   r	   r
   r   r   r   r   r   r   r   Úunits)ZAAZdoublelinesÚtemplatez(?<!\\)%c                 C   s8   || kr4t | | tƒr&| | | ¡ n| | | ¡ dS )z´
    Add a value from a dictionary to a list

    Parameters
    ----------
    adict : dictionary
    key : hashable
    alist : list
        List where value should be added
    N)Ú
isinstanceÚstrÚappendÚextend)ÚadictÚkeyÚalist© r   úU/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/io/ascii/latex.pyÚadd_dictval_to_list0   s    r   c                 C   s:   t  | dd¡¡}t| ƒD ]\}}| |¡r|  S qdS )a  
    Find the first line which matches a patters

    Parameters
    ----------
    lines : list
        List of strings
    latex : str
        Search pattern

    Returns
    -------
    line_num : int, None
        Line number. Returns None, if no match was found

    ú\ú\\N)ÚreÚcompileÚreplaceÚ	enumerateÚmatch)ÚlinesÚlatexZ	re_stringÚiÚliner   r   r   Úfind_latex_lineB   s
    

r&   c                   @   s   e Zd Zdd„ ZdS )ÚLatexInputterc                 C   s   dd„ |D ƒS )Nc                 S   s   g | ]}|  ¡ ‘qS r   ©Ústrip)Ú.0Zlinr   r   r   Ú
<listcomp>]   s     z/LatexInputter.process_lines.<locals>.<listcomp>r   ©Úselfr"   r   r   r   Úprocess_lines\   s    zLatexInputter.process_linesN)Ú__name__Ú
__module__Ú__qualname__r.   r   r   r   r   r'   [   s   r'   c                       s<   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Zd	d
„ Z‡  Z	S )ÚLatexSplitterz1Split LaTeX table data. Default delimiter is `&`.ú&c                    s8   t  |d ¡d  ¡ }| d¡s,|d |d< tƒ  |¡S )Néÿÿÿÿr   r   )Ú
RE_COMMENTÚsplitr)   ÚendswithÚsuperÚ__call__)r-   r"   Ú	last_line©Ú	__class__r   r   r9   e   s    
zLatexSplitter.__call__c                 C   s:   t  |¡d }| ¡ }| d¡r,| d¡}n
t d¡‚|S )zWRemove whitespace at the beginning or end of line. Also remove
        \ at end of liner   r   z(Lines in LaTeX table have to end with \\)r5   r6   r)   r7   Úrstripr   ÚInconsistentTableError©r-   r%   r   r   r   Úprocess_linel   s    
ÿzLatexSplitter.process_linec                 C   s4   |  ¡ }|r0|d dkr0|d dkr0|dd… }|S )z:Remove whitespace and {} at the beginning or end of value.r   Ú{r4   Ú}r   r(   )r-   Úvalr   r   r   Úprocess_valy   s    zLatexSplitter.process_valc                 C   s&   d| j  d }| dd„ |D ƒ¡d S )z?Join values together and add a few extra spaces for readabilityú c                 s   s   | ]}|  ¡ V  qd S ©Nr(   ©r*   Úxr   r   r   Ú	<genexpr>ƒ   s     z%LatexSplitter.join.<locals>.<genexpr>z \\)Ú	delimiterÚjoin)r-   ÚvalsrJ   r   r   r   rK   €   s    zLatexSplitter.join)
r/   r0   r1   Ú__doc__rJ   r9   r@   rD   rK   Ú__classcell__r   r   r;   r   r2   `   s   r2   c                   @   s0   e Zd ZdZdZeZdd„ Zdd„ Zdd„ Z	d	S )
ÚLatexHeaderz(Class to read the header of Latex Tablesz\begin{tabular}c                 C   s$   t || jƒ}|d k	r|d S d S d S ©Nr   )r&   r   )r-   r"   r%   r   r   r   Ú
start_lineŒ   s    zLatexHeader.start_linec              	   C   sd   i }dd„ | j D ƒ}t| j|ƒD ]>\}}|r z|jdd�||< W q  tk
r\   |||< Y q X q |S )Nc                 S   s   g | ]}|j j‘qS r   )ÚinfoÚunit)r*   Úcolr   r   r   r+   •   s     z*LatexHeader._get_units.<locals>.<listcomp>Zlatex_inline)Úformat)ÚcolsÚzipÚcolnamesZ	to_stringÚAttributeError)r-   r   Z	col_unitsÚnamerS   r   r   r   Ú
_get_units“   s    zLatexHeader._get_unitsc                    s6  d| j krt| jƒd | j d< d| j kr<d| j d  d }nd}| j d d k	rj| d| j d  d	 | ¡ t| j d
|ƒ d| j krš| d| j d  d	 ¡ | | jd | j d  d	 ¡ t| j d|ƒ | | j | j¡¡ |  	¡ ‰ d| j krüˆ  
| j d ¡ ˆ �r$| | j ‡ fdd„| jD ƒ¡¡ t| j d|ƒ d S )Nr   Úcr
   ú[ú]Ú r   ú\begin{rB   r   r	   z	\caption{rA   r   r   c                    s   g | ]}ˆ   |d ¡‘qS ©rE   ©Úget©r*   rZ   ©r   r   r   r+   ²   s     z%LatexHeader.write.<locals>.<listcomp>r   )r#   ÚlenrV   r   r   r   ÚsplitterrK   rX   r[   Úupdate)r-   r"   Úalignr   re   r   Úwritež   s*    



ÿzLatexHeader.writeN)
r/   r0   r1   rM   r   r2   Úsplitter_classrQ   r[   rj   r   r   r   r   rO   †   s   rO   c                   @   s4   e Zd ZdZdZdZeZdd„ Zdd„ Z	dd	„ Z
dS )
Ú	LatexDataz&Class to read the data in LaTeX tablesNz\end{tabular}c                 C   s<   | j rt|| j ƒS | j |¡}|d kr0t d¡‚|d S d S )NzCould not find table startr   )r   r&   ÚheaderrQ   r   r>   )r-   r"   Ústartr   r   r   rQ   ¾   s    
zLatexData.start_linec                 C   s   | j rt|| j ƒS d S d S rF   )r   r&   r,   r   r   r   Úend_lineÇ   s    zLatexData.end_linec                 C   sn   t | jd|ƒ tj | |¡ t | jd|ƒ | | j¡ t | jd|ƒ | jd d k	rj| d| jd  d ¡ d S )Nr   r   r   r   ú\end{rB   )r   r#   r   ÚBaseDatarj   r   r   r,   r   r   r   rj   Í   s    zLatexData.write)r/   r0   r1   rM   r   r   r2   rk   rQ   ro   rj   r   r   r   r   rl   ·   s   	rl   c                       sb   e Zd ZdZdZdgZdZdZeZ	e
ZeZdZdddd	d
dgi ddf‡ fdd„	Zddd„Z‡  ZS )ÚLatexa  LaTeX format table.

    This class implements some LaTeX specific commands.  Its main
    purpose is to write out a table in a form that LaTeX can compile. It
    is beyond the scope of this class to implement every possible LaTeX
    command, instead the focus is to generate a syntactically valid
    LaTeX tables.

    This class can also read simple LaTeX tables (one line per table
    row, no ``\multicolumn`` or similar constructs), specifically, it
    can read the tables that it writes.

    Reading a LaTeX table, the following keywords are accepted:

    **ignore_latex_commands** :
        Lines starting with these LaTeX commands will be treated as comments (i.e. ignored).

    When writing a LaTeX table, the some keywords can customize the
    format.  Care has to be taken here, because python interprets ``\\``
    in a string as an escape character.  In order to pass this to the
    output either format your strings as raw strings with the ``r``
    specifier or use a double ``\\\\``.

    Examples::

        caption = r'My table \label{mytable}'
        caption = 'My table \\\\label{mytable}'

    **latexdict** : Dictionary of extra parameters for the LaTeX output

        * tabletype : used for first and last line of table.
            The default is ``\\begin{table}``.  The following would generate a table,
            which spans the whole page in a two-column document::

                ascii.write(data, sys.stdout, Writer = ascii.Latex,
                            latexdict = {'tabletype': 'table*'})

            If ``None``, the table environment will be dropped, keeping only
            the ``tabular`` environment.

        * tablealign : positioning of table in text.
            The default is not to specify a position preference in the text.
            If, e.g. the alignment is ``ht``, then the LaTeX will be ``\\begin{table}[ht]``.

        * col_align : Alignment of columns
            If not present all columns will be centered.

        * caption : Table caption (string or list of strings)
            This will appear above the table as it is the standard in
            many scientific publications.  If you prefer a caption below
            the table, just write the full LaTeX command as
            ``latexdict['tablefoot'] = r'\caption{My table}'``

        * preamble, header_start, header_end, data_start, data_end, tablefoot: Pure LaTeX
            Each one can be a string or a list of strings. These strings
            will be inserted into the table without any further
            processing. See the examples below.

        * units : dictionary of strings
            Keys in this dictionary should be names of columns. If
            present, a line in the LaTeX table directly below the column
            names is added, which contains the values of the
            dictionary. Example::

              from astropy.io import ascii
              data = {'name': ['bike', 'car'], 'mass': [75,1200], 'speed': [10, 130]}
              ascii.write(data, Writer=ascii.Latex,
                               latexdict = {'units': {'mass': 'kg', 'speed': 'km/h'}})

            If the column has no entry in the ``units`` dictionary, it defaults
            to the **unit** attribute of the column. If this attribute is not
            specified (i.e. it is None), the unit will be written as ``' '``.

        Run the following code to see where each element of the
        dictionary is inserted in the LaTeX table::

            from astropy.io import ascii
            data = {'cola': [1,2], 'colb': [3,4]}
            ascii.write(data, Writer=ascii.Latex, latexdict=ascii.latex.latexdicts['template'])

        Some table styles are predefined in the dictionary
        ``ascii.latex.latexdicts``. The following generates in table in
        style preferred by A&A and some other journals::

            ascii.write(data, Writer=ascii.Latex, latexdict=ascii.latex.latexdicts['AA'])

        As an example, this generates a table, which spans all columns
        and is centered on the page::

            ascii.write(data, Writer=ascii.Latex, col_align='|lr|',
                        latexdict={'preamble': r'\begin{center}',
                                   'tablefoot': r'\end{center}',
                                   'tabletype': 'table*'})

    **caption** : Set table caption
        Shorthand for::

            latexdict['caption'] = caption

    **col_align** : Set the column alignment.
        If not present this will be auto-generated for centered
        columns. Shorthand for::

            latexdict['col_align'] = col_align

    r#   z.texzLaTeX tableNZhlineZvspaceZ	tablelineZtopruleZmidruleZ
bottomruler_   c                    sŠ   t ƒ  ¡  i | _| j| j_| j| j_d| jd< | j |¡ |rH|| jd< |rV|| jd< || _dd dd„ | jD ƒ¡ | j_| jj| j_d S )	Nr   r   r	   r   z%|ú|c                 S   s   g | ]}d | ‘qS )r   r   )r*   Úcommandr   r   r   r+   q  s     z"Latex.__init__.<locals>.<listcomp>)	r8   Ú__init__r#   rm   Údatarh   Úignore_latex_commandsrK   Úcomment)r-   rw   Ú	latexdictr	   r   r;   r   r   ru   S  s    





ÿ
zLatex.__init__c                 C   s    d | j _d | j_tjj| |d�S )N)r   )rm   rQ   rv   r   Ú
BaseReaderrj   )r-   r   r   r   r   rj   u  s    zLatex.write)N)r/   r0   r1   rM   Ú_format_nameÚ_io_registry_format_aliasesÚ_io_registry_suffixÚ_descriptionrO   Úheader_classrl   Ú
data_classr'   Zinputter_classZmax_ndimru   rj   rN   r   r   r;   r   rr   ×   s*   júô"rr   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚAASTexHeaderSplitterzµExtract column names from a `deluxetable`_.

    This splitter expects the following LaTeX code **in a single line**:

        \tablehead{\colhead{col1} & ... & \colhead{coln}}
    c                    s   t t| ƒ |¡S rF   )r8   r2   r9   r,   r;   r   r   r9   ƒ  s    zAASTexHeaderSplitter.__call__c                 C   s^   |  d¡d }| dd¡}| ¡ }|d dkrH|d dkrH|dd… }n
t d	¡‚| d
d¡S )z#extract column names from tableheadú%r   ú
\tableheadr_   rA   r4   rB   r   z\tablehead is missing {}z\colhead)r6   r   r)   r   r>   r?   r   r   r   r@   †  s    
z!AASTexHeaderSplitter.process_linec                 C   s   d  dd„ |D ƒ¡S )Nú & c                 S   s   g | ]}d t |ƒ d ‘qS ©z	\colhead{rB   )r   rG   r   r   r   r+   ’  s     z-AASTexHeaderSplitter.join.<locals>.<listcomp>)rK   )r-   rL   r   r   r   rK   ‘  s    zAASTexHeaderSplitter.join)r/   r0   r1   rM   r9   r@   rK   rN   r   r   r;   r   r�   {  s   r�   c                   @   s(   e Zd ZdZdZeZdd„ Zdd„ ZdS )ÚAASTexHeaderzÀIn a `deluxetable
    <http://fits.gsfc.nasa.gov/standard30/deluxetable.sty>`_ some header
    keywords differ from standard LaTeX.

    This header is modified to take that into account.
    rƒ   c                 C   s
   t |dƒS )Nrƒ   )r&   r,   r   r   r   rQ   Ÿ  s    zAASTexHeader.start_linec                    s  d| j krt| jƒd | j d< d| j kr<d| j d  d }nd}| d| j d  d	 | j d  d
 | ¡ t| j d|ƒ d| j krš| d| j d  d
 ¡ d dd„ | jD ƒ¡}|  ¡ ‰ d| j krÒˆ  | j d ¡ ˆ rú|d| j	 ‡ fdd„| jD ƒ¡ 7 }| d| d
 ¡ d S )Nr   r\   r
   r]   r^   r_   r`   r   z}{rB   r   r	   z\tablecaption{r„   c                 S   s   g | ]}d | d ‘qS r…   r   rd   r   r   r   r+   ´  s     z&AASTexHeader.write.<locals>.<listcomp>r   z\\ c                    s   g | ]}ˆ   |d ¡‘qS ra   rb   rd   re   r   r   r+   º  s     z\tablehead{)
r#   rf   rV   r   r   rK   rX   r[   rh   rg   )r-   r"   ri   Z	tableheadr   re   r   rj   ¢  s<    

ÿþýüûÿ


ÿzAASTexHeader.writeN)	r/   r0   r1   rM   r   r�   rk   rQ   rj   r   r   r   r   r†   •  s
   r†   c                   @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )	Ú
AASTexDatazGIn a `deluxetable`_ the data is enclosed in `\startdata` and `\enddata`z
\startdataz\enddatac                 C   s   t || jƒd S rP   )r&   r   r,   r   r   r   rQ   Ä  s    zAASTexData.start_linec                 C   s€   |  | j¡ t|ƒ}tj | |¡ t|ƒ|krJtjdd|d tjd�|d< |  | j	¡ t
| jd|ƒ |  d| jd  d ¡ d S )	Nz\s* \\ \\ \s* $r_   r4   )Úflagsr   rp   r   rB   )r   r   rf   r   rq   rj   r   ÚsubÚVERBOSEr   r   r#   )r-   r"   Zlines_length_initialr   r   r   rj   Ç  s    zAASTexData.writeN)r/   r0   r1   rM   r   r   rQ   rj   r   r   r   r   r‡   ¿  s
   r‡   c                       s:   e Zd ZdZdZdgZdZdZeZ	e
Z‡ fdd„Z‡  ZS )ÚAASTexa…  AASTeX format table.

    This class implements some AASTeX specific commands.
    AASTeX is used for the AAS (American Astronomical Society)
    publications like ApJ, ApJL and AJ.

    It derives from the ``Latex`` reader and accepts the same
    keywords.  However, the keywords ``header_start``, ``header_end``,
    ``data_start`` and ``data_end`` in ``latexdict`` have no effect.
    Zaastexr_   z(AASTeX deluxetable used for AAS journalsc                    s0   t ƒ jf |Ž d|kr"d|d ks,d| jd< d S )Nry   r   Zdeluxetable)r8   ru   r#   )r-   Úkwargsr;   r   r   ru   è  s    zAASTex.__init__)r/   r0   r1   rM   r{   r|   r}   r~   r†   r   r‡   r€   ru   rN   r   r   r;   r   r‹   Ô  s   r‹   )rM   r   r_   r   Z
latexdictsr   r5   r   r&   ZBaseInputterr'   ZBaseSplitterr2   Z
BaseHeaderrO   rq   rl   rz   rr   r�   r†   r‡   r‹   r   r   r   r   Ú<module>   sL   
üüõó
&1  %*