Skip to content

Instantly share code, notes, and snippets.

@LeonarddeR
Last active October 21, 2021 09:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeonarddeR/82564a57a1a79a36fad79c1b92771c29 to your computer and use it in GitHub Desktop.
Save LeonarddeR/82564a57a1a79a36fad79c1b92771c29 to your computer and use it in GitHub Desktop.
Convert NVDA code base from epydoc to sphinx
import re
import os
from collections import defaultdict
import sys
curFile = os.path.abspath(__file__)
rePatterns = {
# Adds an empty line in a doc string before param definitions.
# Shouldn't touch doc strings that only contain param info.
re.compile("@type", re.IGNORECASE): ":type",
re.compile("@param", re.IGNORECASE): ":param",
re.compile(r'(:\s*r?"""(?:.(?!"""|@))*?)(?!""")(?=\n(\t*)\@)', re.DOTALL | re.IGNORECASE): "\\1\n",
re.compile("@return(s?)", re.IGNORECASE): ":returns",
re.compile("@rtype", re.IGNORECASE): ":rtype",
re.compile("@raise(s?)", re.IGNORECASE): ":raises",
re.compile("L{(.+?)}", re.IGNORECASE): "`\\1`",
re.compile("C{(.+?)}", re.IGNORECASE): "``\\1``",
re.compile("@(i|c)??var", re.IGNORECASE): ":var",
re.compile(r"(\t?)@(\w+?:)", re.IGNORECASE): "\\1#9840: Manual intervention required\n\\1.. \\2",
}
counts = defaultdict(int)
def processFile(file):
if file == curFile:
return
print(f"Processing {file}")
contents = None
with open(file, encoding="utf_8") as f:
contents = f.read()
for pattern, repl in rePatterns.items():
contents, replacements = pattern.subn(repl, contents)
if replacements:
print(f"{pattern.pattern} > {repl}: {replacements} items replaced")
counts[pattern.pattern] += replacements
with open(file, "w", encoding="utf_8") as f:
f.write(contents)
print(f"Finnished rocessing {file}")
def printSummary():
for pattern, count in counts.items():
print(f"Overall changes for pattern {pattern}: {count}")
def main(argv):
if len(argv) != 2:
raise RuntimeError("Invalid arguments")
path = os.path.abspath(argv[1])
for curDir,subDirs,files in os.walk(path):
for f in files:
if not f.endswith(".py"):
continue
# Python file
filePath = os.path.join(curDir, f)
processFile(filePath)
printSummary()
if __name__ == "__main__":
main(sys.argv)
@LeonarddeR
Copy link
Author

@feerrenrut and @seanbudd, could you please have a look at this? What would be the best way to review this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment