B
    K
Ç[Ï5  ã               @   sö  d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	 ddl
mZmZmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZmZmZmZmZmZ e eƒZ!de!d< de!d< de!d< e eƒZ"dddiidfe"d< eZ#eƒ Z$e#e$krdee$f Z#G dd„ deƒZ%e eƒZ&de&d< G dd„ deƒZ'G dd„ deƒZ(G dd„ deƒZ)G d d!„ d!eƒZ*G d"d#„ d#eƒZ+e eƒZ,d$e,d< e eƒZ-d%d&diid'fe-d(< d%d)diid*fe-d+< d%d,diid-fe-d,< G d.d%„ d%eƒZ.e.j/ Z0Z1e2d/kròe0ƒ  dS )0z#A tornado based Jupyter lab server.é    N)Ú
JupyterAppÚbase_aliases)ÚslugifyÚWORKSPACE_EXTENSION)ÚNotebookAppÚaliasesÚflags)Úurl_path_join)ÚBoolÚUnicodeé   )Ú__version__)Úload_configÚload_jupyter_server_extension)ÚbuildÚcleanÚget_app_dirÚget_app_versionÚget_user_settings_dirÚget_workspaces_dirzLabBuildApp.app_dirzapp-dirzLabBuildApp.nameÚnamezLabBuildApp.versionÚversionÚLabBuildAppÚ	dev_buildTzBuild in Development modeZdevz%s (dev), %s (app)c               @   sj   e Zd ZeZdZeZeZe	ddddZ
e	ddddZe	ddddZeddd	dZeddd
dZdd„ ZdS )r   zú
    Build the JupyterLab application

    The application is built in the JupyterLab app directory in `/staging`.
    When the build is complete it is put in the JupyterLab app `/static`
    directory, where it is used to serve the application.
    Ú TzThe app directory to build in)ÚconfigÚhelpZ
JupyterLabz!The name of the built applicationz$The version of the built applicationz3Whether to build in dev mode (defaults to dev mode)z3Whether to clean before building (defaults to True)c             C   sr   | j s
dnd}| jptƒ }| j dt¡ | jrH| j d| ¡ t| jƒ | j d|¡ t|| j	| j|| jd d S )Nz
build:prodr   zJupyterLab %szCleaning %szBuilding in %s)Úapp_dirr   r   ÚcommandÚlogger)
r   r   r   ÚlogÚinfor   Ú	pre_cleanr   r   r   )Úselfr   r   © r$   ú0lib/python3.7/site-packages/jupyterlab/labapp.pyÚstartE   s    
zLabBuildApp.startN)Ú__name__Ú
__module__Ú__qualname__r   ÚdescriptionÚbuild_aliasesr   Úbuild_flagsr   r   r   r   r
   r   r"   r&   r$   r$   r$   r%   r   *   s   zLabCleanApp.app_dirc               @   s.   e Zd ZeZdZeZeddddZdd„ Z	dS )	ÚLabCleanAppz‰
    Clean the JupyterLab application

    This will clean the app directory by removing the `staging` and `static`
    directories.
    r   TzThe app directory to clean)r   r   c             C   s   t | j| jd d S )N)r   )r   r   r    )r#   r$   r$   r%   r&   a   s    zLabCleanApp.startN)
r'   r(   r)   r   r*   Úclean_aliasesr   r   r   r&   r$   r$   r$   r%   r-   U   s
   r-   c               @   s   e Zd ZeZdZdd„ ZdS )Ú
LabPathAppaD  
    Print the configured paths for the JupyterLab application

    The application path can be configured using the JUPYTERLAB_DIR
        environment variable.
    The user settings path can be configured using the JUPYTERLAB_SETTINGS_DIR
        environment variable or it will fall back to
        `/lab/user-settings` in the default Jupyter configuration directory.
    The workspaces path can be configured using the JUPYTERLAB_WORKSPACES_DIR
        environment variable or it will fall back to
        '/lab/workspaces' in the default Jupyter configuration directory.
    c             C   s.   t dtƒ  ƒ t dtƒ  ƒ t dtƒ  ƒ d S )NzApplication directory:   %szUser Settings directory: %szWorkspaces directory: %s)Úprintr   r   r   )r#   r$   r$   r%   r&   t   s    zLabPathApp.startN)r'   r(   r)   r   r*   r&   r$   r$   r$   r%   r/   e   s   r/   c               @   s   e Zd ZeZdZdd„ ZdS )ÚLabWorkspaceExportAppa  
    Export a JupyterLab workspace

    If no arguments are passed in, this command will export the default
        workspace.
    If a workspace name is passed in, this command will export that workspace.
    If no workspace is found, this command will export an empty workspace.
    c             C   s  t | jd}|j}t|ƒ}|j}|j}t| jƒdkrFtdƒ t	 
