|
| 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) |
0 commit comments