Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CondaKernelSpecManager.env_filter regex #47

Merged
merged 1 commit into from
Oct 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion nb_conda_kernels/manager.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
import json
import re
import subprocess
import sys
import time

from os.path import exists, join, split, dirname, abspath
from traitlets import Unicode

from jupyter_client.kernelspec import (
KernelSpecManager,
Expand All @@ -19,6 +21,9 @@ class CondaKernelSpecManager(KernelSpecManager):
""" A custom KernelSpecManager able to search for conda environments and
create kernelspecs for them.
"""
env_filter = Unicode(None, config=True, allow_none=True,
help="Do not list environment names that match this regex")

def __init__(self, **kwargs):
super(CondaKernelSpecManager, self).__init__(**kwargs)

Expand All @@ -28,6 +33,9 @@ def __init__(self, **kwargs):
self._conda_kernels_cache = None
self._conda_kernels_cache_expiry = None

if self.env_filter is not None:
self._env_filter_regex = re.compile(self.env_filter)

self.log.info("[nb_conda_kernels] enabled, %s kernels found",
len(self._conda_kspecs))

Expand Down Expand Up @@ -56,6 +64,27 @@ def _conda_info(self):

return self._conda_info_cache

def _skip_env(self, path):
"""Get whether the environment should be included in the kernel specs or
not based on whether its path matches env_filter.

If the filter regex is None, always returns False (i.e., never skips).

Parameters
----------
path: str
Full path of the conda environment

Returns
-------
bool
True if the filter matches and the env should not be included in the
kernel specs.
"""
if self.env_filter is None:
return False
return self._env_filter_regex.search(path) is not None

def _all_envs(self):
""" Find the all the executables for each env where jupyter is
installed.
Expand Down Expand Up @@ -83,7 +112,7 @@ def get_paths_by_env(display_prefix, language_key, language_exe, envs):
language_envs = {}
for base in envs:
exe_path = join(base, language_exe)
if exists(join(base, jupyter)) and exists(exe_path):
if exists(join(base, jupyter)) and exists(exe_path) and not self._skip_env(base):
env_name = split(base)[1]
name = 'conda-env-{}-{}'.format(env_name, language_key)
language_envs[name] = {
Expand Down