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