-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #544 from Farama-Foundation/python_docstrings
Python docstrings
- Loading branch information
Showing
6 changed files
with
666 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import re | ||
|
||
|
||
FILES_TO_PARSE = [ | ||
{"filepath": "doc/DoomGame.md", "namespace": "DoomGamePython"}, | ||
{"filepath": "doc/Utilities.md"}, | ||
] | ||
OUTPUT_FILE = "src/lib_python/ViZDoomDocstrings.h" | ||
RAW_STRING_ESCAPE_SEQ = "DOCSTRING" | ||
FUNCTION_HEADER_REGEX = r"^### *<a *name=\"([a-zA-Z]+)\"><\/a> *`([a-zA-Z]+)` *$" | ||
LINES_TO_IGNORE_REGEXES = [ | ||
r"---", # Lines | ||
r"^\|.+\|$", # Tables | ||
r"^See also:.*$", # See also | ||
r"- \[.*\]\(.*\)", # List of links starting with - | ||
r"\* \[.*\]\(.*\)", # List of links starting with * | ||
r"^Added in .*$", # Added in annotations | ||
r"^Changed in .*$", # Changed in annotations | ||
r"^Deprecated since .*$", # Deprecated since annotations | ||
r"^Removed in .*$", # Removed in annotations | ||
r"^Config key: .*$", # Config annotations | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
with open(OUTPUT_FILE, "w") as output_file: | ||
output_file.write( | ||
""" | ||
/* | ||
This file is autogenerated by scripts/create_python_docstrings_from_docs.py | ||
Do not edit it manually. Edit the Markdown files instead and regenerate it. | ||
*/ | ||
#ifndef __VIZDOOM_DOCSTRINGS_H__ | ||
#define __VIZDOOM_DOCSTRINGS_H__ | ||
namespace vizdoom {{ | ||
namespace docstrings {{ | ||
""" | ||
) | ||
|
||
for file in FILES_TO_PARSE: | ||
if "namespace" in file: | ||
output_file.write(f"namespace {file['namespace']} {{\n\n") | ||
|
||
with open(file["filepath"]) as f: | ||
lines = f.readlines() | ||
|
||
started = False | ||
next_docstring = "" | ||
for line in lines: | ||
# If lines match pattern, extract the function name and arguments | ||
match = re.match(FUNCTION_HEADER_REGEX, line) | ||
if match: | ||
if started: | ||
next_docstring = next_docstring.strip() | ||
next_docstring += f'){RAW_STRING_ESCAPE_SEQ}";\n\n' | ||
output_file.write(next_docstring) | ||
|
||
next_docstring = "" | ||
function_name = match.group(1) | ||
output_file.write( | ||
f' const char *{function_name} = R"{RAW_STRING_ESCAPE_SEQ}(' | ||
) | ||
started = True | ||
|
||
elif started: | ||
# Filter out some lines | ||
if not any(re.match(r, line) for r in LINES_TO_IGNORE_REGEXES): | ||
next_docstring += line | ||
|
||
if started: | ||
output_file.write(f'){RAW_STRING_ESCAPE_SEQ}";\n\n') | ||
|
||
if "namespace" in file: | ||
output_file.write(f"}} // namespace {file['namespace']}\n\n") | ||
|
||
output_file.write( | ||
""" | ||
} // namespace docstrings | ||
} // namespace vizdoom | ||
#endif | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.