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