o
    QDi                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dl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mZ d	dlmZ ejddejejejejejejejfd!ddZd"ddZd#dd ZdS )$    )annotationsN)List)Console   )
write_file)Resource)system   )commonhelpers)consolescriptnamesource	List[str]r   strtypepathdebugbooltrusted	standardsc                 C  sz  t  }|r|t_|r|t_tj| |d} | s'|s'd}tj||d tjdd|	d z:t
t| |||d}	tjdd	d
}
ttj|
j |
j}g }|	j|d}|D ]}	|tj||	|dd|d qTW n ty } ztj|||d tjddd}~ww |sd}tj||d td|	d tjddd
}
ttj|
j t||d}t|
j| td|
j d dS )zScript data)r   z(Providing "source" or "path" is required)note   )codez[bold]Index)r   r   r   datatypeFz.db)deletesuffixr   T)resourcedatabasefastuse_fallbackr   )r   	exceptionNzNot found any tabular resourcesz[bold]Scriptz.py)nameszPYTHONSTARTUP=z python3)r   r   r   r   r   create_sourceprint_errortyperExitruler   tempfileNamedTemporaryFileatexitregisterosremover   listextendindex_resource	Exceptionprint_exceptiongenerate_startupr   )r   r   r   r   r   r   r   r   r   r   filer    r$   	resourcesr#   startup r9   ]/var/www/Datamplify/venv/lib/python3.10/site-packages/frictionless/console/commands/script.pyconsole_script   sd   


r;   r    r$   returnc                C  s   d| vr	d|  } g }| d | d | d|  d | d g }|D ]}t|}| | | | d| d	 q'| d
d| d d|S )Nz://z
sqlite:///zimport sqlalchemy as sazimport pandas as pdzengine = sa.create_engine('z')zconn = engine.connect()z = pd.read_sql('z', conn)zprint('Available data frames: z, 
)appendgenerate_dfnamejoin)r    r$   linesdfnamesr   dfnamer9   r9   r:   r5   c   s   





r5   c                 C  s   dt dd|   S )Ndf_z[^0-9a-zA-Z_]+_)resublowerr   r9   r9   r:   r?   t   s   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   )
__future__r   r,   r.   rF   r*   typingr   r'   rich.consoler   r   r   r   r   r    r
   r   commandr   resource_namer   r   r   r   r   r;   r5   r?   r9   r9   r9   r:   <module>   s0    

O