Coverage for /home/tbone/.local/share/hatch/env/virtual/importnb-aVRh-lqt/test.stdlib/lib/python3.9/site-packages/importnb/ipython_extension.py: 36%

1# coding: utf-8

2"""# `importnb` Jupyter magic extensions

3"""

4

5""" %importnb --stdout --stderr --display --shell

6"""

7

8 import argparse

9 import inspect

10 from functools import partial

11 from importlib import import_module

12

13 import pkg_resources

14

15

16 def get_module_object(str):

17 module, object = str.split(":", 1)

18 return getattr(import_module(module), object)

19

20

21 parser = argparse.ArgumentParser(

22 description="""Define the importnb loader properties."""

23)

24 parser.add_argument("--cls", type=get_module_object, default="importnb:Notebook")

25 parser.add_argument("--fuzzy", action="store_true")

26

27 try:

28 from IPython.core import magic_arguments

29 from IPython.core.magic import (

30 Magics,

31 magics_class,

32 line_magic,

33 cell_magic,

34 line_cell_magic,

35 )

36

37 __IPYTHON__ = True

38 except ImportError:

39 __IPYTHON__ = False

40

41

42 class ImportNbExtensionBase:

43 loaders = None

44

45 def __init__(self, shell, loader=None):

46 self.loaders = []

47 # A default loader to install

48 if loader:

49 self.loaders.append(loader(position=-1).__enter__())

50

51

52 if __IPYTHON__:

53

54 @magics_class

55 class ImportNbExtension(Magics, ImportNbExtensionBase):

56 loaders = None

57

58 def __init__(self, shell, loader=None):

59 Magics.__init__(self, shell)

60 ImportNbExtensionBase.__init__(self, shell, loader)

61

62 @line_cell_magic

63 def importnb(self, line, cell=None):

64 if line.strip() == "pop":

65 return self.pop()

66 module.__package__

67

68 details = vars(parser.parse_args(line.split()))

69 self.loaders.append(details.pop("cls")(**details))

70

71 if cell is None:

72 self.loaders[-1].__enter__()

73 return

74

75 with self.loaders.pop(-1):

76 self.parent.run_cell(cell)

77

78 def unload(self):

79 while self.loaders:

80 self.pop()

81

82 def pop(self):

83 self.loaders.pop().__exit__(None, None, None)

84

85

86 else:

87

88 class ImportNbExtension(ImportNbExtensionBase):

89 ...

90

91

92 manager = None

93

94

95 def IPYTHON_MAIN():

96 """Decide if the Ipython command line is running code."""

97 import pkg_resources

98

99 runner_frame = inspect.getouterframes(inspect.currentframe())[-2]

100 return (

101 getattr(runner_frame, "function", None)

102 == pkg_resources.load_entry_point(

103 "ipython", "console_scripts", "ipython"

104 ).__name__

105 )

106

107

108 def load_ipython_extension(ip=None):

109 global manager, module

110 if IPYTHON_MAIN():

111 from .parameterize import Parameterize as Notebook

112 else:

113 from .loader import Notebook

114

115 # Auto loading only works in IPython and

116 # we only read need it when there are parameters.

117 manager = ImportNbExtension(ip, Notebook)

118

119 if ip:

120 ip.register_magics(manager)

121 ip.user_ns.update(__path__=[str(__import__("pathlib").Path().absolute())])

122 from .completer import load_ipython_extension

123

124 load_ipython_extension(ip)

125

126 ip.user_ns["reload"] = __import__("importlib").reload

127

128

129 def unload_ipython_extension(ip=None):

130 global manager

131 ip and manager and manager.unload()