o
    ;Di!                  
   @   s   d dl mZmZmZ d dlZd dlmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZ d dlmZ dddded	d
dddf
ddZee_dddded	d
dddf
ddZee_G dd deZdd Zdd Zdd Zdd Zdd ZdS )    )absolute_importprint_functiondivisionN)	text_typenumeric_typesnextPY2izip_longeststring_typescallable)ArgumentError)TableRecord)getcodec)write_source_from_argstrict
Fc                    s  t |}|dx}trt|}|j||d}n	tj|||dd}zOt| }zt| W n t	y7   g  Y nw t
| ||||
 |rPt|rP fdd|D }|D ]}t| |||||	|
 qRt|| |  W tsp|  ntsx|  w w W d   dS 1 sw   Y  dS )a  
    Write the table as HTML to a file. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['c', 2]]
        >>> etl.tohtml(table1, 'example.html', caption='example table')
        >>> print(open('example.html').read())
        <table class='petl'>
        <caption>example table</caption>
        <thead>
        <tr>
        <th>foo</th>
        <th>bar</th>
        </tr>
        </thead>
        <tbody>
        <tr>
        <td>a</td>
        <td style='text-align: right'>1</td>
        </tr>
        <tr>
        <td>b</td>
        <td style='text-align: right'>2</td>
        </tr>
        <tr>
        <td>c</td>
        <td style='text-align: right'>2</td>
        </tr>
        </tbody>
        </table>

    The `caption` keyword argument is used to provide a table caption
    in the output HTML.

    wberrors encodingr   newlinec                 3       | ]}t | V  qd S Nr   .0rowhdr E/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/html.py	<genexpr>X       ztohtml.<locals>.<genexpr>N)r   openr   r   streamwriterioTextIOWrapperiterr   StopIteration_write_beginr   
_write_row
_write_endflushdetach)tablesourcer   r   captionvreprlineterminatorindex_headertr_style	td_stylestruncatebufcodecfitr   r"   r    r#   tohtml   sH   *


"r>   c                 C   s&   t |}t| |||||||||	|
dS )z`
    Return a table that writes rows to a Unicode HTML file as they are
    iterated over.

    )
r2   r   r   r3   r4   r5   r6   r7   r8   r9   )r   TeeHTMLViewr1   r2   r   r   r3   r4   r5   r6   r7   r8   r9   r"   r"   r#   teehtmli   s   	
rA   c                
   @   s2   e Zd Zddddedddddf
ddZdd ZdS )	r?   Nr   r   Fc                 C   sF   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
d S r   r@   )selfr1   r2   r   r   r3   r4   r5   r6   r7   r8   r9   r"   r"   r#   __init__~   s   
zTeeHTMLView.__init__c                 #   s^   | j }| j}| j}| j}| j}| j}| j}| j}| j}	| j	}
| j
}|d~}tr6t|}|j||d}n	tj|||dd}zUt|}z	t|  V  W n tyX   g  Y nw t| |||| |rqt|rq fdd|D }|D ]}t| |||
||	| |V  qst|| |  W ts|  nts|  w w W d    d S 1 sw   Y  d S )Nr   r   r   r   c                 3   r   r   r   r   r    r"   r#   r$      r%   z'TeeHTMLView.__iter__.<locals>.<genexpr>)r1   r2   r   r   r5   r3   r6   r7   r8   r4   r9   r&   r   r   r'   r(   r)   r*   r   r+   r,   r   r-   r.   r/   r0   )rB   r1   r2   r   r   r5   r3   r6   r7   r8   r4   r9   r:   r;   r<   r=   r   r"   r    r#   __iter__   sb   



"zTeeHTMLView.__iter__)__name__
__module____qualname__r   rC   rD   r"   r"   r"   r#   r?   }   s    
r?   c                 C   s   |  d|  |d ur|  d| |  |rT|  d|  |  d|  t|D ]\}}|r4d||f }|r<|d | }|  d| |  q(|  d|  |  d|  |  d	|  d S )
Nz<table class='petl'>z<caption>%s</caption>z<thead><tr>z%s|%sz<th>%s</th></tr>z</thead>z<tbody>)write	enumerate)r<   fldsr5   r3   r6   r9   ihr"   r"   r#   r,      s   r,   c                 C   s   t ||}|r| d| |  n| d|  t||d dD ]-\}	}
||
}|r/|d | }t|	|
|}|rC| d||f |  q| d| |  q| d|  d S )Nz<tr style='%s'>rH   )	fillvaluez<td style='%s'>%s</td>z<td>%s</td>rI   )_get_tr_cssrJ   r	   _get_td_css)r<   rL   r   r5   r4   r7   r8   r9   tr_cssrN   vrtd_cssr"   r"   r#   r-      s   
r-   c                 C   s2   |rt |tr	|S t|r|| S td| dS )N#expected string or callable, got %rr   )
isinstancer
   r   r   )r   r7   r"   r"   r#   rP      s   
rP   c                 C   s   |r:t |tr	|S t|r||S t |tr4| |v r3||  }t |tr%|S t|r-||S td| ntd| t |trFt |tsFdS dS )NrV   z)expected string, callable or dict, got %rztext-align: rightr   )rW   r
   r   dictr   r   bool)rN   rS   r8   sr"   r"   r#   rQ      s,   



rQ   c                 C   s    |  d|  |  d|  d S )Nz</tbody>z</table>)rJ   )r<   r5   r"   r"   r#   r.     s   r.   )
__future__r   r   r   r(   petl.compatr   r   r   r   r	   r
   r   petl.errorsr   petl.util.baser   r   petl.io.baser   petl.io.sourcesr   r>   rA   r?   r,   r-   rP   rQ   r.   r"   r"   r"   r#   <module>   s.   $
T
I