Limnoria-doc/generate_plugin_doc.py

82 lines
2.9 KiB
Python
Raw Normal View History

2011-06-28 08:11:32 +02:00
#!/usr/bin/env python2
from __future__ import with_statement
import os
import re
import sys
# Commands instances are converted into SynchronizedAndFirewalled
from supybot.callbacks import SynchronizedAndFirewalled as Commands
validCommandName = re.compile('^[a-z]+$')
def main():
pluginNames = sys.argv[1:]
for pluginName in pluginNames:
supybot = __import__('supybot.plugins.%s.plugin' % pluginName)
PluginClass = getattr(supybot.plugins, pluginName).plugin.Class
filename = 'use/plugins/%s.rst' % pluginName.lower()
2011-06-28 17:29:38 +02:00
try:
os.unlink(filename)
except OSError:
pass
2011-06-28 08:11:32 +02:00
with open(filename, 'a') as fd:
fd.write('\n.. _plugin-%s:\n\nThe %s plugin\n' %
(pluginName.lower(), pluginName))
fd.write('='*len('The %s plugin' % pluginName))
fd.write('\n\n')
2011-06-28 17:29:38 +02:00
writeDoc(PluginClass, fd, pluginName.lower())
2011-06-28 08:11:32 +02:00
def writeDoc(PluginClass, fd, prefix):
2011-06-28 17:29:38 +02:00
prefix += ' '
2011-06-28 08:11:32 +02:00
for attributeName, attribute in PluginClass.__dict__.items():
if not callable(attribute):
continue
if not validCommandName.match(attributeName):
continue
2011-06-28 17:29:38 +02:00
if attributeName == 'die':
continue
2011-06-28 08:11:32 +02:00
if isinstance(attribute, Commands):
writeDoc(attribute, fd, prefix + attributeName)
else:
if attribute.__doc__ is None:
attribute.__doc__ = ''
syntax = attribute.__doc__.split('\n\n')[0].strip()
2011-06-28 17:29:38 +02:00
if syntax == 'takes no arguments' or syntax == '' or syntax == '':
2011-06-28 08:11:32 +02:00
syntax = ''
else:
syntax = ' ' + syntax
args = {
'prefix_dash': prefix.replace(' ', '-'),
'command': attributeName, # Does not contain spaces
'prefix_with_trailing_space': prefix,
'syntax': syntax,
'help_string': parseHelpString(attribute.__doc__),
}
args['decoration'] = '^'*len('%(prefix_with_trailing_space)s%(command)s%(syntax)s' %
args)
2011-06-28 08:13:53 +02:00
fd.write('.. _command-%(prefix_dash)s%(command)s:\n\n'
2011-06-28 08:11:32 +02:00
'%(prefix_with_trailing_space)s%(command)s%(syntax)s\n'
'%(decoration)s\n\n'
'%(help_string)s\n\n' % args)
def parseHelpString(string):
# Remove the syntax
string = '\n\n'.join(string.split('\n\n')[1:])
# Remove the starting and ending spaces
string = '\n'.join([x.strip(' ') for x in string.split('\n')])
2011-06-28 17:29:38 +02:00
if string.endswith('\n'):
string = string[0:-1]
2011-06-28 08:11:32 +02:00
# Put the argument names into italic
string = re.sub(r'(<[^>]+>)', r'*\1*', string, re.M)
string = re.sub(r'(--[^ ]+)', r'*\1*', string, re.M)
2011-06-28 17:29:38 +02:00
# Turn config variable names into refs
string = re.sub(r'(supybot.[a-zA-Z0-9.]+)', r':ref:`\1`', string, re.M)
2011-06-28 08:11:32 +02:00
return string
if __name__ == '__main__':
main()