|
1 | 1 | from pathlib import Path
|
2 |
| -from typing import Any, Dict, Optional |
| 2 | +from typing import Any, Dict, List, Optional |
3 | 3 |
|
4 | 4 | import nbformat
|
5 | 5 | from nbclient.client import (
|
@@ -79,20 +79,34 @@ async def apply_mocks(
|
79 | 79 | if execute_reply["content"]["ename"] == "ModuleNotFoundError":
|
80 | 80 | if self.find_import_errors:
|
81 | 81 | raise CellImportError()
|
82 |
| - |
83 |
| - if c.kc is None: |
84 |
| - raise Exception("there is no kernelclient") |
85 |
| - mocks: Dict[str, Any] = ( |
86 |
| - cell.get("metadata", {}).get("nbmake", {}).get("mock", {}) |
87 |
| - ) |
88 |
| - for v in mocks: |
89 |
| - if isinstance(mocks[v], str): |
90 |
| - out = await c.kc.execute_interactive(f"{v} = '{mocks[v]}'") |
91 |
| - else: |
92 |
| - out = await c.kc.execute_interactive(f"{v} = {mocks[v]}") |
93 |
| - |
94 |
| - if out["content"]["status"] != "ok": |
95 |
| - raise Exception(f"Failed to apply mock {v}\n\n{str(out)}") |
| 82 | + else: |
| 83 | + if c.kc is None: |
| 84 | + raise Exception("there is no kernelclient") |
| 85 | + mocks: Dict[str, Any] = ( |
| 86 | + cell.get("metadata", {}).get("nbmake", {}).get("mock", {}) |
| 87 | + ) |
| 88 | + for v in mocks: |
| 89 | + if isinstance(mocks[v], str): |
| 90 | + out = await c.kc.execute_interactive(f"{v} = '{mocks[v]}'") |
| 91 | + else: |
| 92 | + out = await c.kc.execute_interactive(f"{v} = {mocks[v]}") |
| 93 | + |
| 94 | + if out["content"]["status"] != "ok": |
| 95 | + raise Exception(f"Failed to apply mock {v}\n\n{str(out)}") |
| 96 | + |
| 97 | + post_cell_execute: List[str] = ( |
| 98 | + cell.get("metadata", {}) |
| 99 | + .get("nbmake", {}) |
| 100 | + .get("post_cell_execute", []) |
| 101 | + ) |
| 102 | + if post_cell_execute: |
| 103 | + pce = "\n".join(post_cell_execute) |
| 104 | + out = await c.kc.execute_interactive(pce) |
| 105 | + |
| 106 | + if out["content"]["status"] != "ok": |
| 107 | + raise Exception( |
| 108 | + f"Post cell execution failed:\n{pce}\n\n{str(out)}" |
| 109 | + ) |
96 | 110 |
|
97 | 111 | c.on_cell_executed = apply_mocks
|
98 | 112 |
|
|
0 commit comments