Limnoria/scripts/supybot-plugin-doc

166 lines
6.4 KiB
Plaintext
Raw Normal View History

###
# Copyright (c) 2005, Ali Afshar
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
2005-03-22 14:58:53 +01:00
import os
import cgi
2005-03-22 14:58:53 +01:00
import sys
import imp
import supybot.log as log
import supybot.conf as conf
import supybot.registry as registry
import supybot.utils as utils
2005-03-22 14:58:53 +01:00
log.setLevel(10)
def genModules(*dirs):
log.info('Generating plugin modules.')
2005-03-22 14:58:53 +01:00
for plugindir in dirs:
log.info('Analysing %s', plugindir)
# strip the trailing separator
2005-03-22 14:58:53 +01:00
path, plugin = os.path.split(plugindir.rstrip(os.sep))
# len(plugin) will ? always be true
2005-03-22 14:58:53 +01:00
if len(plugin) and plugin[0].isupper():
try:
fm = imp.find_module(plugin, [path])
except ImportError:
log.warning('Failed find_module, skipping %s', plugin)
2005-03-22 14:58:53 +01:00
continue
if fm[0]:
log.warning('Module is not a directory, skipping %s', plugin)
2005-03-22 14:58:53 +01:00
continue
try:
mod = imp.load_module(plugin, *fm)
log.info('Successfully loaded %s', plugin)
yield mod
2005-03-22 14:58:53 +01:00
except ImportError:
log.warning('Failed load_module, skipping %s', plugin)
2005-03-22 14:58:53 +01:00
continue
else:
log.warning('Does not start with an uppercase, skipping %s', plugin)
2005-03-22 14:58:53 +01:00
continue
class PluginDoc(object):
def __init__(self, mod):
self.mod = mod
self.lines = []
def appendLine(self, line, indent=0):
self.lines.append('%s%s' % (' ' * indent * 2, cgi.escape(line)))
def renderSTX(self):
inst = self.mod.Class(None)
self.appendLine('Documentation for the %s plugin for'
'supybot' % self.mod.Class.__name__)
2005-03-22 14:58:53 +01:00
self.appendLine('')
commands = inst.listCommands()
if len(commands):
self.appendLine('Commands', 1)
self.appendLine('')
2005-03-22 14:58:53 +01:00
for command in inst.listCommands():
log.debug('command: %s', command)
2005-03-22 14:58:53 +01:00
self.appendLine(command, 2)
self.appendLine('')
com = getattr(self.mod.Class, command, False)
doc = None
if com:
doc = com.__doc__.splitlines()
else:
# this is a nested plugin
nclass = None
try:
nroot, nname = command.split()
except ValueError:
log.warning('Failed to find command, skipping %s', command)
break
nclass = getattr(self.mod.Class, nroot, False)
if nclass:
ncom = getattr(nclass, nname, False)
doc = ncom.__doc__.splitlines()
else:
log.warning('Failed to find nested pligun command, skipping %s',
command)
break
2005-03-22 14:58:53 +01:00
if doc:
args = doc.pop(0)
doc = [l.strip() for l in doc]
self.appendLine('Arguments: **%s**' % args, 3)
self.appendLine('')
self.appendLine('Description: %s' % ''.join(doc), 3)
self.appendLine('')
else:
self.appendLine('No help Associated with this command', 3)
self.appendLine('')
# now the config
self.appendLine('')
self.appendLine('Configuration', 1)
self.appendLine('')
try:
confs = conf.supybot.plugins.get(self.mod.Class.__name__)
except registry.NonExistentRegistryEntry:
log.info('No configuration for plugin %s', plugin)
self.appendLine('No help configuration with this plugin', 2)
self.appendLine('')
confs = None
confVars = None
if confs:
confVars = confs.getValues(getChildren=True)
if confVars:
for (c, v) in confVars:
try:
self.appendLine(c, 2)
self.appendLine('')
default = str(v)
#if isinstance(v._default, basestring):
#default = utils.dqrepr(default)
help = v.help()
channelValue = v.channelValue
self.appendLine('Default: %s' % default, 3)
self.appendLine('')
self.appendLine('Channel Specific: %s' % channelValue, 3)
self.appendLine('')
self.appendLine('Help: %s' % help, 3)
self.appendLine('')
except registry.NonExistentRegistryEntry:
self.appendLine('')
pass
self.appendLine('')
2005-03-22 14:58:53 +01:00
return '\n'.join(self.lines)
def main(*args):
for m in genModules(*args):
f.write(PluginDoc(m).renderSTX())
if __name__ == '__main__':
f = open('f.stx', 'w')
2005-03-22 14:58:53 +01:00
main(*sys.argv[1:])
f.close()
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=78: