Skip to content

Commit

Permalink
python310Packages.experiment-utilities: unvendor ipynbname
Browse files Browse the repository at this point in the history
  • Loading branch information
dotlambda committed Aug 3, 2023
1 parent 6cb8289 commit 2c22bd6
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
, fasteners
, fetchFromGitLab
, qgrid
, ipynbname
, ipywidgets
, odfpy
, scipy
Expand All @@ -28,6 +29,10 @@ buildPythonPackage rec {
hash = "sha256-zjmmLUpGjUhpw2+stLJE6cImesnBSvrcid5bHMftX/Q=";
};

patches = [
./unvendor-ipynbname.patch
];

# This dependency constraint (<=7.6.5) was due to a bug in qgrid that has been patched in its
# owned derivation
postPatch = ''
Expand All @@ -39,6 +44,7 @@ buildPythonPackage rec {
cloudpickle
dill
fasteners
ipynbname
ipywidgets
odfpy
plotly
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
diff --git a/exputils/gui/jupyter/__init__.py b/exputils/gui/jupyter/__init__.py
index 6e9aefb..fdfdd28 100644
--- a/exputils/gui/jupyter/__init__.py
+++ b/exputils/gui/jupyter/__init__.py
@@ -30,8 +30,8 @@ from exputils.gui.jupyter.misc import remove_children_from_widget
from exputils.gui.jupyter.misc import set_children_of_widget
from exputils.gui.jupyter.misc import generate_random_state_backup_name

-from exputils.gui.jupyter.ipynbname import get_notebook_name
-from exputils.gui.jupyter.ipynbname import get_notebook_path
+from ipynbname import name as get_notebook_name
+from ipynbname import path as get_notebook_path

DEFAULT_CONFIG_DIRECTORY = '.ipython_config'

diff --git a/exputils/gui/jupyter/ipynbname.py b/exputils/gui/jupyter/ipynbname.py
deleted file mode 100644
index 51e21b7..0000000
--- a/exputils/gui/jupyter/ipynbname.py
+++ /dev/null
@@ -1,86 +0,0 @@
-##
-## This file is part of the exputils package.
-##
-## Copyright: INRIA
-## Year: 2022, 2023
-## Contact: chris.reinke@inria.fr
-##
-## exputils is provided under GPL-3.0-or-later
-##
-# Taken from https://pypi.org/project/ipynbname/
-# TODO: add them to licence
-
-from notebook import notebookapp
-import urllib, json, os, ipykernel, ntpath
-
-FILE_ERROR = "Can't identify the notebook {}."
-CONN_ERROR = "Unable to access server;\n \
- + ipynbname requires either no security or token based security."
-
-def _get_kernel_id():
- """ Returns the kernel ID of the ipykernel.
- """
- connection_file = os.path.basename(ipykernel.get_connection_file())
- kernel_id = connection_file.split('-', 1)[1].split('.')[0]
- return kernel_id
-
-
-def _get_sessions(srv):
- """ Given a server, returns sessions, or HTTPError if access is denied.
- NOTE: Works only when either there is no security or there is token
- based security. An HTTPError is raised if unable to connect to a
- server.
- """
- try:
- qry_str = ""
- token = srv['token']
- if token:
- qry_str = f"?token={token}"
- url = f"{srv['url']}api/sessions{qry_str}"
- req = urllib.request.urlopen(url)
- return json.load(req)
- except:
- raise urllib.error.HTTPError(CONN_ERROR)
-
-
-def _get_nb_path(sess, kernel_id):
- """ Given a session and kernel ID, returns the notebook path for the
- session, or None if there is no notebook for the session.
- """
- if sess['kernel']['id'] == kernel_id:
- return sess['notebook']['path']
- return None
-
-
-def get_notebook_name():
- """ Returns the short name of the notebook w/o the .ipynb extension,
- or raises a FileNotFoundError exception if it cannot be determined.
- """
- kernel_id = _get_kernel_id()
- for srv in notebookapp.list_running_servers():
- try:
- sessions = _get_sessions(srv)
- for sess in sessions:
- nb_path = _get_nb_path(sess, kernel_id)
- if nb_path:
- return ntpath.basename(nb_path).replace('.ipynb', '')
- except:
- pass # There may be stale entries in the runtime directory
- raise FileNotFoundError(FILE_ERROR.format('name'))
-
-
-def get_notebook_path():
- """ Returns the absolute path of the notebook,
- or raises a FileNotFoundError exception if it cannot be determined.
- """
- kernel_id = _get_kernel_id()
- for srv in notebookapp.list_running_servers():
- try:
- sessions = _get_sessions(srv)
- for sess in sessions:
- nb_path = _get_nb_path(sess, kernel_id)
- if nb_path:
- return os.path.join(srv['notebook_dir'], nb_path)
- except:
- pass # There may be stale entries in the runtime directory
- raise FileNotFoundError(FILE_ERROR.format('path'))
\ No newline at end of file
diff --git a/setup.cfg b/setup.cfg
index 9d9cbb0..6080ed6 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -25,3 +25,4 @@ install_requires =
tensorboard >= 1.15.0
fasteners >= 0.18
pyyaml >= 6.0
+ ipynbname

0 comments on commit 2c22bd6

Please sign in to comment.