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 d dl	m
Z
mZ d dlmZ d dlmZmZ 		dd	d
ZG dd de
Z		dddZee
_		dddZee
_dd Z		dddZee
_G dd de
Zdd ZdS )    )absolute_importprint_functiondivisionN)nextPY2	text_type)Tableasdict)getcodec)read_source_from_argwrite_source_from_argstrictlinesc                 C   s   t | } t| ||||dS )a  
    Extract a table from lines in the given text file. E.g.::

        >>> import petl as etl
        >>> # setup example file
        ... text = 'a,1\nb,2\nc,2\n'
        >>> with open('example.txt', 'w') as f:
        ...     f.write(text)
        ...
        12
        >>> table1 = etl.fromtext('example.txt')
        >>> table1
        +-------+
        | lines |
        +=======+
        | 'a,1' |
        +-------+
        | 'b,2' |
        +-------+
        | 'c,2' |
        +-------+

        >>> # post-process, e.g., with capture()
        ... table2 = table1.capture('lines', '(.*),(.*)$', ['foo', 'bar'])
        >>> table2
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' | '1' |
        +-----+-----+
        | 'b' | '2' |
        +-----+-----+
        | 'c' | '2' |
        +-----+-----+

    Note that the strip() function is called on each line, which by default
    will remove leading and trailing whitespace, including the end-of-line
    character - use the `strip` keyword argument to specify alternative
    characters to strip. Set the `strip` argument to `False` to disable this
    behaviour and leave line endings in place.

    )headerencodingerrorsstrip)r   TextView)sourcer   r   r   r    r   E/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/text.pyfromtext   s   -r   c                   @   s"   e Zd Z		dddZdd ZdS )	r   r   Nr   c                 C   s"   || _ || _|| _|| _|| _d S N)r   r   r   r   r   )selfr   r   r   r   r   r   r   r   __init__D   s
   
zTextView.__init__c              	   c   s    | j d\}trt| j}|j|| jd}ntj|| j| jdd}z/| j	d ur/t
| j	V  | jdu r>|D ]}|fV  q6n|D ]
}|| jfV  q@W tsR|  ntsZ|  w w W d    d S 1 sfw   Y  d S )Nrbr    r   r   newlineF)r   openr   r
   r   streamreaderr   ioTextIOWrapperr   tupler   detach)r   bufcodecfliner   r   r   __iter__L   s8   




"zTextView.__iter__)r   Nr   N__name__
__module____qualname__r   r+   r   r   r   r   r   B   s
    
r   c              
   C      t | |d|||||d dS )a  
    Write the table to a text file. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['c', 2]]
        >>> prologue = '''{| class="wikitable"
        ... |-
        ... ! foo
        ... ! bar
        ... '''
        >>> template = '''|-
        ... | {foo}
        ... | {bar}
        ... '''
        >>> epilogue = '|}'
        >>> etl.totext(table1, 'example.txt', template=template,
        ... prologue=prologue, epilogue=epilogue)
        >>> # see what we did
        ... print(open('example.txt').read())
        {| class="wikitable"
        |-
        ! foo
        ! bar
        |-
        | a
        | 1
        |-
        | b
        | 2
        |-
        | c
        | 2
        |}

    The `template` will be used to format each row via
    `str.format <http://docs.python.org/library/stdtypes.html#str.format>`_.

    wbr   moder   r   templateprologueepilogueN
_writetexttabler   r   r   r4   r5   r6   r   r   r   totexth   s   
,
r;   c              
   C   r0   )z+
    Append the table to a text file.

    abr2   Nr7   r9   r   r   r   
appendtext   s   

r=   c              	   C   s.  |d usJ dt ||}||{}tr!t|}	|	j||d}
n	tj|||dd}
zR|d ur4|
| t| }zt	|}W n t
yI   g }Y nw ttt|}|D ]}t||}|jdi |}|
| qS|d urq|
| |
  W ts||
  nts|
  w w W d    d S 1 sw   Y  d S )Ntemplate is requiredr   r   r   r   )r   r!   r   r
   streamwriterr#   r$   writeiterr   StopIterationlistmapr   r	   formatflushr&   )r:   r   r3   r   r   r4   r5   r6   r'   r(   r)   ithdrfldsrowrecsr   r   r   r8      sH   





"r8   c              	   C   s&   |dusJ dt | ||||||dS )zT
    Return a table that writes rows to a text file as they are iterated over.

    Nr>   )r   r   r   r4   r5   r6   )TeeTextViewr9   r   r   r   teetext   s   
rN   c                   @   s"   e Zd Z		dddZdd ZdS )rM   Nr   c                 C   s.   || _ || _|| _|| _|| _|| _|| _d S r   r9   )r   r:   r   r   r   r4   r5   r6   r   r   r   r      s   
zTeeTextView.__init__c                 C   s"   t | j| j| j| j| j| j| jS r   )_iterteetextr:   r   r   r   r4   r5   r6   )r   r   r   r   r+      s   zTeeTextView.__iter__NNr   NNNr,   r   r   r   r   rM      s
    

rM   c              	   c   sT   |d us	J dt |}|d}tr!t|}|j||d}	ntj|||d}	zf|d ur3|	| t| }
zt	|
}W n t
yT   Y W tsL|	  W d    d S w t|V  ttt|}|
D ]}t||}|jdi |}|	| |V  qc|d ur|	| |	  W ts|	  nts|	  w w W d    d S 1 sw   Y  d S )Nr>   r1   r   )r   r   r   )r   r!   r   r
   r?   r#   r$   r@   rA   r   rB   r&   r%   rC   rD   r   r	   rE   rF   )r:   r   r   r   r4   r5   r6   r'   r(   r)   rG   rH   rI   rJ   rK   rL   r   r   r   rO      sR   






"rO   )NNr   Nr   rP   )
__future__r   r   r   r#   petl.compatr   r   r   petl.util.baser   r	   petl.io.baser
   petl.io.sourcesr   r   r   r   r;   r=   r8   rN   rM   rO   r   r   r   r   <module>   s2   
2&
1
,
