##// END OF EJS Templates
Better describe functions in kernelspec module
Thomas Kluyver -
Show More
@@ -40,15 +40,23 b' class KernelSpec(HasTraits):'
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):
@@ -57,6 +65,11 b' def _list_kernels_in(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:
@@ -73,16 +86,17 b' def _make_native_kernel_dir():'
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 """
General Comments 0
You need to be logged in to leave comments. Login now