##// END OF EJS Templates
Allow IPython directory to be passed down to kernel selection from App...
Thomas Kluyver -
Show More
@@ -341,6 +341,7 b' class IPythonConsoleApp(ConnectionFileMixin):'
341 connection_file=self.connection_file,
341 connection_file=self.connection_file,
342 kernel_name=self.kernel_name,
342 kernel_name=self.kernel_name,
343 parent=self,
343 parent=self,
344 ipython_dir=self.ipython_dir,
344 )
345 )
345 self.kernel_manager.client_factory = self.kernel_client_class
346 self.kernel_manager.client_factory = self.kernel_client_class
346 self.kernel_manager.start_kernel(extra_arguments=self.kernel_argv)
347 self.kernel_manager.start_kernel(extra_arguments=self.kernel_argv)
@@ -9,8 +9,6 b' from IPython.utils.path import get_ipython_dir'
9 from IPython.utils.py3compat import PY3
9 from IPython.utils.py3compat import PY3
10 from IPython.utils.traitlets import HasTraits, List, Unicode, Dict
10 from IPython.utils.traitlets import HasTraits, List, Unicode, Dict
11
11
12 USER_KERNEL_DIR = pjoin(get_ipython_dir(), 'kernels')
13
14 if os.name == 'nt':
12 if os.name == 'nt':
15 programdata = os.environ.get('PROGRAMDATA', None)
13 programdata = os.environ.get('PROGRAMDATA', None)
16 if programdata:
14 if programdata:
@@ -20,12 +18,6 b" if os.name == 'nt':"
20 else:
18 else:
21 SYSTEM_KERNEL_DIR = "/usr/share/ipython/kernels"
19 SYSTEM_KERNEL_DIR = "/usr/share/ipython/kernels"
22
20
23 # List of kernel directories to search. Later ones take priority over earlier.
24 kernel_dirs = [
25 SYSTEM_KERNEL_DIR,
26 USER_KERNEL_DIR,
27 ]
28
29 NATIVE_KERNEL_NAME = 'python3' if PY3 else 'python2'
21 NATIVE_KERNEL_NAME = 'python3' if PY3 else 'python2'
30
22
31 class KernelSpec(HasTraits):
23 class KernelSpec(HasTraits):
@@ -61,21 +53,40 b' def _is_kernel_dir(path):'
61 return os.path.isdir(path) and os.path.isfile(pjoin(path, 'kernel.json'))
53 return os.path.isdir(path) and os.path.isfile(pjoin(path, 'kernel.json'))
62
54
63 def _list_kernels_in(dir):
55 def _list_kernels_in(dir):
64 """Ensure dir exists, and return a mapping of kernel names to resource
56 """Return a mapping of kernel names to resource directories from dir.
65 directories from it.
57
58 If dir is None or does not exist, returns an empty dict.
66 """
59 """
67 if dir is None or not os.path.isdir(dir):
60 if dir is None or not os.path.isdir(dir):
68 return {}
61 return {}
69 return {f.lower(): pjoin(dir, f) for f in os.listdir(dir)
62 return {f.lower(): pjoin(dir, f) for f in os.listdir(dir)
70 if _is_kernel_dir(pjoin(dir, f))}
63 if _is_kernel_dir(pjoin(dir, f))}
71
64
72 def _make_native_kernel_dir():
65 class KernelSpecManager(HasTraits):
66 ipython_dir = Unicode()
67 def _ipython_dir_default(self):
68 return get_ipython_dir()
69
70 user_kernel_dir = Unicode()
71 def _user_kernel_dir_default(self):
72 return pjoin(self.ipython_dir, 'kernels')
73
74 kernel_dirs = List(
75 help="List of kernel directories to search. Later ones take priority over earlier."
76 )
77 def _kernel_dirs_default(self):
78 return [
79 SYSTEM_KERNEL_DIR,
80 self.user_kernel_dir,
81 ]
82
83 def _make_native_kernel_dir(self):
73 """Makes a kernel directory for the native kernel.
84 """Makes a kernel directory for the native kernel.
74
85
75 The native kernel is the kernel using the same Python runtime as this
86 The native kernel is the kernel using the same Python runtime as this
76 process. This will put its informatino in the user kernels directory.
87 process. This will put its informatino in the user kernels directory.
77 """
88 """
78 path = pjoin(USER_KERNEL_DIR, NATIVE_KERNEL_NAME)
89 path = pjoin(self.user_kernel_dir, NATIVE_KERNEL_NAME)
79 os.makedirs(path, mode=0o755)
90 os.makedirs(path, mode=0o755)
80 with open(pjoin(path, 'kernel.json'), 'w') as f:
91 with open(pjoin(path, 'kernel.json'), 'w') as f:
81 json.dump({'argv':[NATIVE_KERNEL_NAME, '-c',
92 json.dump({'argv':[NATIVE_KERNEL_NAME, '-c',
@@ -90,24 +101,35 b' def _make_native_kernel_dir():'
90 # TODO: Copy icons into directory
101 # TODO: Copy icons into directory
91 return path
102 return path
92
103
93 def find_kernel_specs():
104 def find_kernel_specs(self):
94 """Returns a dict mapping kernel names to resource directories."""
105 """Returns a dict mapping kernel names to resource directories."""
95 d = {}
106 d = {}
96 for kernel_dir in kernel_dirs:
107 for kernel_dir in self.kernel_dirs:
97 d.update(_list_kernels_in(kernel_dir))
108 d.update(_list_kernels_in(kernel_dir))
98
109
99 if NATIVE_KERNEL_NAME not in d:
110 if NATIVE_KERNEL_NAME not in d:
100 d[NATIVE_KERNEL_NAME] = _make_native_kernel_dir()
111 d[NATIVE_KERNEL_NAME] = self._make_native_kernel_dir()
101 return d
112 return d
102 # TODO: Caching?
113 # TODO: Caching?
103
114
104 def get_kernel_spec(kernel_name):
115 def get_kernel_spec(self, kernel_name):
105 """Returns a :class:`KernelSpec` instance for the given kernel_name.
116 """Returns a :class:`KernelSpec` instance for the given kernel_name.
106
117
107 Raises KeyError if the given kernel name is not found.
118 Raises KeyError if the given kernel name is not found.
108 """
119 """
109 if kernel_name == 'python':
120 if kernel_name == 'python':
110 kernel_name = NATIVE_KERNEL_NAME
121 kernel_name = NATIVE_KERNEL_NAME
111 d = find_kernel_specs()
122 d = self.find_kernel_specs()
112 resource_dir = d[kernel_name.lower()]
123 resource_dir = d[kernel_name.lower()]
113 return KernelSpec.from_resource_dir(resource_dir)
124 return KernelSpec.from_resource_dir(resource_dir)
125
126 def find_kernel_specs():
127 """Returns a dict mapping kernel names to resource directories."""
128 return KernelSpecManager().find_kernel_specs()
129
130 def get_kernel_spec(kernel_name):
131 """Returns a :class:`KernelSpec` instance for the given kernel_name.
132
133 Raises KeyError if the given kernel name is not found.
134 """
135 return KernelSpecManager().get_kernel_spec(kernel_name) No newline at end of file
@@ -19,6 +19,7 b' import zmq'
19 from IPython.config.configurable import LoggingConfigurable
19 from IPython.config.configurable import LoggingConfigurable
20 from IPython.utils.importstring import import_item
20 from IPython.utils.importstring import import_item
21 from IPython.utils.localinterfaces import is_local_ip, local_ips
21 from IPython.utils.localinterfaces import is_local_ip, local_ips
22 from IPython.utils.path import get_ipython_dir
22 from IPython.utils.traitlets import (
23 from IPython.utils.traitlets import (
23 Any, Instance, Unicode, List, Bool, Type, DottedObjectName
24 Any, Instance, Unicode, List, Bool, Type, DottedObjectName
24 )
25 )
@@ -60,15 +61,20 b' class KernelManager(LoggingConfigurable, ConnectionFileMixin):'
60 # generally a Popen instance
61 # generally a Popen instance
61 kernel = Any()
62 kernel = Any()
62
63
64 kernel_spec_manager = Instance(kernelspec.KernelSpecManager)
65
66 def _kernel_spec_manager_default(self):
67 return kernelspec.KernelSpecManager(ipython_dir=self.ipython_dir)
68
63 kernel_name = Unicode('python')
69 kernel_name = Unicode('python')
64
70
65 kernel_spec = Instance(kernelspec.KernelSpec)
71 kernel_spec = Instance(kernelspec.KernelSpec)
66
72
67 def _kernel_spec_default(self):
73 def _kernel_spec_default(self):
68 return kernelspec.get_kernel_spec(self.kernel_name)
74 return self.kernel_spec_manager.get_kernel_spec(self.kernel_name)
69
75
70 def _kernel_name_changed(self, name, old, new):
76 def _kernel_name_changed(self, name, old, new):
71 self.kernel_spec = kernelspec.get_kernel_spec(new)
77 self.kernel_spec = self.kernel_spec_manager.get_kernel_spec(new)
72 self.ipython_kernel = new in {'python', 'python2', 'python3'}
78 self.ipython_kernel = new in {'python', 'python2', 'python3'}
73
79
74 kernel_cmd = List(Unicode, config=True,
80 kernel_cmd = List(Unicode, config=True,
@@ -92,6 +98,10 b' class KernelManager(LoggingConfigurable, ConnectionFileMixin):'
92
98
93 ipython_kernel = Bool(True)
99 ipython_kernel = Bool(True)
94
100
101 ipython_dir = Unicode()
102 def _ipython_dir_default(self):
103 return get_ipython_dir()
104
95 # Protected traits
105 # Protected traits
96 _launch_args = Any()
106 _launch_args = Any()
97 _control_socket = Any()
107 _control_socket = Any()
General Comments 0
You need to be logged in to leave comments. Login now