MCPcopy Index your code
hub / github.com/ipython/ipython / set_hook

Method set_hook

IPython/core/interactiveshell.py:1073–1112  ·  view source on GitHub ↗

set_hook(name,hook) -> sets an internal IPython hook. IPython exposes some of its internal API as user-modifiable hooks. By adding your function to one of these hooks, you can modify IPython's behavior to call at runtime your own routines.

(self, name, hook, priority=50, str_key=None, re_key=None)

Source from the content-addressed store, hash-verified

1071 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
1072
1073 def set_hook(self, name, hook, priority=50, str_key=None, re_key=None):
1074 """set_hook(name,hook) -> sets an internal IPython hook.
1075
1076 IPython exposes some of its internal API as user-modifiable hooks. By
1077 adding your function to one of these hooks, you can modify IPython's
1078 behavior to call at runtime your own routines."""
1079
1080 # At some point in the future, this should validate the hook before it
1081 # accepts it. Probably at least check that the hook takes the number
1082 # of args it's supposed to.
1083
1084 f = types.MethodType(hook,self)
1085
1086 # check if the hook is for strdispatcher first
1087 if str_key is not None:
1088 sdp = self.strdispatchers.get(name, StrDispatch())
1089 sdp.add_s(str_key, f, priority )
1090 self.strdispatchers[name] = sdp
1091 return
1092 if re_key is not None:
1093 sdp = self.strdispatchers.get(name, StrDispatch())
1094 sdp.add_re(re.compile(re_key), f, priority )
1095 self.strdispatchers[name] = sdp
1096 return
1097
1098 dp = getattr(self.hooks, name, None)
1099 if name not in IPython.core.hooks.__all__:
1100 print("Warning! Hook '%s' is not one of %s" % \
1101 (name, IPython.core.hooks.__all__ ))
1102
1103 if not dp:
1104 dp = IPython.core.hooks.CommandChainDispatcher()
1105
1106 try:
1107 dp.add(f,priority)
1108 except AttributeError:
1109 # it was not commandchain, plain old func - replace
1110 dp = f
1111
1112 setattr(self.hooks,name, dp)
1113
1114 #-------------------------------------------------------------------------
1115 # Things related to events

Callers 4

init_hooksMethod · 0.95
init_completerMethod · 0.95
quick_completerFunction · 0.80
install_editorFunction · 0.80

Calls 5

addMethod · 0.95
StrDispatchClass · 0.90
getMethod · 0.80
add_sMethod · 0.80
add_reMethod · 0.80

Tested by

no test coverage detected