Skip to content

Instantly share code, notes, and snippets.

@jghibiki
Created February 21, 2014 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jghibiki/9128470 to your computer and use it in GitHub Desktop.
Save jghibiki/9128470 to your computer and use it in GitHub Desktop.
This script will attempt to search through all Renpy classes, attempting to find docstrings with the :odoc: and a keyword corresponding to one of those listed in the keywords list. If a docstring containing both those things is found, we will parse the docstring for :type: and :name: doc strings as well. If the string following :type: is the str…
label buildDocStrings:
python:
#configuration values
keywords = ["logger"] #the list of keywords that can follow an :odoc: tag
fileHeader = ".. Automatically generated file - do not modify." #this file header should be added to each file at the top.
incPath = _config.gamedir + "/../sphinx/source/inc/" ## this should be changed to point to a dir called inc in your sphinx source dir
#The guts are below
import inspect
import re
docLog = Logger("docLog.log")
g = {}
g.update(globals())
addToDoc = []
#Parse for docstrings by seacrching methods and classes defined in globals
#if a class has methods with docstrings, we'll pull those in as well by using getattr()
for key in g:
if (g[key].__doc__) != None:
for line in (g[key].__doc__).split("\n"):
if ":root:" in line:
if (line.strip())[6:] in keywords:
addToDoc.append(g[key].__doc__)
if inspect.isclass(g[key]):
for method in dir(g[key]):
myMethod = getattr(g[key], method)
if(myMethod.__doc__) != None:
for mLine in (myMethod.__doc__).split("\n"):
if ":root:" in mLine:
if (mLine.strip())[6:] in keywords:
addToDoc.append(myMethod.__doc__)
addToDoc = list(set(addToDoc)) #removes duplicates
#now we'll work on parsing out info from the docstring and sending it to the inc dir as a file
for item in d_addToDoc:
doc=""
name=""
itemType=""
docString=""
first = True
indent = 0
#first find what doc to use
lineList = reduce(lambda acc, elem: acc[:-1] + [acc[-1] + elem] if elem == "\n" else acc + [elem], re.split("(%s)" % re.escape("\n"), item), [])
for line in lineList:
if ":root:" in line:
doc = line.strip()[5:]
if ":type:" in line:
itemType = line.strip()[6:]
if ":name:" in line:
name = line.strip()[6:]
#now try get other lines and add them to the docString
if ":root:" not in line:
if ":type:" not in line:
if ":name:" not in line:
if first and not line.isspace():
indent = len(line) - len(line.lstrip())
first = False
docString += line[indent:]
if itemType == "class" or type == "function":
docFile = open(incPath + doc, "w")
elif itemType == "method":
docFile = open(incPath + doc + "." + name, "w")
else:
postError("Item type is not a class, function, or method!\n itemType: " + itemType)
docFile.write(fileHeader + "\n" + docString)
docFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment