Skip to content

Commit 982b3d0

Browse files
committed
add sphinx auto docs
1 parent e6fe5fc commit 982b3d0

File tree

5 files changed

+128
-49
lines changed

5 files changed

+128
-49
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ tb_logger/*
66
wandb/*
77
tmp/*
88

9+
docs/api
10+
scripts/__init__.py
11+
912
*.DS_Store
1013
.idea
1114

docs/api.rst

-45
This file was deleted.

docs/auto_generate_api.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import os
2+
from os import path as osp
3+
4+
5+
def scandir(dir_path, suffix=None, recursive=False, full_path=False):
6+
"""Scan a directory to find the interested files.
7+
8+
Args:
9+
dir_path (str): Path of the directory.
10+
suffix (str | tuple(str), optional): File suffix that we are
11+
interested in. Default: None.
12+
recursive (bool, optional): If set to True, recursively scan the
13+
directory. Default: False.
14+
full_path (bool, optional): If set to True, include the dir_path.
15+
Default: False.
16+
17+
Returns:
18+
A generator for all the interested files with relative paths.
19+
"""
20+
21+
if (suffix is not None) and not isinstance(suffix, (str, tuple)):
22+
raise TypeError('"suffix" must be a string or tuple of strings')
23+
24+
root = dir_path
25+
26+
def _scandir(dir_path, suffix, recursive):
27+
for entry in os.scandir(dir_path):
28+
if not entry.name.startswith('.') and entry.is_file():
29+
if full_path:
30+
return_path = entry.path
31+
else:
32+
return_path = osp.relpath(entry.path, root)
33+
34+
if suffix is None:
35+
yield return_path
36+
elif return_path.endswith(suffix):
37+
yield return_path
38+
else:
39+
if recursive:
40+
yield from _scandir(entry.path, suffix=suffix, recursive=recursive)
41+
else:
42+
continue
43+
44+
return _scandir(dir_path, suffix=suffix, recursive=recursive)
45+
46+
47+
# specifically generate a fake __init__.py for scripts folder to generate docs
48+
with open('../scripts/__init__.py', 'w') as f:
49+
pass
50+
51+
module_name_list = ['basicsr', 'scripts']
52+
for module_name in module_name_list:
53+
cur_dir = osp.abspath(osp.dirname(__file__))
54+
output_dir = osp.join(cur_dir, 'api')
55+
module_dir = osp.join(osp.dirname(cur_dir), module_name)
56+
os.makedirs(output_dir, exist_ok=True)
57+
58+
api_content = f'{module_name} API\n=========================\n'
59+
submodule_name_list = []
60+
61+
for path in sorted(scandir(module_dir, suffix='.py', recursive=True)):
62+
if path in ['__init__.py', 'version.py']:
63+
continue
64+
65+
path = f'{module_name}.' + path.replace('\\', '/').replace('/', '.').replace('.py', '.rst')
66+
67+
# create .rst file
68+
output_rst = osp.join(output_dir, path)
69+
with open(output_rst, 'w') as f:
70+
# write contents
71+
title = path.replace('.rst', '').replace('_', '\\_')
72+
content = f'{title}\n===================================================================================\n'
73+
automodule = path.replace('.rst', '')
74+
content += f'\n.. automodule:: {automodule}'
75+
content += r'''
76+
:members:
77+
:undoc-members:
78+
:show-inheritance:
79+
'''
80+
f.write(content)
81+
82+
# add to api.rst
83+
submodule_name = path.split('.')[1]
84+
if submodule_name not in submodule_name_list:
85+
submodule_name_list.append(submodule_name)
86+
api_content += f'\n\n{module_name}.{submodule_name}\n-----------------------------------------------------'
87+
api_content += r'''
88+
.. toctree::
89+
:maxdepth: 4
90+
91+
'''
92+
93+
api_content += f' {automodule}\n'
94+
95+
# write to api.rst
96+
with open(os.path.join(output_dir, f'api_{module_name}.rst'), 'w') as f:
97+
f.write(api_content)

docs/conf.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
# documentation root, use os.path.abspath to make it absolute, like shown here.
1212
#
1313
import os
14-
# import subprocess
14+
import subprocess
1515
import sys
1616

1717
sys.path.insert(0, os.path.abspath('..'))
1818

1919
# -- Project information -----------------------------------------------------
2020

2121
project = 'BasicSR'
22-
copyright = '2018-2021, BasicSR Authors'
22+
copyright = '2018-2022, BasicSR Authors'
2323
author = 'BasicSR Authors'
2424

2525
# -- General configuration ---------------------------------------------------
@@ -58,3 +58,26 @@
5858

5959
# The master toctree document.
6060
master_doc = 'index'
61+
62+
# def run_apidoc(_):
63+
# # automatically generate api docs
64+
# modules = ['basicsr']
65+
# for module in modules:
66+
# cur_dir = os.path.abspath(os.path.dirname(__file__))
67+
# output_path = os.path.join(cur_dir, 'api')
68+
# cmd_path = 'sphinx-apidoc'
69+
# if hasattr(sys, 'real_prefix'): # Check to see if we are in a virtualenv
70+
# # If we are, assemble the path manually
71+
# cmd_path = os.path.abspath(os.path.join(sys.prefix, 'bin', 'sphinx-apidoc'))
72+
# subprocess.check_call([cmd_path, '-e', '-o', output_path, '../' + module, '--force'])
73+
74+
# def setup(app):
75+
# app.connect('builder-inited', run_apidoc)
76+
77+
78+
def auto_generate_api(app):
79+
subprocess.run(['python', './auto_generate_api.py'])
80+
81+
82+
def setup(app):
83+
app.connect('builder-inited', auto_generate_api)

docs/index.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ Welcome to BasicSR's documentation!
22
===================================
33

44
.. toctree::
5-
:maxdepth: 3
5+
:maxdepth: 4
66
:caption: API
77

8-
api.rst
8+
api/api_basicsr.rst
9+
api/api_scripts.rst
910

1011
Indices and tables
1112
==================

0 commit comments

Comments
 (0)