a
    Df                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlm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 G dd dejZG dd dejZG dd deZdd Zdd Zdd ZeedZedD ]Ze eej< qdS )    N)run_path)	MagicMock)parse_qs)web)WSGIContainer   )entry_points_for   )statec                   @   s   e Zd ZdZdS )	HTTPErrorz
    Custom HTTPError type
    N)__name__
__module____qualname____doc__ r   r   Z/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/io/rest.pyr      s   r   c                   @   s   e Zd Zdd ZdS )BaseHandlerc              	   K   s   |  dd | jdrbd|v rbg }tj|d  D ]}|| q2| td|| j	|di n| td|| j	di d S )NContent-Typeapplication/jsonZserve_tracebackexc_infoerror)codemessage	traceback)r   r   )

set_headersettingsgetr   format_exceptionappendfinishjsondumpsZ_reason)selfstatus_codekwargslinesliner   r   r   write_error   s$    
zBaseHandler.write_errorN)r   r   r   r'   r   r   r   r   r      s   r   c                       s<   e Zd Z fddZedd Zedd Zdd Z  ZS )	ParamHandlerc                    s(   | dd | _t j||fi | d S )Nroot)popr)   super__init__)r"   apprequestr$   	__class__r   r   r,   3   s    zParamHandler.__init__c                    s    fdd|D } j |S )Nc                    s   i | ]}|t  |qS r   )getattr).0pparameterizedr   r   
<dictcomp>9       z*ParamHandler.serialize.<locals>.<dictcomp>)paramZserialize_parameters)clsr5   
parametersvaluesr   r4   r   	serialize7   s    zParamHandler.serializec                    sB   |D ]&}| j vrd| d}t|ddq fdd| D S )N'z!' query parameter not recognized.i  )reasonr#   c                    s    i | ]\}}| j ||qS r   )r8   Zdeserialize_value)r2   r3   vr4   r   r   r6   B   s   z,ParamHandler.deserialize.<locals>.<dictcomp>)r8   r   items)r9   r5   r:   r3   r>   r   r4   r   deserialize<   s    

zParamHandler.deserializec                    s   | j j}||| jt| j d  }tj|d\}}}|sBd S t| j j	}| 
|d |}|d jjf i | | dd | | |d | d S )N)NNNr   r   r   )r.   pathindexr)   lenr
   Z_rest_endpointsr   r   queryrA   r8   updater   writer<   )r"   rB   endpointr5   r:   _argsparamsr   r   r   r   E   s    
zParamHandler.get)	r   r   r   r,   classmethodr<   rA   r   __classcell__r   r   r/   r   r(   1   s   

r(   c                 C   s   ddl m}m} ddlm}m} g }| D ]}|dd }|dkrL||}n\|dkrzdd l}	W n. ty }
 ztd|
W Y d }
~
n
d }
~
0 0 ||}n|d		||
|j q(||d
ddS )Nr   )NotebookHandlerScriptHandler)UnsupportedFileTypemake_app.pyipynb4Please install nbconvert to serve Jupyter Notebooks.-{} is not a script (.py) or notebook (.ipynb)zPanel REST APIzrest/)prefix)Ztranquilizer.handlerrN   rO   Ztranquilizer.mainrP   rQ   split	nbconvertImportErrorformatextendZtranquilized_functions)filesrN   rO   rP   rQ   Z	functionsfilename	extensionsourcerZ   er   r   r   build_tranquilize_applicationT   s     
 
rc   c                 C   s(   t | }t|}d| tjt|dfgS )a  
    Returns a Tranquilizer based REST API. Builds the API by evaluating
    the scripts and notebooks being served and finding all tranquilized
    functions inside them.

    Arguments
    ---------
    files: list(str)
      A list of paths being served
    endpoint: str
      The endpoint to serve the REST API on

    Returns
    -------
    A Tornado routing pattern containing the route and handler
    z^/%s/.*)fallback)rc   r   r   ZFallbackHandlerdict)r^   rH   r-   trr   r   r   tranquilizer_rest_providerj   s    rg   c              
   C   s|  | D ]:}| dd }|dkrPzt| W n  tyL   tjjd Y n0 q|dkr2zddl}W n ty   tddY n0 dd	lm} | }|	|\}}t
j|}	tjd
|	dd^}
|
| |
  zt|
jdt id W n" ty   tjjd Y n0 W d   n1 s&0    Y  qtd|q|r\|ds\|d7 }|rjd| ndtt|dfgS )a]  
    Returns a Param based REST API given the scripts or notebooks
    containing the tranquilized functions.

    Arguments
    ---------
    files: list(str)
      A list of paths being served
    endpoint: str
      The endpoint to serve the REST API on

    Returns
    -------
    A Tornado routing pattern containing the route and handler
    rR   rS   rT   z0Could not run app script on REST server startup.rU   r   NrV   )ScriptExporterwT)modedirdeleteZget_ipython)Zinit_globalsz2Could not run app notebook on REST server startup.rW   /z^/%s.*z^.*)r)   )rY   r   	Exceptionr8   mainwarningrZ   r[   rh   from_filenameosrB   dirnametempfileNamedTemporaryFilerG   flushnamer   
ValueErrorr\   endswithr(   re   )r^   rH   r_   r`   rZ   rh   Zexporterra   rI   
source_dirtmpr   r   r   param_rest_provider   s6    


6r|   )Ztranquilizerr8   zpanel.io.rest)r    rr   rt   r   Zrunpyr   Zunittest.mockr   urllib.parser   r8   Ztornador   Ztornado.wsgir   Zentry_pointsr   r
   r   ZRequestHandlerr   r(   rc   rg   r|   ZREST_PROVIDERSentry_pointloadrw   r   r   r   r   <module>   s,   #0