浏览代码
Added local precommit hook to autogenerate markdown documentation using pydoc-markdown
/develop/api-documentation-update-some-fixes
Added local precommit hook to autogenerate markdown documentation using pydoc-markdown
/develop/api-documentation-update-some-fixes
Miguel Alonso Jr
4 年前
当前提交
11984db2
共有 4 个文件被更改,包括 132 次插入 和 30 次删除
-
5.pre-commit-config.yaml
-
57docs/Python-API-Documentation.md
-
25ml-agents-envs/pydoc-config.yaml
-
75utils/generate_markdown_docs.py
|
|||
# config to specify which modules will be used to render api docs |
|||
folder: docs |
|||
modules: |
|||
- name: mlagents_envs |
|||
file_name: Python-API-Documentation.md |
|||
submodules: |
|||
- env_utils |
|||
- base_env |
|||
- communicator |
|||
- environment |
|||
- registry |
|||
- registry.unity_env_registry |
|||
- registry.binary_utils |
|||
- registry.remote_registry_entry |
|||
- registry.base_registry_entry |
|||
- side_channel |
|||
- side_channel.raw_bytes_channel |
|||
- side_channel.outgoing_message |
|||
- side_channel.engine_configuration_channel |
|||
- side_channel.side_channel_manager |
|||
- side_channel.stats_side_channel |
|||
- side_channel.incoming_message |
|||
- side_channel.float_properties_channel |
|||
- side_channel.environment_parameters_channel |
|||
- side_channel.side_channel |
|
|||
#!/usr/bin/env python3 |
|||
import os |
|||
import subprocess |
|||
import sys |
|||
import yaml |
|||
import argparse |
|||
import hashlib |
|||
|
|||
|
|||
# pydoc-markdown -I . -m module_name --render_toc > doc.md |
|||
|
|||
|
|||
def hash_file(filename): |
|||
if os.path.exists(filename): |
|||
hasher = hashlib.md5() |
|||
with open(filename, "rb") as file_to_hash: |
|||
buffer = file_to_hash.read() |
|||
hasher.update(buffer) |
|||
return hasher.hexdigest() |
|||
else: |
|||
return 0 |
|||
|
|||
|
|||
def remove_trailing_whitespace(filename): |
|||
nchanged = 0 |
|||
with open(filename, "rb") as f: |
|||
code1 = f.read().decode() |
|||
lines = [line.rstrip() for line in code1.splitlines()] |
|||
while lines and not lines[-1]: |
|||
lines.pop(-1) |
|||
lines.append("") # always end with a newline |
|||
code2 = "\n".join(lines) |
|||
if code1 != code2: |
|||
nchanged += 1 |
|||
with open(filename, "wb") as f: |
|||
f.write(code2.encode()) |
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
parser = argparse.ArgumentParser() |
|||
parser.add_argument("--package_dirs", nargs="+") |
|||
args = parser.parse_args() |
|||
|
|||
ok = False |
|||
return_code = 0 |
|||
for package_dir in args.package_dirs: |
|||
config_path = os.path.join(os.getcwd(), package_dir, "pydoc-config.yaml") |
|||
print(config_path) |
|||
with open(config_path) as config_file: |
|||
config = yaml.full_load(config_file) |
|||
for module in config["modules"]: |
|||
module_name = module["name"] |
|||
submodules = module["submodules"] |
|||
output_file_name = f"./{config['folder']}/{module['file_name']}" |
|||
old_hash = hash_file(output_file_name) |
|||
module_args = [] |
|||
for submodule in submodules: |
|||
module_args.append("-m") |
|||
module_args.append(f"{module_name}.{submodule}") |
|||
with open(output_file_name, "w") as output_file: |
|||
subprocess_args = [ |
|||
"pydoc-markdown", |
|||
"-I", |
|||
f"./{package_dir}", |
|||
*module_args, |
|||
"--render-toc", |
|||
] |
|||
subprocess.check_call(subprocess_args, stdout=output_file) |
|||
remove_trailing_whitespace(output_file_name) |
|||
new_hash = hash_file(output_file_name) |
|||
ok = old_hash == new_hash |
|||
|
|||
return_code = 0 if ok else 1 |
|||
|
|||
sys.exit(return_code) |
撰写
预览
正在加载...
取消
保存
Reference in new issue