Skip to content

Commit 66e4599

Browse files
committed
Added Full docstring reading of commands and parsing of data to json
1 parent 06d6fe0 commit 66e4599

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

GenerateDoc.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
from cogs.Announce import Announce
2+
from os import listdir
3+
import os
4+
import importlib
5+
from inspect import getmembers, isfunction, ismethod, isclass
6+
import glob
7+
import re
8+
import json
9+
10+
"""
11+
KoalaBot utility function for generating bot command docs
12+
Created By: Charlie Bowe, Aqeel Little
13+
"""
14+
15+
16+
class DocumentationEntry:
17+
"""Class for storing documentation entries for bot commands
18+
"""
19+
name = None
20+
params = []
21+
desc = None
22+
23+
def __init__(self,name: str,params,desc: str):
24+
self.name = name
25+
self.params = params
26+
self.desc = desc
27+
28+
class CogDocumentation:
29+
"""Stores a list of documentation entries for a cog
30+
"""
31+
name = None
32+
docs = []
33+
34+
def __init__(self,name: str, docs):
35+
#Change BaseCog to KoalaBot
36+
if name == 'BaseCog':
37+
self.name = 'KoalaBot'
38+
else:
39+
self.name = name
40+
self.docs = docs
41+
42+
docList = []
43+
44+
45+
#Get the directory of the cogs folder
46+
dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),'cogs')
47+
#Grab the names of all the .py files in the cogs folder
48+
modules = [ fl for fl in listdir(dir) if fl.endswith('.py') ]
49+
50+
cogs = []
51+
#for i in range 0 to amount of cogs
52+
53+
def get_decorators(function):
54+
# If we have no func_closure, it means we are not wrapping any other functions.
55+
if not function.func_closure:
56+
return [function]
57+
decorators = []
58+
# Otherwise, we want to collect all of the recursive results for every closure we have.
59+
for closure in function.func_closure:
60+
decorators.extend(get_decorators(closure.cell_contents))
61+
return [function] + decorators
62+
63+
def get_cog_docs():
64+
for i in range (0,len(modules)):
65+
#Cut off the .py extension
66+
modules[i] = modules[i][:-3]
67+
#Import the library and store it in cogs
68+
cogs.append(__import__('cogs.'+modules[i]))
69+
70+
#get the refernce to the current library
71+
#E.g. cogs.Announce
72+
currentLib = getattr(cogs[i], modules[i])
73+
74+
#Store all of the classes of the cog in classes
75+
classes = [ obj for obj in getmembers(currentLib) if isclass(obj[1]) ]
76+
#print(f'Current classes: {classes} \n')
77+
78+
docs = []
79+
for cls in classes:
80+
if cls[0] != modules[i]:
81+
print(f'{cls[0]} is not {modules[i]}')
82+
83+
#Store the functions of each classes in class_funcs
84+
class_funcs = [ obj for obj in getmembers(cls[1]) ]
85+
86+
for obj in class_funcs:
87+
try:
88+
text = getattr(getattr(currentLib,modules[i]),str(obj[0])).help.splitlines()
89+
except AttributeError:
90+
pass
91+
continue
92+
except:
93+
print("Unexpected error")
94+
continue
95+
96+
name = getattr(getattr(currentLib,modules[i]),str(obj[0])).name
97+
98+
if getattr(getattr(currentLib,modules[i]),str(obj[0])).parent != None:
99+
name = f'{getattr(getattr(currentLib,modules[i]),str(obj[0])).parent} {name}'
100+
101+
desc = ""
102+
params = []
103+
for line in text:
104+
matchObj = re.match( r':param (.*): (.*)', line, re.M|re.I)
105+
106+
if matchObj:
107+
if matchObj.group(1) == 'ctx':
108+
continue
109+
params.append(matchObj.group(1))
110+
else:
111+
desc += line
112+
docs.append(DocumentationEntry(name,params,desc))
113+
114+
115+
docList.append(CogDocumentation(modules[i],docs))
116+
return docList
117+
118+
docList = get_cog_docs()
119+
120+
for cogDoc in docList:
121+
print(f'-= {cogDoc.name} =-')
122+
for doc in cogDoc.docs:
123+
print(doc.name)
124+
125+
def parse_docs(docList, filename):
126+
"""Pass a list of CogDocumentation objects into a json file
127+
:param docList: List of CogDocumentation
128+
:param filename: filename of json file
129+
"""
130+
data = []
131+
for cogDoc in docList:
132+
cog = {}
133+
cog['name'] = cogDoc.name
134+
commands = []
135+
for docEntry in cogDoc.docs:
136+
entry = {}
137+
entry['command'] = docEntry.name
138+
entry['parameters'] = docEntry.params
139+
entry['description'] = docEntry.desc
140+
commands.append(entry)
141+
cog['commands'] = commands
142+
data.append(cog)
143+
144+
file = open(filename, "w")
145+
file.write(json.dumps(data, indent=2))
146+
file.close()
147+
148+
parse_docs(docList,'test.json')

0 commit comments

Comments
 (0)