-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
42 lines (31 loc) · 985 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Main entrypoint of the extension.
"""
import signal
import sys
from pydoc import _start_server, _url_handler # type: ignore
from pydoc_ext.extension import PydocExtension, iter_all_module_names
def shutdown_and_exit(serverthread) -> None:
"""
Gracefully shutdown the server.
"""
if serverthread.serving:
serverthread.stop()
sys.exit(0)
def main() -> None:
"""
Launch the pydoc http server and the extension
"""
# The first module walk is slow - later ones will be much faster
for _ in iter_all_module_names():
pass
# Launch the pydoc HTTP server on random port
serverthread = _start_server(_url_handler, 0)
# Handle SIGINT gracefully
signal.signal(signal.SIGINT, lambda sig, frame: shutdown_and_exit(serverthread))
# Start the extension and wait for it to exit
PydocExtension(serverthread.url).run()
# Shutdown
shutdown_and_exit(serverthread)
if __name__ == "__main__":
main()