File: doc-server.py

package info (click to toggle)
metpy 1.7.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,584 kB
  • sloc: python: 41,853; makefile: 111; javascript: 57
file content (39 lines) | stat: -rwxr-xr-x 1,298 bytes parent folder | download | duplicates (2)
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/pst-versions.json':
            return str(TEST_FILES_DIR / 'pst-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/dev')
        httpd.serve_forever()
    except KeyboardInterrupt:
        sys.exit(0)