|
2 | 2 | from functools import partial
|
3 | 3 | from io import StringIO
|
4 | 4 | import os
|
5 |
| -import shutil |
| 5 | +from pathlib import Path |
6 | 6 | import sys
|
7 |
| -import tempfile |
| 7 | +from tempfile import TemporaryDirectory |
8 | 8 |
|
9 | 9 | from IPython.display import Image
|
10 | 10 | from metakernel import MetaKernel
|
@@ -78,24 +78,25 @@ def do_execute_direct(self, code):
|
78 | 78 | if settings["backend"] == "inline":
|
79 | 79 | nfig = len(self._matlab.get(0., "children"))
|
80 | 80 | if nfig:
|
81 |
| - tmpdir = tempfile.mkdtemp() |
82 |
| - try: |
83 |
| - self._matlab.eval( |
84 |
| - "arrayfun(" |
85 |
| - "@(h, i) print(h, sprintf('{}/%i', i), '-d{}', '-r{}')," |
86 |
| - "get(0, 'children'), (1:{})')" |
87 |
| - .format(tmpdir, settings["format"], settings["resolution"], nfig), |
88 |
| - nargout=0) |
89 |
| - self._matlab.eval( |
90 |
| - "arrayfun(@(h) close(h), get(0, 'children'))", |
91 |
| - nargout=0) |
92 |
| - for fname in sorted(os.listdir(tmpdir)): |
93 |
| - self.Display(Image( |
94 |
| - filename="{}/{}".format(tmpdir, fname))) |
95 |
| - except Exception as exc: |
96 |
| - self.Error(exc) |
97 |
| - finally: |
98 |
| - shutil.rmtree(tmpdir) |
| 81 | + with TemporaryDirectory() as tmpdir: |
| 82 | + try: |
| 83 | + self._matlab.eval( |
| 84 | + "arrayfun(" |
| 85 | + "@(h, i) print(h, sprintf('{}/%i', i), '-d{}', '-r{}')," |
| 86 | + "get(0, 'children'), (1:{})')".format( |
| 87 | + tmpdir, |
| 88 | + settings["format"], |
| 89 | + settings["resolution"], |
| 90 | + nfig), |
| 91 | + nargout=0) |
| 92 | + self._matlab.eval( |
| 93 | + "arrayfun(@(h) close(h), get(0, 'children'))", |
| 94 | + nargout=0) |
| 95 | + for fname in sorted(os.listdir(tmpdir)): |
| 96 | + self.Display(Image( |
| 97 | + filename="{}/{}".format(tmpdir, fname))) |
| 98 | + except Exception as exc: |
| 99 | + self.Error(exc) |
99 | 100 |
|
100 | 101 | def get_kernel_help_on(self, info, level=0, none_on_fail=False):
|
101 | 102 | name = info.get("help_obj", "")
|
@@ -146,6 +147,19 @@ def get_completions(self, info):
|
146 | 147 |
|
147 | 148 | return compls
|
148 | 149 |
|
| 150 | + def do_is_complete(self, code): |
| 151 | + if self.parse_code(code)["magic"]: |
| 152 | + return {"status": "complete"} |
| 153 | + with TemporaryDirectory() as tmpdir: |
| 154 | + Path(tmpdir, "test_complete.m").write_text(code) |
| 155 | + self._matlab.eval( |
| 156 | + "try, pcode {} -inplace; catch, end".format(tmpdir), |
| 157 | + nargout=0) |
| 158 | + if Path(tmpdir, "test_complete.p").exists(): |
| 159 | + return {"status": "complete"} |
| 160 | + else: |
| 161 | + return {"status": "incomplete"} |
| 162 | + |
149 | 163 | def handle_plot_settings(self):
|
150 | 164 | raw = self.plot_settings
|
151 | 165 | settings = self._validated_plot_settings
|
|
0 commit comments