U
    e»{jÕF  ã                   @   sÒ   d 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	 G dd	„ d	e
ƒZG d
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	jƒZdS )zÇAn extensible HTML table reader and writer.

html.py:
  Classes to read and write HTML tables

`BeautifulSoup <http://www.crummy.com/software/BeautifulSoup/>`_
must be installed to read HTML tables.
é    N)Údeepcopy)ÚColumn)Úwriteré   )Úcorec                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
SoupStringz8
    Allows for strings to hold BeautifulSoup data.
    c                 O   s   t j| f|ž|ŽS ©N)ÚstrÚ__new__)ÚclsÚargsÚkwargs© r   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/io/ascii/html.pyr
      s    zSoupString.__new__c                 C   s
   || _ d S r   )Úsoup)ÚselfÚvalr   r   r   Ú__init__   s    zSoupString.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r   r   r      s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
ListWriterzD
    Allows for XMLWriter to write to a list instead of a file.
    c                 C   s
   || _ d S r   )Úout)r   r   r   r   r   r   %   s    zListWriter.__init__c                 C   s   | j  |¡ d S r   )r   Úappend)r   Údatar   r   r   Úwrite(   s    zListWriter.writeN)r   r   r   r   r   r   r   r   r   r   r       s   r   c                 C   sd   | dks| j dkrdS d|kr&|dkS |d }t|tƒrNd| jkoL| d |kS t|tƒr`||kS dS )zb
    Checks whether the given BeautifulSoup tag is the table
    the user intends to process.
    NÚtableFÚtable_idr   Úid)ÚnameÚ
isinstancer	   ÚattrsÚint)r   ÚhtmldictZnumtabler   r   r   r   Úidentify_table,   s    

r%   c                   @   s   e Zd ZdZdd„ ZdS )ÚHTMLInputterz—
    Input lines of HTML in a valid form.

    This requires `BeautifulSoup
    <http://www.crummy.com/software/BeautifulSoup/>`_ to be installed.
    c           
   	   C   s  zddl m} W n tk
r.   t d¡‚Y nX d| jkrjt ¡ �  t dd¡ |d 	|¡ƒ}W 5 Q R X n|d 	|¡| jd ƒ}| 
d¡}t|ƒD ]"\}}t|| j|d	 ƒr’|} qüq’t| jd
 tƒrØd| jd
 › �}nd| jd
 › d�}t d|› d�¡‚dd„ | 
d¡D ƒ}	|	S )zh
        Convert the given input into a list of SoupString rows
        for further processing.
        r   )ÚBeautifulSoupz3BeautifulSoup must be installed to read HTML tablesÚparserÚignorez&.*no parser was explicitly specified.*Ú
r   r   r   znumber zid 'ú'zERROR: HTML table z
 not foundc                 S   s   g | ]}t |ƒ‘qS r   )r   ©Ú.0Úxr   r   r   Ú
<listcomp>o   s     z.HTMLInputter.process_lines.<locals>.<listcomp>Útr)Zbs4r'   ÚImportErrorr   ZOptionalTableImportErrorÚhtmlÚwarningsÚcatch_warningsÚfilterwarningsÚjoinÚfind_allÚ	enumerater%   r!   r#   ÚInconsistentTableError)
r   Úlinesr'   r   ZtablesÚiZpossible_tabler   Z	err_descrZ	soup_listr   r   r   Úprocess_linesJ   s6    ÿ


 ÿ

ÿzHTMLInputter.process_linesN)r   r   r   r   r<   r   r   r   r   r&   B   s   r&   c                   @   s   e Zd ZdZdd„ ZdS )ÚHTMLSplitterz 
    Split HTML table data.
    c                 c   sx   |D ]X}t |tƒstdƒ‚|j}| d¡}|r>dd„ |D ƒV  | d¡}|rdd„ |D ƒV  qt|ƒdkrtt d¡‚d	S )
z=
        Return HTML data from lines as a generator.
        ú'HTML lines should be of type SoupStringÚthc                 S   s2   g | ]*}|  d ¡r$|j ¡ |d  fn|j ¡ ‘qS ©Úcolspan)Zhas_attrÚtextÚstrip©r-   Úelr   r   r   r/   „   s   þÿz)HTMLSplitter.__call__.<locals>.<listcomp>Útdc                 S   s   g | ]}|j  ¡ ‘qS r   )rB   rC   rD   r   r   r   r/   Œ   s     r   z.HTML tables must contain data in a <table> tagN)r!   r   Ú	TypeErrorr   r7   Úlenr   r9   )r   r:   Úliner   Zheader_elementsZdata_elementsr   r   r   Ú__call__y   s     

ü
ÿzHTMLSplitter.__call__N)r   r   r   r   rJ   r   r   r   r   r=   t   s   r=   c                       s<   e Zd ZdZe e¡e e¡e e¡gZ	‡ fdd„Z
‡  ZS )ÚHTMLOutputterzÍ
    Output the HTML data as an ``astropy.table.Table`` object.

    This subclass allows for the final table to contain
    multidimensional columns (defined using the colspan attribute
    of <th>).
    c                    s”   g }d}|t |ƒk r†|| }t|dƒrr||||j … }t |j¡}ttdd„ |D ƒŽ ƒ|_| 	|¡ ||j7 }q| 	|¡ |d7 }qt
ƒ  ||¡S )z?
        Process the data in multidimensional columns.
        r   rA   c                 S   s   g | ]
}|j ‘qS r   )Ústr_valsr,   r   r   r   r/   ¯   s     z*HTMLOutputter.__call__.<locals>.<listcomp>r   )rH   ÚhasattrrA   r   r   r    ÚlistÚziprL   r   ÚsuperrJ   )r   ÚcolsÚmetaÚnew_colsZcol_numÚcolZ	span_colsÚnew_col©Ú	__class__r   r   rJ   ¢   s    



zHTMLOutputter.__call__)r   r   r   r   r   Zconvert_numpyr#   Úfloatr	   Zdefault_convertersrJ   Ú__classcell__r   r   rV   r   rK   “   s   	ýrK   c                   @   s    e Zd ZeZdd„ Zdd„ ZdS )Ú
HTMLHeaderc                 C   s@   t |ƒD ]2\}}t|tƒs"tdƒ‚|j}|jdk	r|  S qdS )zE
        Return the line number at which header data begins.
        r>   N)r8   r!   r   rG   r   r?   ©r   r:   r;   rI   r   r   r   r   Ú
start_line¼   s    


zHTMLHeader.start_linec                 C   s¶   g | _ g }| jD ]š}t|tƒrŒtj|d d�}t|d ƒ|_| j  |¡ | |d ¡ t	dt|d ƒƒD ] }| j  t d¡¡ | d¡ qhq| j  tj|d�¡ | |¡ q|| _dS )zU
        Set columns from header names, handling multicolumns appropriately.
        r   )r    r   Ú N)
rQ   Únamesr!   Útupler   r   r#   rA   r   Úrange)r   Z	new_namesr    rT   r;   r   r   r   Ú_set_cols_from_namesÊ   s    

zHTMLHeader._set_cols_from_namesN)r   r   r   r=   Úsplitter_classr\   ra   r   r   r   r   rZ   ¹   s   rZ   c                   @   s    e Zd ZeZdd„ Zdd„ ZdS )ÚHTMLDatac                 C   s^   t |ƒD ]F\}}t|tƒs"tdƒ‚|j}|jdk	r|jdk	rFt d¡‚|  S qt d¡‚dS )zD
        Return the line number at which table data begins.
        r>   Nz9HTML tables cannot have headings and data in the same rowz!No start line found for HTML data)	r8   r!   r   rG   r   rF   r?   r   r9   r[   r   r   r   r\   å   s    


ÿ
zHTMLData.start_linec                 C   sP   d}t |ƒD ].\}}t|tƒs&tdƒ‚|j}|jdk	r|}q|dkrHdS |d S )zB
        Return the line number at which table data ends.
        éÿÿÿÿr>   Nr   )r8   r!   r   rG   r   rF   )r   r:   Z
