Show More
@@ -1,105 +1,112 b'' | |||||
1 | import io |
|
1 | import io | |
2 | import json |
|
2 | import json | |
3 | import os |
|
3 | import os | |
4 | import sys |
|
4 | import sys | |
5 |
|
5 | |||
6 | pjoin = os.path.join |
|
6 | pjoin = os.path.join | |
7 |
|
7 | |||
8 | from IPython.utils.path import get_ipython_dir |
|
8 | 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 |
|
10 | from IPython.utils.traitlets import HasTraits, List, Unicode | |
11 |
|
11 | |||
12 | USER_KERNEL_DIR = pjoin(get_ipython_dir(), 'kernels') |
|
12 | USER_KERNEL_DIR = pjoin(get_ipython_dir(), 'kernels') | |
13 |
|
13 | |||
14 | if os.name == 'nt': |
|
14 | if os.name == 'nt': | |
15 | programdata = os.environ.get('PROGRAMDATA', None) |
|
15 | programdata = os.environ.get('PROGRAMDATA', None) | |
16 | if programdata: |
|
16 | if programdata: | |
17 | SYSTEM_KERNEL_DIR = pjoin(programdata, 'ipython', 'kernels') |
|
17 | SYSTEM_KERNEL_DIR = pjoin(programdata, 'ipython', 'kernels') | |
18 | else: # PROGRAMDATA is not defined by default on XP. |
|
18 | else: # PROGRAMDATA is not defined by default on XP. | |
19 | SYSTEM_KERNEL_DIR = None |
|
19 | SYSTEM_KERNEL_DIR = None | |
20 | else: |
|
20 | else: | |
21 | SYSTEM_KERNEL_DIR = "/usr/share/ipython/kernels" |
|
21 | 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 | ] | |||
22 |
|
28 | |||
23 | NATIVE_KERNEL_NAME = 'python3' if PY3 else 'python2' |
|
29 | NATIVE_KERNEL_NAME = 'python3' if PY3 else 'python2' | |
24 |
|
30 | |||
25 | class KernelSpec(HasTraits): |
|
31 | class KernelSpec(HasTraits): | |
26 | argv = List() |
|
32 | argv = List() | |
27 | display_name = Unicode() |
|
33 | display_name = Unicode() | |
28 | language = Unicode() |
|
34 | language = Unicode() | |
29 | codemirror_mode = None |
|
35 | codemirror_mode = None | |
30 |
|
36 | |||
31 | resource_dir = Unicode() |
|
37 | resource_dir = Unicode() | |
32 |
|
38 | |||
33 | def __init__(self, resource_dir, argv, display_name, language, |
|
39 | def __init__(self, resource_dir, argv, display_name, language, | |
34 | codemirror_mode=None): |
|
40 | codemirror_mode=None): | |
35 | super(KernelSpec, self).__init__(resource_dir=resource_dir, argv=argv, |
|
41 | super(KernelSpec, self).__init__(resource_dir=resource_dir, argv=argv, | |
36 | display_name=display_name, language=language, |
|
42 | display_name=display_name, language=language, | |
37 | codemirror_mode=codemirror_mode) |
|
43 | codemirror_mode=codemirror_mode) | |
38 | if not self.codemirror_mode: |
|
44 | if not self.codemirror_mode: | |
39 | self.codemirror_mode = self.language |
|
45 | self.codemirror_mode = self.language | |
40 |
|
46 | |||
41 | @classmethod |
|
47 | @classmethod | |
42 | def from_resource_dir(cls, resource_dir): |
|
48 | def from_resource_dir(cls, resource_dir): | |
43 | """Create a KernelSpec object by reading kernel.json |
|
49 | """Create a KernelSpec object by reading kernel.json | |
44 |
|
50 | |||
45 | Pass the path to the *directory* containing kernel.json. |
|
51 | Pass the path to the *directory* containing kernel.json. | |
46 | """ |
|
52 | """ | |
47 | kernel_file = pjoin(resource_dir, 'kernel.json') |
|
53 | kernel_file = pjoin(resource_dir, 'kernel.json') | |
48 | with io.open(kernel_file, 'r', encoding='utf-8') as f: |
|
54 | with io.open(kernel_file, 'r', encoding='utf-8') as f: | |
49 | kernel_dict = json.load(f) |
|
55 | kernel_dict = json.load(f) | |
50 | return cls(resource_dir=resource_dir, **kernel_dict) |
|
56 | return cls(resource_dir=resource_dir, **kernel_dict) | |
51 |
|
57 | |||
52 | def _is_kernel_dir(path): |
|
58 | def _is_kernel_dir(path): | |
53 | """Is ``path`` a kernel directory?""" |
|
59 | """Is ``path`` a kernel directory?""" | |
54 | return os.path.isdir(path) and os.path.isfile(pjoin(path, 'kernel.json')) |
|
60 | return os.path.isdir(path) and os.path.isfile(pjoin(path, 'kernel.json')) | |
55 |
|
61 | |||
56 | def _list_kernels_in(dir): |
|
62 | def _list_kernels_in(dir): | |
57 | """Ensure dir exists, and return a mapping of kernel names to resource |
|
63 | """Ensure dir exists, and return a mapping of kernel names to resource | |
58 | directories from it. |
|
64 | directories from it. | |
59 | """ |
|
65 | """ | |
60 | if dir is None or not os.path.isdir(dir): |
|
66 | if dir is None or not os.path.isdir(dir): | |
61 | return {} |
|
67 | return {} | |
62 | return {f.lower(): pjoin(dir, f) for f in os.listdir(dir) |
|
68 | return {f.lower(): pjoin(dir, f) for f in os.listdir(dir) | |
63 | if _is_kernel_dir(pjoin(dir, f))} |
|
69 | if _is_kernel_dir(pjoin(dir, f))} | |
64 |
|
70 | |||
65 | def _make_native_kernel_dir(): |
|
71 | def _make_native_kernel_dir(): | |
66 | """Makes a kernel directory for the native kernel. |
|
72 | """Makes a kernel directory for the native kernel. | |
67 |
|
73 | |||
68 | The native kernel is the kernel using the same Python runtime as this |
|
74 | The native kernel is the kernel using the same Python runtime as this | |
69 | process. This will put its informatino in the user kernels directory. |
|
75 | process. This will put its informatino in the user kernels directory. | |
70 | """ |
|
76 | """ | |
71 | path = pjoin(USER_KERNEL_DIR, NATIVE_KERNEL_NAME) |
|
77 | path = pjoin(USER_KERNEL_DIR, NATIVE_KERNEL_NAME) | |
72 | os.makedirs(path, mode=0o755) |
|
78 | os.makedirs(path, mode=0o755) | |
73 | with io.open(pjoin(path, 'kernel.json'), 'w', encoding='utf-8') as f: |
|
79 | with io.open(pjoin(path, 'kernel.json'), 'w', encoding='utf-8') as f: | |
74 | json.dump({'argv':[NATIVE_KERNEL_NAME, '-c', |
|
80 | json.dump({'argv':[NATIVE_KERNEL_NAME, '-c', | |
75 | 'from IPython.kernel.zmq.kernelapp import main; main()', |
|
81 | 'from IPython.kernel.zmq.kernelapp import main; main()', | |
76 | '-f', '{connection_file}'], |
|
82 | '-f', '{connection_file}'], | |
77 | 'display_name': 'Python 3' if PY3 else 'Python 2', |
|
83 | 'display_name': 'Python 3' if PY3 else 'Python 2', | |
78 | 'language': 'python', |
|
84 | 'language': 'python', | |
79 | 'codemirror_mode': {'name': 'python', |
|
85 | 'codemirror_mode': {'name': 'python', | |
80 | 'version': sys.version_info[0]}, |
|
86 | 'version': sys.version_info[0]}, | |
81 | }, |
|
87 | }, | |
82 | f, indent=1) |
|
88 | f, indent=1) | |
83 | # TODO: Copy icons into directory |
|
89 | # TODO: Copy icons into directory | |
84 | return path |
|
90 | return path | |
85 |
|
91 | |||
86 | def list_kernel_specs(): |
|
92 | def list_kernel_specs(): | |
87 | """Returns a dict mapping kernel names to resource directories.""" |
|
93 | """Returns a dict mapping kernel names to resource directories.""" | |
88 | d = _list_kernels_in(SYSTEM_KERNEL_DIR) |
|
94 | d = {} | |
89 | d.update(_list_kernels_in(USER_KERNEL_DIR)) |
|
95 | for kernel_dir in kernel_dirs: | |
|
96 | d.update(_list_kernels_in(kernel_dir)) | |||
90 |
|
97 | |||
91 | if NATIVE_KERNEL_NAME not in d: |
|
98 | if NATIVE_KERNEL_NAME not in d: | |
92 | d[NATIVE_KERNEL_NAME] = _make_native_kernel_dir() |
|
99 | d[NATIVE_KERNEL_NAME] = _make_native_kernel_dir() | |
93 | return d |
|
100 | return d | |
94 | # TODO: Caching? |
|
101 | # TODO: Caching? | |
95 |
|
102 | |||
96 | def get_kernel_spec(kernel_name): |
|
103 | def get_kernel_spec(kernel_name): | |
97 | """Returns a :class:`KernelSpec` instance for the given kernel_name. |
|
104 | """Returns a :class:`KernelSpec` instance for the given kernel_name. | |
98 |
|
105 | |||
99 | Raises KeyError if the given kernel name is not found. |
|
106 | Raises KeyError if the given kernel name is not found. | |
100 | """ |
|
107 | """ | |
101 | if kernel_name == 'native': |
|
108 | if kernel_name == 'native': | |
102 | kernel_name = NATIVE_KERNEL_NAME |
|
109 | kernel_name = NATIVE_KERNEL_NAME | |
103 | d = list_kernel_specs() |
|
110 | d = list_kernel_specs() | |
104 | resource_dir = d[kernel_name.lower()] |
|
111 | resource_dir = d[kernel_name.lower()] | |
105 | return KernelSpec.from_resource_dir(resource_dir) No newline at end of file |
|
112 | return KernelSpec.from_resource_dir(resource_dir) |
General Comments 0
You need to be logged in to leave comments.
Login now