Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/py/generate_api_module_listing.py
4503 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
19
"""This script recursively scans the `selenium` package directory and generates an API listing.
20
21
Recursively scans the `selenium` package directory to find all modules,
22
then generates the `py/docs/source/api.rst` file containing a listing of all
23
modules in separate sections. The `api.rst` file is later used by
24
`sphinx-autogen` to generate sphinx autodoc stub pages used in the Python API
25
documentation. See `py/tox.ini` for how it is invoked.
26
"""
27
28
import os
29
import site
30
31
32
def find_modules(package_name):
33
modules = []
34
for dirpath, _, filenames in os.walk(package_name):
35
for filename in filenames:
36
if filename.endswith(".py") and not filename.startswith("__"):
37
module_name = (
38
os.path.join(dirpath, filename)
39
.removeprefix(site.getsitepackages()[-1])
40
.removeprefix(os.sep)
41
.removesuffix(".py")
42
.replace(os.sep, ".")
43
)
44
modules.append(module_name)
45
return sorted(set(modules))
46
47
48
if __name__ == "__main__":
49
# Support running via bazel run (uses BUILD_WORKSPACE_DIRECTORY)
50
workspace = os.environ.get("BUILD_WORKSPACE_DIRECTORY", "")
51
base_dir = os.path.join(workspace, "py") if workspace else "."
52
os.chdir(base_dir)
53
54
package_name = "selenium"
55
output_file = os.path.join("docs", "source", "api.rst")
56
print(f"generating module list for sphinx autodoc in: {output_file}\n")
57
modules = [module for module in find_modules(package_name) if ".devtools." not in module]
58
base_modules = [mod for mod in sorted({module.rsplit(".", 1)[0] for module in modules}) if mod != package_name]
59
print("found sections:")
60
for base_module in base_modules:
61
print(f" {base_module}")
62
with open(output_file, "w") as f:
63
f.write(
64
"""\
65
..
66
this file was auto-generated by `generate_api_module_listing.py`
67
DO NOT EDIT
68
69
:orphan:
70
71
======================
72
Selenium Documentation
73
======================
74
"""
75
)
76
for base_module in base_modules:
77
content_section = base_module.split(".", 1)[1]
78
separator = "-" * len(content_section)
79
f.write(
80
f"""
81
{content_section}
82
{separator}
83
84
.. currentmodule:: {base_module}
85
.. autosummary::
86
:toctree: {base_module.replace(".", "_")}
87
88
"""
89
)
90
for module in modules:
91
if base_module in module:
92
if len(module.split(".")) - len(base_module.split(".")) == 1:
93
f.write(f" {module}\n")
94
f.write(
95
"""
96
Indices and tables
97
98
* :ref:`genindex`
99
* :ref:`modindex`
100
* :ref:`search`
101
"""
102
)
103
104