last_indexr;   rI   r   r   r   r   Úend_lineø   s    

zHTMLData.end_lineN)r   r   r   r=   rb   r\   re   r   r   r   r   rc   â   s   rc   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i f‡ fdd„	Z‡ fdd	„Zd
d„ Zdd„ Z‡  ZS )ÚHTMLaq
  HTML format table.

    In order to customize input and output, a dict of parameters may
    be passed to this class holding specific customizations.

    **htmldict** : Dictionary of parameters for HTML input/output.

        * css : Customized styling
            If present, this parameter will be included in a <style>
            tag and will define stylistic attributes of the output.

        * table_id : ID for the input table
            If a string, this defines the HTML id of the table to be processed.
            If an integer, this specifies the index of the input table in the
            available tables. Unless this parameter is given, the reader will
            use the first table found in the input file.

        * multicol : Use multi-dimensional columns for output
            The writer will output tuples as elements of multi-dimensional
            columns if this parameter is true, and if not then it will
            use the syntax 1.36583e-13 .. 1.36583e-13 for output. If not
            present, this parameter will be true by default.

        * raw_html_cols : column name or list of names with raw HTML content
            This allows one to include raw HTML content in the column output,
            for instance to include link references in a table.  This option
            requires that the bleach package be installed.  Only whitelisted
            tags are allowed through for security reasons (see the
            raw_html_clean_kwargs arg).

        * raw_html_clean_kwargs : dict of keyword args controlling HTML cleaning
            Raw HTML will be cleaned to prevent unsafe HTML from ending up in
            the table output.  This is done by calling ``bleach.clean(data,
            **raw_html_clean_kwargs)``.  For details on the available options
            (e.g. tag whitelist) see:
            https://bleach.readthedocs.io/en/latest/clean.html

        * parser : Specific HTML parsing library to use
            If specified, this specifies which HTML parsing library
            BeautifulSoup should use as a backend. The options to choose
            from are 'html.parser' (the standard library parser), 'lxml'
            (the recommended parser), 'xml' (lxml's XML parser), and
            'html5lib'. html5lib is a highly lenient parser and therefore
            might work correctly for unusual input if a different parser
            fails.

        * jsfiles : list of js files to include when writing table.

        * cssfiles : list of css files to include when writing table.

        * js : js script to include in the body when writing table.

        * table_class : css class for the table

    r2   z.htmlz
HTML tableé   c                    sF   t ƒ  ¡  t|ƒ| _d|kr&d| jd< d|kr8d| jd< | j| j_dS )zB
        Initialize classes for HTML reading and writing.
        ÚmulticolTr   r   N)rP   r   r   r2   Zinputter)r   r$   rV   r   r   r   N  s    



zHTML.__init__c                    s   t ƒ | _tƒ  |¡S )zU
        Read the ``table`` in HTML format and return a resulting ``Table``.
        )rK   Z	outputterrP   Úread)r   r   rV   r   r   ri   Z  s    z	HTML.readc                    sz  |   |¡ t|j ¡ ƒ}|| jj_|| j_t| jjt	ƒrF| jjg| j_| j 
