-
Notifications
You must be signed in to change notification settings - Fork 178
/
tools.py
executable file
·110 lines (93 loc) · 3.08 KB
/
tools.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
#
# Copyright (c) 2020 Idiap Research Institute, http://www.idiap.ch/
# Written by Angelos Katharopoulos <[email protected]>
#
"""A script to contain some package administration tools and automations such
as building the documentation.
Maybe this script should be a shell-script, maybe it shouldn't. :-)
"""
import argparse
from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler
import os
from shutil import rmtree
from subprocess import call
import sys
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
def throttled(once_every):
last_time = [0]
def decorator(f):
def decorated(*args, **kwargs):
if time.time() - last_time[0] > once_every:
last_time[0] = time.time()
return f(*args, **kwargs)
return decorated
return decorator
@throttled(3)
def build_docs(args):
# Remove the directory
rmtree(args.output_dir)
call(["mkdocs", "build", "-d", args.output_dir])
call(["pdoc", "--html", "-o", os.path.join(args.output_dir, "api_docs"),
"fast_transformers"])
def serve_docs(args):
class BuildDocsEventHandler(FileSystemEventHandler):
def on_any_event(self, event):
if os.path.splitext(event.src_path)[1] in [".md", ".py"]:
build_docs(args)
build_docs(args)
this_dir = os.path.dirname(os.path.realpath(__file__))
observer = Observer()
observer.schedule(BuildDocsEventHandler(), this_dir, recursive=True)
observer.start()
try:
handler = partial(SimpleHTTPRequestHandler, directory=args.output_dir)
httpd = HTTPServer(args.bind, handler)
httpd.serve_forever()
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Build the documentation site"
)
subparsers = parser.add_subparsers(dest="command")
# Documentation command
docs = subparsers.add_parser(
"build_docs",
help="Build the documentation site"
)
docs.add_argument(
"--output_dir", "-o",
default="site",
help="Choose the output directory to store the html (default: site)"
)
# Serve the documentation (for writing the docs)
serve = subparsers.add_parser(
"serve_docs",
help="Serve the documentation site for development purposes"
)
serve.add_argument(
"--bind", "-b",
type=lambda x: (x.split(":")[0], int(x.split(":")[1])),
default=("", 8000),
help="The address and port to bind the server to (default: :8000)"
)
serve.add_argument(
"--output_dir", "-o",
default="site",
help="Choose the output directory to store the html (default: site)"
)
# Parse the arguments
args = parser.parse_args()
if args.command is None:
parser.print_help()
sys.exit(1)
# Dispatch the command
dict(
build_docs=build_docs,
serve_docs=serve_docs
)[args.command](args)