Skip to content

Commit

Permalink
Merge pull request #47 from parente/env-filter
Browse files Browse the repository at this point in the history
Support CondaKernelSpecManager.env_filter regex
  • Loading branch information
bollwyvl authored Oct 12, 2016
2 parents fa870f1 + 3c47982 commit 1b5b428
Showing 1 changed file with 30 additions and 1 deletion.
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

0 comments on commit 1b5b428

Please sign in to comment.