File: build_toc.py

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (72 lines) | stat: -rwxr-xr-x 1,379 bytes parent folder | download
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
#!/usr/bin/env python3

import glob
import os
import pathlib
import yaml

RST_DIR = pathlib.Path().joinpath("rst").absolute()


def check_usage(names):
    # check if each toc item has a corresponding rst file
    for name in names:
        if not os.path.exists(f"{RST_DIR}/{name}.rst"):
            print(f"toc item={name}: no rst file found!")

    # check if rest file has a corresponding toc item
    for fname in glob.glob(f"{RST_DIR}/*.rst"):
        name = os.path.basename(fname)
        if name[0].isupper():
            name, _, _ = name.rpartition(".")
            if name not in names:
                print(f"file={fname}: no toc item found!")


def build_toc():
    title = "Python API"
    t = f"""
.. _python_api:

{title}
{"*" * len(title)}

"""
    names = set()
    with open("categories.yaml", "r") as f:
        conf = yaml.load(f, Loader=yaml.FullLoader)
        for category in conf:
            title = category["title"]
            t += f"""
{title}
{"=" * len(title)}
"""
            for name in category["items"]:
                t += f"""    - :py:class:`ecflow.{name}`\n"""
                names.add(name)

        t += """
Api
===

.. toctree::
   :maxdepth: 1
   :glob:

   *

  
.. contents::
   :depth: 2
   :local:
   :backlinks: top
    

"""
    with open("python_api.rst", "w") as f:
        f.write(t)

    check_usage(names)


build_toc()