forked from Unidata/MetPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc-server.py
executable file
·39 lines (31 loc) · 1.26 KB
/
doc-server.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
#!/usr/bin/env python
# Copyright (c) 2009,2017,2018,2019 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Start a server for hosting the built HTML documentation."""
from functools import partial
import http.server
import pathlib
import posixpath
import socketserver
import sys
PORT = 8000
TEST_FILES_DIR = pathlib.Path('test-server')
class Server(http.server.SimpleHTTPRequestHandler):
"""Server handles serving docs by dynamically remapping to the build directory."""
def translate_path(self, path):
"""Translate a request path to the proper path into the built docs."""
if path == '/MetPy/banner.html':
return str(TEST_FILES_DIR / 'banner.html')
elif path == '/MetPy/versions.json':
return str(TEST_FILES_DIR / 'versions.json')
elif path.startswith('/MetPy/'):
path = posixpath.join('/', *path.split('/')[3:])
return super().translate_path(path)
build_server = partial(Server, directory='build/html')
with socketserver.TCPServer(('', PORT), build_server) as httpd:
try:
print(f'Serving docs at: http://localhost:{PORT}/MetPy/v1.0')
httpd.serve_forever()
except KeyboardInterrupt:
sys.exit(0)