|
| 1 | +import sys |
| 2 | + |
| 3 | +from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic) |
| 4 | +from IPython.display import display, Markdown |
| 5 | + |
| 6 | +from jumper.context import kernel_context |
| 7 | +from jumper.kernel import KernelMode |
| 8 | + |
| 9 | +from jumper.perfdatahandler import PerformanceDataHandler |
| 10 | + |
| 11 | + |
| 12 | +@magics_class |
| 13 | +class KernelMagics(Magics): |
| 14 | + def __init__(self, shell): |
| 15 | + super(KernelMagics, self).__init__(shell) |
| 16 | + self.perfdata_handler = PerformanceDataHandler() |
| 17 | + self.mode = KernelMode.DEFAULT |
| 18 | + |
| 19 | + @cell_magic |
| 20 | + def abra(self, line, cell): |
| 21 | + print('HELLO!\n', cell) |
| 22 | + |
| 23 | + @cell_magic |
| 24 | + def set_perfmonitor(self, line, code): |
| 25 | + """ |
| 26 | + Read the perfmonitor and try to select it. |
| 27 | + """ |
| 28 | + if self.mode == KernelMode.DEFAULT: |
| 29 | + monitor = code.split("\n")[1] |
| 30 | + if monitor in {"local", "localhost", "LOCAL", "LOCALHOST"}: |
| 31 | + self.cell_output( |
| 32 | + "Selected local monitor. No parallel monitoring." |
| 33 | + ) |
| 34 | + else: |
| 35 | + try: |
| 36 | + self.perfdata_handler.set_monitor(monitor) |
| 37 | + kernel_context.nodelist = self.perfdata_handler.get_nodelist() |
| 38 | + if len(kernel_context.nodelist) <= 1: |
| 39 | + kernel_context.nodelist = None |
| 40 | + self.cell_output( |
| 41 | + "Found monitor: " |
| 42 | + + str(monitor) |
| 43 | + + " but no nodelist, using local setup. " |
| 44 | + ) |
| 45 | + else: |
| 46 | + self.cell_output( |
| 47 | + "Selected monitor: " |
| 48 | + + str(monitor) |
| 49 | + + " and got nodes: " |
| 50 | + + str(kernel_context.nodelist) |
| 51 | + ) |
| 52 | + except Exception as e: |
| 53 | + self.cell_output(f"Error setting monitor\n{e}", "stderr") |
| 54 | + else: |
| 55 | + self.cell_output( |
| 56 | + f"KernelWarning: Currently in {self.mode}, command ignored.", |
| 57 | + "stderr", |
| 58 | + ) |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def cell_output(string, stream="stdout"): |
| 62 | + if stream == "stderr": |
| 63 | + print(string, file=sys.stderr) |
| 64 | + else: |
| 65 | + print(string) |
0 commit comments