1"""Generate the reST-files for the API documentation.
2
3sphinx-apidoc is not customizeable enough to do this.
4"""
5import os
6import pkgutil
7from os import path
8
9import fava
10
11MODULES = list(pkgutil.walk_packages(fava.__path__, fava.__name__ + "."))
12
13RST_PATH = path.join(path.dirname(__file__), "api")
14if not path.isdir(RST_PATH):
15    os.mkdir(RST_PATH)
16
17
18def heading(name, level="-"):
19    """Return the rst-heading for the given heading."""
20    return f"{name}\n{level * len(name)}\n\n"
21
22
23for package in ["fava"] + [mod.name for mod in MODULES if mod.ispkg]:
24    submodules = [
25        mod.name
26        for mod in MODULES
27        if mod.name.startswith(package)
28        and not mod.ispkg
29        and "_test" not in mod.name
30        and mod.name.count(".") == package.count(".") + 1
31    ]
32    with open(path.join(RST_PATH, f"{package}.rst"), "w") as rst:
33        rst.write(heading(package, "="))
34        rst.write(f".. automodule:: {package}\n\n")
35        for submod in submodules:
36            rst.write(heading(submod, "-"))
37            rst.write(f".. automodule:: {submod}\n\n")
38