Skip to content

Commit 3a75691

Browse files
author
OutlyingWest
committed
is_magic_code() method added to repair logic of kernel
1 parent 0535bf4 commit 3a75691

4 files changed

Lines changed: 67 additions & 20 deletions

File tree

src/jumper/context.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
from dataclasses import dataclass
2+
from enum import Enum
23

34
from jumper.perfdatahandler import PerformanceDataHandler
45

56

7+
# kernel modes
8+
class KernelMode(Enum):
9+
DEFAULT = (0, "default")
10+
MULTICELL = (1, "multicell")
11+
WRITEFILE = (2, "writefile")
12+
13+
def __str__(self):
14+
return self.value[1]
15+
16+
617
@dataclass
718
class KernelContext:
819
# will be set to True as soon as GPU data is received

src/jumper/kernel.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
from enum import Enum
1111
from textwrap import dedent
1212
from statistics import mean
13+
from typing import Type
14+
1315
import pandas as pd
16+
from IPython.core.magic import Magics
1417
from ipykernel.ipkernel import IPythonKernel
1518
from itables import show
1619

17-
from jumper.context import kernel_context
20+
from jumper.context import kernel_context, KernelMode
1821
from jumper.userpersistence import PersHelper, scorep_script_name
1922
from jumper.userpersistence import magics_cleanup
2023
import importlib
21-
from jumper.perfdatahandler import PerformanceDataHandler
22-
import jumper.visualization as perfvis
24+
from magic_extension.magic import KernelMagics
2325

2426
# import jumper.multinode_monitor.slurm_monitor as slurm_monitor
2527

@@ -30,16 +32,6 @@
3032
subprocess_dump = "subprocess_dump.pkl"
3133

3234

33-
# kernel modes
34-
class KernelMode(Enum):
35-
DEFAULT = (0, "default")
36-
MULTICELL = (1, "multicell")
37-
WRITEFILE = (2, "writefile")
38-
39-
def __str__(self):
40-
return self.value[1]
41-
42-
4335
class JumperKernel(IPythonKernel):
4436
implementation = "Python and Score-P"
4537
implementation_version = "1.0"
@@ -953,8 +945,16 @@ async def do_execute(
953945
self.get_perfdata_index(-1, metrics[1:]), nmetrics)
954946
return self.standard_reply()
955947
"""
956-
957-
if code.startswith("%%display_code_for_index"):
948+
if self.is_magic_code(code, KernelMagics):
949+
return await super().do_execute(
950+
code,
951+
silent,
952+
store_history,
953+
user_expressions,
954+
allow_stdin,
955+
cell_id=cell_id,
956+
)
957+
elif code.startswith("%%display_code_for_index"):
958958
if len(code.split(" ")) == 1:
959959
self.cell_output(
960960
"No index specified. Use: %%display_code_for_index index",
@@ -1179,6 +1179,44 @@ async def do_execute(
11791179
explicit_scorep=False,
11801180
)
11811181

1182+
def is_magic_code(
1183+
self,
1184+
code: str,
1185+
magic_class: Type[Magics]
1186+
) -> bool:
1187+
"""
1188+
Determines whether the given code starts with a cell or line magic
1189+
defined in the specified magic class.
1190+
1191+
Args:
1192+
code (str): The source code of the cell.
1193+
magic_class (Type[Magics]): The magic class to filter against (e.g. JumperMagics).
1194+
1195+
Returns:
1196+
bool: True if the code starts with a magic defined in the given class, False otherwise.
1197+
"""
1198+
code = code.strip()
1199+
if not code:
1200+
return False
1201+
1202+
first_token = code.split(maxsplit=1)[0]
1203+
1204+
mm = self.shell.magics_manager
1205+
target_magics = set()
1206+
1207+
# Collect magic names, which are registered in "magic_class"
1208+
for magic_type in ('cell', 'line'):
1209+
for name, func in mm.magics[magic_type].items():
1210+
if getattr(func, '__self__', None).__class__ == magic_class:
1211+
target_magics.add((magic_type, name))
1212+
1213+
if first_token.startswith("%%"):
1214+
return ('cell', first_token[2:]) in target_magics
1215+
elif first_token.startswith("%"):
1216+
return ('line', first_token[1:]) in target_magics
1217+
1218+
return False
1219+
11821220
def do_shutdown(self, restart):
11831221
self.pershelper.postprocess()
11841222
return super().do_shutdown(restart)

src/magic_extension/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""An example magic"""
22
__version__ = '0.0.1'
33

4-
from .magic import KernelMagics
4+
from magic_extension.magic import KernelMagics
5+
56

67
def load_ipython_extension(ipython):
78
print("magic_extension loaded!")

src/magic_extension/magic.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import sys
22

33
from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic)
4-
from IPython.display import display, Markdown
54

6-
from jumper.context import kernel_context
7-
from jumper.kernel import KernelMode
5+
from jumper.context import kernel_context, KernelMode
86

9-
from jumper.perfdatahandler import PerformanceDataHandler
107
import jumper.visualization as perfvis
118

129

0 commit comments

Comments
 (0)