o
    QDi                  (   @  sp  d dl mZ d dlZd dlmZmZm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 dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ erXddlmZ dZdZejddejejejejej ej!ej"ej#ej$ej%ej&ej'ej(ej)ej*ej+ej,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ejej<ej=ej>ej?ejej@f'dAd?d@ZAdS )B    )annotationsN)TYPE_CHECKINGListOptional)Console)Table   )FrictionlessException)stringify_csv_string)platform)Resource)system   )commonhelpers)console)types
   extract)namesource	List[str]r   strtypepathschemeformatcompression	innerpathencodingschemabasepathdialectheader_rowsheader_joincomment_charcomment_rowssheettablekeyskeyedboolbuffer_sizeintsample_size
field_typefield_namesfield_confidencefloatfield_float_numbersfield_missing_valuesschema_syncvalidinvalid
limit_rowsyamljsoncsvdebugtrusted	standardsresource_namekeep_delimiterc'           =      C  s  t  }'|p|%}|#r|#t_|$r|$t_tj| |d} | s+|s+d}(tj|'|(d tjddztj	|||||||||d	})tj
||||||||d}*d}+|rPd	d
 }+n|rVdd
 }+d},|s\| radd
 },n|!rgdd
 },|du rtt|| |!gstt}tt| ||||||||	|
|*d}-|)r|)|-_t|-tjjsd|-j d}(t|(|-j||+|,|d}.|- }/W n ty }0 ztj|'|"|0d tjddd}0~0ww |rtjj|.dd }1t|1 t | rtj|.ddd}1t|1 t |.sd}(tj|'|(d tjdd|!rmt|.dkrd}(tj |(dtj!j"dd tdi }2t|.# d }3|3rit|3d $ }4|rB|&rB|-j%d& }2|2'dd t(|3D ]"\}5}6|5dkrZt t)|4fi |2 t t)|6# fi |2 qFt |'*d t+dd }7|7,d! |7,d |7,d" |/D ]}-|-j-rd#nd$}8|-j.|-j/|-j0g}9|7j1|9d%|8i q|'|7 |'*d& |.2 D ]\}:}3|3stj|'d'd(d) qt+|:d }7t|3d $ }4|4dt3 D ]};|7,|; qt|4t3kr|7,d* |3D ]#}6tt4t5|6# }<|<dt3 }9t|<t3kr
|96d* |7j1|9  qt|3|  krtkr<n nd*gt7t|4t3 }9t|4t3kr7|96d* |7j1|9  |'|7 qdS )+z
    Extract rows from a data source.

    Based on the inferred data source type it will return resource or package data.
    Default output format is tabulated with a front matter. Output will be utf-8 encoded.
    )r   z(Providing "source" or "path" is required)note   )code)	
descriptorr#   r$   r%   r&   r'   r(   r)   r*   )r,   r.   r/   r0   r1   r3   r4   r5   Nc                 S  s   | j S Nr6   row rI   ^/var/www/Datamplify/venv/lib/python3.10/site-packages/frictionless/console/commands/extract.py<lambda>}   s    z!console_extract.<locals>.<lambda>c                 S  s   | j  S rE   rF   rG   rI   rI   rJ   rK      s    c                 S     | j ddS )NT)r:   to_dictrG   rI   rI   rJ   rK          c                 S  rL   )NT)r;   rM   rG   rI   rI   rJ   rK      rO   )r   r   r   r   datatyper   r   r   r    r!   detectorzResource with data type "z" is not extractable)r   filterprocessr8   )r<   	exceptionT)allow_unicoder   F)indentensure_asciiz-No tabular data have been found in the sourcez7For the "csv" mode you need to provide a resource "name)errfgboldr   r;   r   z[bold]Datasetdataset)titler   r   	sky_blue1 stylez[bold]TableszNo rows foundEmpty)rA   r\   z...)8r   r   r=   r>   r   create_sourceprint_errortyperExitcreate_dialectcreate_detectoranyDEFAULT_MAX_ROWSr   r"   
isinstancer   frictionless_resourcesExtractablerP   r	   r   list	Exceptionprint_exceptionr9   	safe_dumpstripprintpyjsondumpslensechocolorsREDvaluesr)   get_controlto_descriptorpop	enumerater
   ruler   
add_columntabularr   r   r   add_rowitemsDEFAULT_MAX_FIELDSmapr   appendmin)=r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r,   r.   r/   r0   r1   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   r   rA   dialect_objdetector_objrR   rS   resourcedata	resourcesrT   contentoptionsr   labelsindexitemviewr_   rH   r\   labelrx   rI   rI   rJ   console_extract   s  6













 

r   )Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r!   r   r"   r   r#   r   r$   r   r%   r   r&   r   r'   r   r(   r   r)   r   r*   r+   r,   r-   r.   r-   r/   r   r0   r   r1   r2   r3   r+   r4   r   r5   r+   r6   r+   r7   r+   r8   r-   r9   r+   r:   r+   r;   r+   r<   r+   r=   r+   r>   r   r?   r   r@   r+   )B
__future__r   r:   rr   typingr   r   r   rc   rich.consoler   
rich.tabler   rT   r	   r   r
   r   r   r   r   r^   r   r   r   r   rh   commandr   r?   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   	skip_rowsr'   r(   r)   r*   r,   r.   r/   r0   r1   r3   r4   r5   
valid_rowsinvalid_rowsr8   r9   r;   r<   r=   r>   r@   r   rI   rI   rI   rJ   <module>   st    
