o
    i                     @   s  d dl Z d dlZd dlmZm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Zd dlmZ d dlmZmZmZ dd	lmZ ejjZd
d Zejdddd Zdd Zejdddd Zejdddd Z ejdd Z!ejdd Z"dS )    N)new_notebooknew_code_cell)Popen)mkstemp)TemporaryDirectory)urljoin)FirefoxRemoteChrome   )Notebookc              	   C   s   t dD ]?}|  durtdtj|r>zt|}t|W  d   W   S 1 s.w   Y  W n	 t	y=   Y nw t
d qtd|)z0Wait 30 seconds for the notebook server to starti,  NzNotebook server failed to startg?zDidn't find %s in 30 seconds)rangepollRuntimeErrorospathexistsopenjsonload
ValueErrortimesleep)procinfo_file_pathif r   [/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/tests/selenium/conftest.py_wait_for_server   s   
*
r   session)scopec                  c   s0   i } t  v}t|d }| d< tt|dd tt|dd t|dt|dt|d	d
| d< tj }|| d  tjdddd|dg}t	d| t
|||d }| d< t|dd|jdd}| t|| t	d|  | V  W d    n1 sw   Y  tjt| d ddd| d  id d S )N	notebooksnbdiru
   sub ∂ir1u   sub ∂ir 1au
   sub ∂ir2u   sub ∂ir 1bjupyter_configjupyter_runtimeipython)JUPYTER_CONFIG_DIRJUPYTER_RUNTIME_DIR
IPYTHONDIR	extra_envz-mnotebookz--no-browserz--notebook-dirz--NotebookApp.base_url=/a@b/zcommand=)cwdenvpopenz	nbserver-dz.jsonzNotebook server info:urlzapi/shutdownAuthorizationztoken token)headers)r   pjoinr   makedirsenvironcopyupdatesys
executableprintr   pidr   requestspostr   )infotdr#   r-   commandr   r   r   r   r   notebook_server%   s:   




rB   c                  C   s   t jd } t jd }t jd t jd t jd dgdt jd d	d
}|d dkr+d|d< |  d| d}td t|d| dd}|S )zThis function helps travis create a driver on Sauce Labs.

    This function will err if used without specifying the variables expected
    in that context.
    SAUCE_USERNAMESAUCE_ACCESS_KEYTRAVIS_JOB_NUMBERTRAVIS_BUILD_NUMBERTRAVIS_PYTHON_VERSIONCIz
Windows 10JUPYTER_TEST_BROWSERlatest)ztunnel-identifierbuildtagsplatformbrowserNameversionrN   firefoxz57.0rO   :z@localhost:4445z&Connecting remote driver on Sauce Labszhttp://z/wd/hub)desired_capabilitiescommand_executor)r   r6   r;   r	   )username
access_keycapabilitieshub_urldriverr   r   r   make_sauce_driverJ   s"   


rY   c                  c   sF    t jdrt } nt jddkrt } nt } | V  |   d S )NrC   rI   chrome)r   r6   getrY   r
   r   quit)rX   r   r   r   selenium_drivere   s   r]   modulec                 C   s    || _ | djdi | | S )Nz{url}?token={token}r   )jupyter_server_infor[   format)r]   rB   r   r   r   authenticated_browsert   s   ra   c                 c   s$    | j }t| V  | j| d S )N)current_window_handler   r   	switch_towindow)ra   tree_whr   r   r   r+   z   s   r+   c                    s    fdd}|S )Nc                    s   dd | D } t | d}t d dd\}}t|ddd	}t|| W d    n1 s.w   Y  tj|}d
j	|fi   t
S )Nc                 S   s"   g | ]}t |trt|n|qS r   )
isinstancestrr   ).0cr   r   r   
<listcomp>   s    z3prefill_notebook.<locals>.inner.<locals>.<listcomp>)cellsr#   z.ipynb)dirsuffixwzutf-8)encodingz{url}notebooks/{}?token={token})r   r   r   nbformatwriter   r   basenamer[   r`   r   )rk   nbfdr   r   fnamerB   r]   r   r   inner   s   
zprefill_notebook.<locals>.innerr   )r]   rB   rw   r   rv   r   prefill_notebook   s   rx   )#r   rp   nbformat.v4r   r   r   pytestr=   
subprocessr   r9   tempfiler   testpath.tempdirr   r   urllib.parser   selenium.webdriverr   r	   r
   utilsr   r   joinr4   r   fixturerB   rY   r]   ra   r+   rx   r   r   r   r   <module>   s6    

$




