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