Skip to content

Commit

Permalink
use ServerApp / NotebookApp instead of JupyterApp
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg1969 committed Mar 7, 2024
1 parent c2312f2 commit 10ca02d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
27 changes: 21 additions & 6 deletions nb_conda_kernels/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@

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__)


JA = "JupyterApp"
NBA = "NotebookApp"
SA = "ServerApp"
CKSM = "nb_conda_kernels.CondaKernelSpecManager"
JKSM = "jupyter_client.kernelspec.KernelSpecManager"
KSMC = "kernel_spec_manager_class"
Expand Down Expand Up @@ -107,19 +121,20 @@ 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 = ''
for fbase in (JC, JNC):
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
Expand Down Expand Up @@ -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 '<no files>'))
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'
Expand Down
20 changes: 10 additions & 10 deletions nb_conda_kernels/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)

Expand Down

0 comments on commit 10ca02d

Please sign in to comment.