d¡ | jsP|nt|j| jd ƒ}t||ƒ}tj ||t ¡}tj |¡ròt|ƒX}	yt|	 ¡ ƒ W n@ tk
rä }
 z"tt ttƒ t|dd¡ƒ W d d }
~
X Y nX W d Q R X ntt ttƒ t|dd¡ƒ d S )N)r   r   z6Too many arguments were provided for workspace export.r   )Úid)ÚdataÚmetadata)ÚLabAppr   Úbase_urlr   Úworkspaces_dirÚpage_urlÚlenÚ
extra_argsr0   ÚsysÚexitÚujoinÚworkspaces_urlr   ÚosÚpathÚjoinr   ÚexistsÚopenÚreadÚ	ExceptionÚjsonÚdumpsÚdict)r#   Úappr6   r   Ú	directoryr8   ÚrawÚslugÚworkspace_pathÚfidÚer$   r$   r%   r&   …   s&    



<zLabWorkspaceExportApp.startN)r'   r(   r)   r   r*   r&   r$   r$   r$   r%   r1   z   s   r1   c               @   s$   e Zd ZeZdZdd„ Zdd„ ZdS )ÚLabWorkspaceImportAppz»
    Import a JupyterLab workspace

    This command will import a workspace from a JSON file. The format of the
        file must be the same as what the export functionality emits.
    c             C   s¢  t | jd}|j}t|ƒ}|j}|j}|j}t| jƒdkrLt	dƒ t
 d¡ | jd }tj |¡}tj |¡s„t	d| ƒ t
 d¡ tƒ }	t|ƒX}
y|  |
|||¡}	W n< tk
rä } zt	d||f ƒ t
 d¡ W d d }~X Y nX W d Q R X tj |¡sHyt |¡ W n: tk
rF } zt	d| ƒ t
 d¡ W d d }~X Y nX t|	d d	 |ƒ}tj ||t ¡}t|d
ƒ}
|
 t |	¡¡ W d Q R X t	d| ƒ d S )N)r   r   z.One argument is required for workspace import.r   z%s does not exist.z%s is not a valid workspace:
%sz-Workspaces directory could not be created:
%sr4   r2   ÚwzSaved workspace: %s)r5   r   r6   r   r7   r8   r>   r9   r:   r0   r;   r<   r?   r@   ÚabspathrB   rH   rC   Ú	_validaterE   Úmakedirsr   rA   r   ÚwriterF   rG   )r#   rI   r6   r   rJ   r8   r>   Ú	file_nameZ	file_pathÚ	workspacerN   rO   rL   rM   r$   r$   r%   r&   ¨   s@    



&zLabWorkspaceImportApp.startc             C   s|   t  |¡}d|krtdƒ‚d|kr*tdƒ‚d|d kr>tdƒ‚|d d }|t||ƒkrx| t||ƒ¡sxd}t|| ƒ‚|S )Nr3   zThe `data` field is missing.r4   z The `metadata` field is missing.r2   z(The `id` field is missing in `metadata`.z8%s does not match page_url or start with workspaces_url.)rF   ÚloadrE   r=   Ú
startswith)r#   r3   r6   r8   r>   rW   r2   Úerrorr$   r$   r%   rS   Ó   s    
zLabWorkspaceImportApp._validateN)r'   r(   r)   r   r*   r&   rS   r$   r$   r$   r%   rP   Ÿ   s   +rP   c                   sV   e Zd ZeZdZeƒ Zeej ¡ d fed< e	e	j ¡ d fed< ‡ fdd„Z
‡  ZS )ÚLabWorkspaceAppz¨
    Import or export a JupyterLab workspace

    There are two sub-commands for export or import of workspaces. This app
        should not otherwise do any work.
    r   ZexportÚimportc                s    t ƒ  ¡  tdƒ t d¡ d S )Nz.Either `export` or `import` must be specified.r   )Úsuperr&   r0   r;   r<   )r#   )Ú	__class__r$   r%   r&   ú   s    