|¡ | j ¡  g }| j dg ¡‰tˆtƒr~ˆg‰‡fdd„|D ƒ}| j di ¡}t t|ƒ¡}| d¡��° | d¡�ô |jddd	id
�� W 5 Q R X |jddddœd
�� W 5 Q R X d| jk�r6| d¡� | | jd ¡ W 5 Q R X d| jk�rr| jd D ]$}|jdd|dd�� W 5 Q R X �qLd| jk�r´| jd D ]*}|jd|d�� | d¡ W 5 Q R X �qˆW 5 Q R X | d¡��” d| jk�r| d¡�, | d¡� | | jd ¡ W 5 Q R X W 5 Q R X t| jd tƒ�r2| jd }nd}d| jk�rV| jd }	d|	i}
ni }
|jd ||
d!���è | d"¡��H | d#¡�l |D ]`}t|jƒd$k�rÂ| jd% �rÂ|jd&|jd$ d'� n
| d&¡ | |jj ¡ ¡ |jd(d)� �qŒW 5 Q R X g }g }g }t||ƒD ]®\}}t|jƒd$k�r”| jd% �r”|jd$ }t|ƒD ]L‰ t‡ fd*d„|D ƒƒ}|  ||j ¡ ¡}| |¡ | |¡ | |¡ �qDn&|  ||j ¡ ¡}| |¡ | |¡ �qW 5 Q R X t|Ž D ]~}| d#¡�h t||ƒD ]V\}}|�rüd+nd,}|j|f|Ž�* | d-¡ | | ¡ ¡ |jd(d)� W 5 Q R X �qêW 5 Q R X �qÐW 5 Q R X W 5 Q R X W 5 Q R X d  |¡gS ).zR
        Return data in ``table`` converted to HTML as a list of strings.
        Úraw_html_colsc                    s   g | ]}|j jˆ k‘qS r   )Úinfor    )r-   rT   )rj   r   r   r/   z  s     zHTML.write.<locals>.<listcomp>Úraw_html_clean_kwargsr2   ÚheadrR   Úcharsetzutf-8)ÚattribzContent-typeztext/html;charset=UTF-8)z
http-equivÚcontentÚcssÚstyleZcssfilesÚlinkÚ
stylesheetztext/css)ÚrelÚhrefÚtypeZjsfilesÚscript)Úsrcr]   ÚbodyÚjsÚnoner   NZtable_classÚclassr   )r   ro   Ztheadr0   r   rh   r?   r@   F)Úindentc                    s   g | ]}|ˆ  ‘qS r   r   rD   )r;   r   r   r/   Å  s     Z
escape_xmlZbleach_cleanrF   )!Z_check_multidim_tablerN   ÚcolumnsÚvaluesr   ÚheaderrQ   r!   Úfill_valuesr_   Z_set_fill_valuesZ_set_col_formatsr2   Úgetr	   r   Z	XMLWriterr   ÚtagZxml_cleaning_methodrH   ÚshapeÚstartrk   r    rC   ÚendrO   r`   r   Ziter_str_valsr   r6   )r   r   rQ   r:   Zcols_escapedrl   ÚwÚfilenameZhtml_table_idZhtml_table_classro   rT   Úcol_str_itersZnew_cols_escapedrS   Zcol_escapedÚspanrU   Znew_col_iter_str_valsZcol_iter_str_valsÚrowrE   Úmethodr   )r;   rj   r   r   b  sÂ    




þþ
   ÿ"$



 ÿ

 ÿ
ÿÿ
Fz
HTML.writec                 c   sn   t |dƒ}t |dƒ}t|ƒD ]L\}}|rF|rF|j| rF|jtj V  q|rb||jkrb|j| V  q|V  qdS )zY
        Return an iterator of the values with replacements based on fill_values
        Úmaskr‚   N)rM   r8   rŽ   r‚   r   Zmasked)r   rT   rŠ   Zis_masked_columnZhas_fill_valuesÚidxZcol_strr   r   r   r‚   ä  s    



zHTML.fill_values)r   r   r   r   Z_format_nameZ_io_registry_format_aliasesZ_io_registry_suffixZ_descriptionrZ   Zheader_classrc   Z
data_classr&   Zinputter_classZmax_ndimr   ri   r   r‚   rY   r   r   rV   r   rf   
  s   8 rf   )r   r3   Úcopyr   Zastropy.tabler   Zastropy.utils.xmlr   r]   r   r	   r   r   r%   ZBaseInputterr&   ZBaseSplitterr=   ZTableOutputterrK   Z
BaseHeaderrZ   ZBaseDatarc   Z
BaseReaderrf   r   r   r   r   Ú<module>   s   	2&)(