|
| 1 | +# Copyright 2024 Hathor Labs |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from logging import getLogger |
| 16 | +from typing import TYPE_CHECKING, Any, Optional |
| 17 | + |
| 18 | +from ipykernel.kernelapp import IPKernelApp as OriginalIPKernelApp |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from hathor.manager import HathorManager |
| 22 | + |
| 23 | + |
| 24 | +class IPKernelApp(OriginalIPKernelApp): |
| 25 | + def __init__(self, runtime_dir: Optional[str] = None): |
| 26 | + super().__init__() |
| 27 | + # https://traitlets.readthedocs.io/en/stable/config-api.html#traitlets.config.Application.logging_config |
| 28 | + self.logging_config: dict[str, Any] = {} # empty out logging config |
| 29 | + # https://traitlets.readthedocs.io/en/stable/config-api.html#traitlets.config.LoggingConfigurable.log |
| 30 | + self.log = getLogger('hathor.ipykernel') # use custom name for the logging adapter |
| 31 | + if runtime_dir is not None: |
| 32 | + # https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.connection_dir |
| 33 | + # https://github.com/ipython/ipykernel/blob/main/ipykernel/kernelapp.py#L301-L320 |
| 34 | + # if not defined now, when init_connection_file is called it will be set to 'kernel-<PID>.json', it is |
| 35 | + # defined now because it's more convinient to have a fixed path that doesn't depend on the PID of the |
| 36 | + # running process, which doesn't benefit us anyway since the data dir |
| 37 | + self.connection_dir = runtime_dir |
| 38 | + self.connection_file = 'kernel.json' |
| 39 | + # https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.no_stderr |
| 40 | + self.no_stderr = True # disable forwarding of stderr (because we use it for logging) |
| 41 | + |
| 42 | + # https://traitlets.readthedocs.io/en/stable/config-api.html#traitlets.config.Application.get_default_logging_config |
| 43 | + def get_default_logging_config(self) -> dict[str, Any]: |
| 44 | + # XXX: disable original logging setup |
| 45 | + return {"version": 1, "disable_existing_loggers": False} |
| 46 | + |
| 47 | + # https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.init_signal |
| 48 | + def init_signal(self) -> None: |
| 49 | + # XXX: ignore registering of signals |
| 50 | + pass |
| 51 | + |
| 52 | + # https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.log_connection_info |
| 53 | + def log_connection_info(self) -> None: |
| 54 | + # XXX: this method is only used to log this info, we can customize it freely |
| 55 | + self.log.info(f'ipykernel connection enabled at {self.abs_connection_file}') |
| 56 | + |
| 57 | + # https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.configure_tornado_logger |
| 58 | + def configure_tornado_logger(self) -> None: |
| 59 | + # XXX: we already setup tornago logging on hathor.cli.util.setup_logging prevent this class from overriding it |
| 60 | + pass |
| 61 | + |
| 62 | + # https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.kernelapp.IPKernelApp.start |
| 63 | + def start(self): |
| 64 | + # XXX: custom start to prevent it from running an event loop and capturing KeyboardInterrupt |
| 65 | + self.kernel.start() |
| 66 | + |
| 67 | + |
| 68 | +# https://ipykernel.readthedocs.io/en/stable/api/ipykernel.html#ipykernel.embed.embed_kernel |
| 69 | +def embed_kernel(manager: 'HathorManager', *, |
| 70 | + runtime_dir: Optional[str] = None, extra_ns: dict[str, Any] = {}) -> None: |
| 71 | + """ Customized version of ipykernel.embed.embed_kernel that takes parameters specific to this project. |
| 72 | +
|
| 73 | + In theory this method could be called multiple times, like the original ipykernel.embed.embed_kernel. |
| 74 | + """ |
| 75 | + # get the app if it exists, or set it up if it doesn't |
| 76 | + if IPKernelApp.initialized(): |
| 77 | + app = IPKernelApp.instance() |
| 78 | + else: |
| 79 | + app = IPKernelApp.instance(runtime_dir=runtime_dir) |
| 80 | + app.initialize([]) |
| 81 | + app.kernel.user_ns = dict(manager=manager) | extra_ns |
| 82 | + app.shell.set_completer_frame() |
| 83 | + app.start() |
0 commit comments