zLabWorkspaceApp.start)r'   r(   r)   r   r*   rH   Úsubcommandsr1   Ú
splitlinesrP   r&   Ú__classcell__r$   r$   )r^   r%   r[   ç   s   r[   zLabApp.app_dirr5   Ú	core_modezStart the app in core mode.z	core-modeÚdev_modez2Start the app in dev mode for running from source.zdev-modeÚwatchzStart the app in watch mode.c            	       s(  e Zd ZeZdZdZeZeZ	e
eej ¡ d feej ¡ d feej ¡ d feej ¡ d feej ¡ d feej ¡ d fdZeddddZed	dd
dZed	dddZeeƒ dddZeeƒ dddZeeƒ dddZeddddZeddddZeddddZ‡ fdd„Z‡ fdd„Z ‡  Z!S )r5   aB  
    JupyterLab - An extensible computational environment for Jupyter.

    This launches a Tornado based HTML Server that serves up an
    HTML5/Javascript JupyterLab client.

    JupyterLab has three different modes of running:

    * Core mode (`--core-mode`): in this mode JupyterLab will run using the JavaScript
      assets contained in the installed `jupyterlab` Python package. In core mode, no
      extensions are enabled. This is the default in a stable JupyterLab release if you
      have no extensions installed.
    * Dev mode (`--dev-mode`): uses the unpublished local JavaScript packages in the
      `dev_mode` folder.  In this case JupyterLab will show a red stripe at the top of
      the page.  It can only be used if JupyterLab is installed as `pip install -e .`.
    * App mode: JupyterLab allows multiple JupyterLab "applications" to be
      created by the user with different combinations of extensions. The `--app-dir` can
      be used to set a directory for different applications. The default application
      path can be found using `jupyter lab path`.
    a°  
        jupyter lab                       # start JupyterLab
        jupyter lab --dev-mode            # start JupyterLab in development mode, with no extensions
        jupyter lab --core-mode           # start JupyterLab in core mode, with no extensions
        jupyter lab --app-dir=~/myjupyterlabapp # start JupyterLab with a particular set of extensions
        jupyter lab --certfile=mycert.pem # use SSL/TLS certificate
    r   )r   r   r@   ÚpathsrW   Z
workspacesz/labTz'The default URL to redirect to from `/`)r   r   r   z8The override url for static lab assets, typically a CDN.z>The override url for static lab theme assets, typically a CDN.z,The app directory to launch JupyterLab from.z The directory for user settings.zThe directory for workspacesFa„  Whether to start the app in core mode. In this mode, JupyterLab
        will run using the JavaScript assets that are within the installed
        JupyterLab Python package. In core mode, third party extensions are disabled.
        The `--dev-mode` flag is an alias to this to be used when the Python package
        itself is installed in development mode (`pip install -e .`).
        a  Whether to start the app in dev mode. Uses the unpublished local
        JavaScript packages in the `dev_mode` folder.  In this case JupyterLab will
        show a red stripe at the top of the page.  It can only be used if JupyterLab
        is installed as `pip install -e .`.
        z&Whether to serve the app in watch modec                s>   t ƒ j||Ž | jj}d|kr&i |d< t| ddƒ|d d< d S )NZpage_config_dataZquit_buttonF)r]   Úinit_webappZweb_appÚsettingsÚgetattr)r#   ÚargsÚkwargsrg   )r^   r$   r%   rf   `  s
    zLabApp.init_webappc                s8   t t| ƒ ¡  d}| j dd¡s4| j |¡ t| ƒ dS )aX  Load any extensions specified by config.

        Import the module, then call the load_jupyter_server_extension function,
        if one exists.

        If the JupyterLab server extension is not enabled, it will
        be manually loaded with a warning.

        The extension API is experimental, and may change in future releases.
        z<JupyterLab server extension not enabled, manually loading...Z
jupyterlabFN)r]   r5   Úinit_server_extensionsZnbserver_extensionsÚgetr    Úwarnr   )r#   Úmsg)r^   r$   r%   rk   i  s
    zLabApp.init_server_extensions)"r'   r(   r)   r   r*   ZexamplesÚlab_aliasesr   Ú	lab_flagsr   rH   r   r`   r-   r/   r[   r_   r   Zdefault_urlZoverride_static_urlZoverride_theme_urlr   r   r   Zuser_settings_dirr   r7   r
   rb   rc   rd   rf   rk   ra   r$   r$   )r^   r%   r5     s<   	Ú__main__)3Ú__doc__rF   r?   r;   Zjupyter_core.applicationr   r   Zjupyterlab_serverr   r   Znotebook.notebookappr   r   r   Znotebook.utilsr	   r=   Z	traitletsr
   r   Z_versionr   Ú	extensionr   r   Zcommandsr   r   r   r   r   r   rH   r+   r,   r   Zapp_versionr   r.   r-   r/   r1   rP   r[   ro   rp   r5   Zlaunch_instanceÚmainZlaunch_new_instancer'   r$   r$   r$   r%   Ú<module>   sV    


'%H





m

