From 10ca02dfce324f638933ff38f046b1e15e0c67ab Mon Sep 17 00:00:00 2001 From: "Michael C. Grant" Date: Thu, 7 Mar 2024 12:29:02 -0600 Subject: [PATCH] use ServerApp / NotebookApp instead of JupyterApp --- nb_conda_kernels/install.py | 27 +++++++++++++++++++++------ nb_conda_kernels/manager.py | 20 ++++++++++---------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/nb_conda_kernels/install.py b/nb_conda_kernels/install.py index d6356ae..d441434 100644 --- a/nb_conda_kernels/install.py +++ b/nb_conda_kernels/install.py @@ -6,9 +6,22 @@ from os.path import join, abspath, exists -from traitlets.config.manager import BaseJSONConfigManager from jupyter_core.paths import jupyter_config_path -from jupyter_client import __version__ as jc_version + +try: + from jupyter_server.config_manager import BaseJSONConfigManager +except ImportError: + try: + from notebook.config_manager import BaseJSONConfigManager + except ImportError: + raise ImportError("Must have notebook >=5.3.0 or jupyter_server installed") + +NEED_NOTEBOOK = False +try: + from notebook import __version__ as nb_version + NEED_NOTEBOOK = int(nb_version.split('.', 1)[0]) < 7 +except ImportError: + pass log = logging.getLogger(__name__) @@ -16,6 +29,7 @@ JA = "JupyterApp" NBA = "NotebookApp" +SA = "ServerApp" CKSM = "nb_conda_kernels.CondaKernelSpecManager" JKSM = "jupyter_client.kernelspec.KernelSpecManager" KSMC = "kernel_spec_manager_class" @@ -107,6 +121,7 @@ def install(enable=False, disable=False, status=None, prefix=None, path=None, ve fpaths = set() is_enabled_all = {} is_enabled_local = {} + need_keys = (SA, NBA) if NEED_NOTEBOOK else (SA,) for path_g in search_paths: flag = '-' if path != path_g else ('*' if path in all_paths else 'x') value = '' @@ -114,12 +129,12 @@ def install(enable=False, disable=False, status=None, prefix=None, path=None, ve fpath = join(path_g, fbase + '.json') cfg = BaseJSONConfigManager(config_dir=path_g).get(fbase) dirty = False - for key in (JA, NBA): + for key in (JA, NBA, SA): spec = cfg.get(key, {}).get(KSMC) if status or path_g != path: # No changes in status mode, or if we're not in the target path expected = spec - elif enable and fbase == JC and key == JA: + elif enable and fbase == JC and key in need_keys: # Add the spec if we are enabling, the entry point is not active, # and we're using the new file (jupyter_config.json) and key (JupyterApp) expected = CKSM @@ -151,8 +166,8 @@ def install(enable=False, disable=False, status=None, prefix=None, path=None, ve value += ': ' value += '\n '.join(json.dumps(cfg, indent=2).splitlines()) log.debug(' {} {}: {}'.format(flag, shorten(path_g), value or '')) - is_enabled_all = bool(is_enabled_all.get(NBA, is_enabled_all.get(JA))) - is_enabled_local = bool(is_enabled_local.get(NBA, is_enabled_local.get(JA))) + is_enabled_all = all(is_enabled_all.get(k) for k in need_keys) + is_enabled_local = all(is_enabled_local.get(k) for k in need_keys) if is_enabled_all != is_enabled_local: sev = 'WARNING' if status else 'ERROR' diff --git a/nb_conda_kernels/manager.py b/nb_conda_kernels/manager.py index 5178717..114f133 100644 --- a/nb_conda_kernels/manager.py +++ b/nb_conda_kernels/manager.py @@ -54,7 +54,7 @@ def _validate_kernelspec_path(self, proposal): if new_value not in ("", "--user", "--sys-prefix"): if not os.path.isdir(self.kernelspec_path): raise TraitError("CondaKernelSpecManager.kernelspec_path is not a directory.") - self.log.debug("[nb_conda_kernels] Force conda_only=True as kernelspec_path is not None.") + self.log.debug("nb_conda_kernels | Force conda_only=True as kernelspec_path is not None.") self.conda_only = True return new_value @@ -92,7 +92,7 @@ def __init__(self, **kwargs): self._kernel_prefix = sys.prefix if self.kernelspec_path == "--sys-prefix" else self.kernelspec_path self.log.info( - "[nb_conda_kernels] enabled, %s kernels found", len(self._conda_kspecs) + "nb_conda_kernels | enabled, %s kernels found.", len(self._conda_kspecs) ) @staticmethod @@ -147,10 +147,10 @@ def run(self): # cache is empty if expiry is None: - self.log.debug("[nb_conda_kernels] refreshing conda info (blocking call)") + self.log.debug("nb_conda_kernels | refreshing conda info (blocking call)") conda_info, err = get_conda_info_data() if conda_info is None: - self.log.error("[nb_conda_kernels] couldn't call conda:\n%s", err) + self.log.error("nb_conda_kernels | couldn't call conda:\n%s", err) self._conda_info_cache = conda_info self._conda_info_cache_expiry = time.time() + CACHE_TIMEOUT @@ -159,16 +159,16 @@ def run(self): t.join() conda_info = t.out if conda_info is None: - self.log.error("[nb_conda_kernels] couldn't call conda:\n%s", t.err) + self.log.error("nb_conda_kernels | couldn't call conda:\n%s", t.err) else: - self.log.debug("[nb_conda_kernels] collected conda info (async call)") + self.log.debug("nb_conda_kernels | collected conda info (async call)") self._conda_info_cache = conda_info self._conda_info_cache_expiry = time.time() + CACHE_TIMEOUT self._conda_info_cache_thread = None # cache expired elif not t and expiry < time.time(): - self.log.debug("[nb_conda_kernels] refreshing conda info (async call)") + self.log.debug("nb_conda_kernels | refreshing conda info (async call)") t = CondaInfoThread() t.start() self._conda_info_cache_thread = t @@ -249,13 +249,13 @@ def _all_specs(self): data = fp.read() spec = json.loads(data.decode('utf-8')) except Exception as err: - self.log.error("[nb_conda_kernels] error loading %s:\n%s", + self.log.error("nb_conda_kernels | error loading %s:\n%s", spec_path, err) continue kernel_dir = dirname(spec_path).lower() kernel_name = raw_kernel_name = basename(kernel_dir) if self.kernelspec_path is not None and kernel_name.startswith("conda-"): - self.log.debug("[nb_conda_kernels] Skipping kernel spec %s", spec_path) + self.log.debug("nb_conda_kernels | Skipping kernel spec %s", spec_path) continue # Ensure to skip dynamically added kernel spec within the environment prefix # We're doing a few of these adjustments here to ensure that # the naming convention is as close as possible to the previous @@ -312,7 +312,7 @@ def _all_specs(self): json.dump(tmp_spec, f) except OSError as error: self.log.warning( - u"[nb_conda_kernels] Fail to install kernel '{}'.".format(kernel_dir), + u"nb_conda_kernels | Fail to install kernel '{}'.".format(kernel_dir), exc_info=error )