##// 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:
@@ -19,12 +17,6 b" if os.name == 'nt':"
19 SYSTEM_KERNEL_DIR = None
17 SYSTEM_KERNEL_DIR = None
20 else:
18 else:
21 SYSTEM_KERNEL_DIR = "/usr/share/ipython/kernels"
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 NATIVE_KERNEL_NAME = 'python3' if PY3 else 'python2'
21 NATIVE_KERNEL_NAME = 'python3' if PY3 else 'python2'
30
22
@@ -61,53 +53,83 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):
73 """Makes a kernel directory for the native kernel.
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
74 kernel_dirs = List(
76 process. This will put its informatino in the user kernels directory.
75 help="List of kernel directories to search. Later ones take priority over earlier."
77 """
76 )
78 path = pjoin(USER_KERNEL_DIR, NATIVE_KERNEL_NAME)
77 def _kernel_dirs_default(self):
79 os.makedirs(path, mode=0o755)
78 return [
80 with open(pjoin(path, 'kernel.json'), 'w') as f:
79 SYSTEM_KERNEL_DIR,
81 json.dump({'argv':[NATIVE_KERNEL_NAME, '-c',
80 self.user_kernel_dir,
82 'from IPython.kernel.zmq.kernelapp import main; main()',
81 ]
83 '-f', '{connection_file}'],
82
84 'display_name': 'Python 3' if PY3 else 'Python 2',
83 def _make_native_kernel_dir(self):
85 'language': 'python',
84 """Makes a kernel directory for the native kernel.
86 'codemirror_mode': {'name': 'python',
85
87 'version': sys.version_info[0]},
86 The native kernel is the kernel using the same Python runtime as this
88 },
87 process. This will put its informatino in the user kernels directory.
89 f, indent=1)
88 """
90 # TODO: Copy icons into directory
89 path = pjoin(self.user_kernel_dir, NATIVE_KERNEL_NAME)
91 return path
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 def find_kernel_specs():
126 def find_kernel_specs():
94 """Returns a dict mapping kernel names to resource directories."""
127 """Returns a dict mapping kernel names to resource directories."""
95 d = {}
128 return KernelSpecManager().find_kernel_specs()
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?
103
129
104 def get_kernel_spec(kernel_name):
130 def get_kernel_spec(kernel_name):
105 """Returns a :class:`KernelSpec` instance for the given kernel_name.
131 """Returns a :class:`KernelSpec` instance for the given kernel_name.
106
132
107 Raises KeyError if the given kernel name is not found.
133 Raises KeyError if the given kernel name is not found.
108 """
134 """
109 if kernel_name == 'python':
135 return KernelSpecManager().get_kernel_spec(kernel_name) No newline at end of file
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
@@ -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,
@@ -91,6 +97,10 b' class KernelManager(LoggingConfigurable, ConnectionFileMixin):'
91 self.ipython_kernel = False
97 self.ipython_kernel = False
92
98
93 ipython_kernel = Bool(True)
99 ipython_kernel = Bool(True)
100
101 ipython_dir = Unicode()
102 def _ipython_dir_default(self):
103 return get_ipython_dir()
94
104
95 # Protected traits
105 # Protected traits
96 _launch_args = Any()
106 _launch_args = Any()
General Comments 0
You need to be logged in to leave comments. Login now