2005-04-01 03:18:54 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2005-03-23 16:32:35 +01:00
|
|
|
###
|
|
|
|
# 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
|
2005-04-08 04:02:10 +02:00
|
|
|
import shutil
|
2005-04-01 03:18:54 +02:00
|
|
|
|
|
|
|
import supybot
|
|
|
|
|
|
|
|
# We need to do this before we import conf.
|
|
|
|
if not os.path.exists('doc-conf'):
|
|
|
|
os.mkdir('doc-conf')
|
|
|
|
|
|
|
|
registryFilename = os.path.join('doc-conf', 'doc.conf')
|
|
|
|
fd = file(registryFilename, 'w')
|
|
|
|
fd.write("""
|
|
|
|
supybot.directories.data: doc-data
|
|
|
|
supybot.directories.conf: doc-conf
|
|
|
|
supybot.directories.log: doc-logs
|
|
|
|
supybot.log.stdout: False
|
|
|
|
supybot.log.level: DEBUG
|
|
|
|
supybot.log.format: %(levelname)s %(message)s
|
|
|
|
supybot.log.plugins.individualLogfiles: False
|
|
|
|
supybot.databases: sqlite anydbm cdb flat pickle
|
|
|
|
""")
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
import supybot.registry as registry
|
|
|
|
registry.open(registryFilename)
|
|
|
|
|
2005-03-22 14:58:53 +01:00
|
|
|
import supybot.log as log
|
2005-03-23 17:41:07 +01:00
|
|
|
import supybot.conf as conf
|
2005-04-01 03:18:54 +02:00
|
|
|
conf.supybot.flush.setValue(False)
|
|
|
|
|
|
|
|
import cgi
|
|
|
|
import sys
|
|
|
|
import textwrap
|
|
|
|
|
2005-03-23 17:41:07 +01:00
|
|
|
import supybot.utils as utils
|
2005-04-01 03:18:54 +02:00
|
|
|
import supybot.world as world
|
|
|
|
import supybot.plugin as plugin
|
|
|
|
import supybot.registry as registry
|
2005-03-23 16:32:35 +01:00
|
|
|
|
2005-04-01 03:18:54 +02:00
|
|
|
world.documenting = True
|
2005-03-22 14:58:53 +01:00
|
|
|
|
|
|
|
class PluginDoc(object):
|
2005-04-01 03:18:54 +02:00
|
|
|
def __init__(self, mod, escape):
|
2005-03-22 14:58:53 +01:00
|
|
|
self.mod = mod
|
2005-04-01 03:18:54 +02:00
|
|
|
self.inst = self.mod.Class(None)
|
|
|
|
self.name = self.mod.Class.__name__
|
|
|
|
self.escape = escape
|
2005-03-22 14:58:53 +01:00
|
|
|
self.lines = []
|
2005-04-01 03:18:54 +02:00
|
|
|
|
|
|
|
def appendLine(self, line, indent=0, escaped=False):
|
|
|
|
line = line.strip()
|
|
|
|
if escaped and self.escape:
|
|
|
|
line = cgi.escape(line)
|
|
|
|
if self.escape:
|
2005-07-26 14:22:16 +02:00
|
|
|
sections = line.split("'")
|
|
|
|
slen = len(sections) - 1
|
|
|
|
# Don't want to escape characters inside of single-quoted strings
|
|
|
|
if slen and slen % 2 == 0:
|
|
|
|
for i in xrange(0, 2, slen):
|
|
|
|
sections[i] = sections[i].replace('[', '[')
|
|
|
|
sections[i] = sections[i].replace(']', ']')
|
|
|
|
line = "'".join(sections)
|
2005-04-01 03:18:54 +02:00
|
|
|
indent = ' ' * indent
|
|
|
|
lines = textwrap.wrap(line, 79,
|
|
|
|
initial_indent=indent,
|
|
|
|
subsequent_indent=indent)
|
|
|
|
self.lines.extend(lines)
|
|
|
|
self.lines.append('')
|
|
|
|
|
2005-03-22 14:58:53 +01:00
|
|
|
def renderSTX(self):
|
2005-04-01 03:18:54 +02:00
|
|
|
self.appendLine('Documentation for the %s plugin for '
|
|
|
|
'Supybot' % self.name)
|
|
|
|
self.appendLine('Purpose', 1)
|
|
|
|
pdoc = getattr(self.mod, '__doc__',
|
|
|
|
'My author didn\'t give me a purpose.')
|
|
|
|
self.appendLine(pdoc, 2, escaped=True)
|
|
|
|
cdoc = getattr(self.mod.Class, '__doc__', None)
|
|
|
|
if cdoc is not None:
|
|
|
|
self.appendLine('Usage', 1)
|
|
|
|
self.appendLine(cdoc, 2, escaped=True)
|
|
|
|
commands = self.inst.listCommands()
|
2005-03-23 17:41:07 +01:00
|
|
|
if len(commands):
|
|
|
|
self.appendLine('Commands', 1)
|
2005-04-01 03:18:54 +02:00
|
|
|
for command in commands:
|
|
|
|
log.debug('command: %s', command)
|
|
|
|
if self.escape:
|
|
|
|
line = '* **%s** ' % command
|
2005-03-23 16:57:57 +01:00
|
|
|
else:
|
2005-04-01 03:18:54 +02:00
|
|
|
line = '* %s ' % command
|
|
|
|
command = command.split()
|
|
|
|
doc = self.inst.getCommandHelp(command)
|
|
|
|
if doc:
|
|
|
|
doc = doc.replace('\x02', '')
|
|
|
|
(args, help) = doc.split(')', 1)
|
|
|
|
args = args.split('(', 1)[1]
|
|
|
|
args = args[len(' '.join(command)):].strip()
|
|
|
|
help = help.split('--', 1)[1].strip()
|
|
|
|
self.appendLine(line + args, 2, escaped=True)
|
|
|
|
self.appendLine(help, 3, escaped=True)
|
|
|
|
else:
|
|
|
|
self.appendLine('No help associated with this command', 3)
|
|
|
|
# now the config
|
2005-03-23 17:41:07 +01:00
|
|
|
self.appendLine('Configuration', 1)
|
|
|
|
try:
|
2005-04-01 03:18:54 +02:00
|
|
|
confs = conf.supybot.plugins.get(self.name)
|
2005-03-23 17:41:07 +01:00
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
log.info('No configuration for plugin %s', plugin)
|
|
|
|
self.appendLine('No help configuration with this plugin', 2)
|
2005-04-01 03:18:54 +02:00
|
|
|
else:
|
|
|
|
self.genConfig(confs, 2)
|
|
|
|
return '\n'.join(self.lines) + '\n'
|
|
|
|
|
|
|
|
def genConfig(self, item, origindent):
|
|
|
|
confVars = item.getValues(getChildren=False, fullNames=False)
|
|
|
|
if not confVars:
|
|
|
|
return
|
|
|
|
for (c, v) in confVars:
|
|
|
|
name = '* %s' % v._name
|
|
|
|
self.appendLine(name, origindent)
|
|
|
|
indent = origindent + 1
|
|
|
|
try:
|
|
|
|
default = str(v)
|
2005-07-26 14:22:16 +02:00
|
|
|
if isinstance(v._default, basestring) or v._default is None:
|
2005-04-01 03:18:54 +02:00
|
|
|
default = utils.str.dqrepr(default)
|
|
|
|
help = v.help()
|
|
|
|
channelValue = v.channelValue
|
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if channelValue:
|
|
|
|
cv = 'is'
|
|
|
|
else:
|
|
|
|
cv = 'is not'
|
|
|
|
if self.escape:
|
|
|
|
default = '**%s**' % default
|
|
|
|
self.appendLine('This config variable defaults to %s and %s '
|
|
|
|
'channel specific.' % (default, cv), indent)
|
|
|
|
if self.escape:
|
|
|
|
help = cgi.escape(help)
|
|
|
|
self.appendLine(help, indent)
|
|
|
|
self.genConfig(v, indent)
|
|
|
|
|
|
|
|
def genDoc(m, escape):
|
|
|
|
Plugin = PluginDoc(m, escape)
|
|
|
|
print 'Generating documentation for %s...' % Plugin.name
|
|
|
|
try:
|
|
|
|
fd = file('%s.stx' % Plugin.name, 'w')
|
|
|
|
except EnvironmentError, e:
|
|
|
|
sys.stderr.write('Unable to open %s.stx for writing.' % Plugin.name)
|
|
|
|
sys.exit(-1)
|
|
|
|
try:
|
|
|
|
fd.write(Plugin.renderSTX())
|
|
|
|
finally:
|
|
|
|
fd.close()
|
2005-03-22 14:58:53 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2005-04-01 03:18:54 +02:00
|
|
|
import glob
|
|
|
|
import os.path
|
|
|
|
import optparse
|
|
|
|
import supybot.plugin as plugin
|
|
|
|
|
|
|
|
parser = optparse.OptionParser(usage='Usage: %prog [options] [plugins]',
|
|
|
|
version='Supybot %s' % conf.version)
|
|
|
|
parser.add_option('-c', '--clean', action='store_true', default=False,
|
2005-04-08 04:02:10 +02:00
|
|
|
dest='clean', help='Cleans the various data/conf/logs '
|
|
|
|
'directories after generating the docs.')
|
2005-04-01 03:18:54 +02:00
|
|
|
parser.add_option('--no-escape',
|
|
|
|
action='store_false', default=True, dest='escape',
|
|
|
|
help='Disables escaping of html entities e.g., < as '
|
|
|
|
'<. This is useful for making offline documentation.')
|
|
|
|
parser.add_option('--plugins-dir',
|
|
|
|
action='append', dest='pluginsDirs', default=[],
|
|
|
|
help='Looks in in the given directory for plugins and '
|
2005-04-08 04:02:10 +02:00
|
|
|
'generates documentation for all of them.')
|
2005-04-01 03:18:54 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
# This must go before checking for args, of course.
|
|
|
|
for pluginDir in options.pluginsDirs:
|
|
|
|
for name in glob.glob(os.path.join(pluginDir, '*')):
|
|
|
|
#print '***', name
|
|
|
|
if os.path.isdir(name):
|
|
|
|
args.append(name)
|
|
|
|
|
|
|
|
if not args:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
args = [s.rstrip('\\/') for s in args]
|
|
|
|
pluginDirs = set([os.path.dirname(s) or '.' for s in args])
|
|
|
|
conf.supybot.directories.plugins.setValue(list(pluginDirs))
|
|
|
|
pluginNames = set([os.path.basename(s) for s in args])
|
|
|
|
plugins = set([])
|
|
|
|
for pluginName in pluginNames:
|
|
|
|
if pluginName.endswith('.py'):
|
|
|
|
pluginName = pluginName[:-3]
|
|
|
|
try:
|
|
|
|
pluginModule = plugin.loadPluginModule(pluginName)
|
|
|
|
except ImportError, e:
|
|
|
|
sys.stderr.write('Failed to load plugin %s: %s\n' % (pluginName,e))
|
|
|
|
sys.stderr.write('(pluginDirs: %s)' %
|
|
|
|
conf.supybot.directories.plugins())
|
|
|
|
sys.exit(-1)
|
|
|
|
plugins.add(pluginModule)
|
|
|
|
|
|
|
|
for Plugin in plugins:
|
|
|
|
genDoc(Plugin, options.escape)
|
2005-03-23 16:32:35 +01:00
|
|
|
|
2005-04-08 04:02:10 +02:00
|
|
|
if options.clean:
|
|
|
|
shutil.rmtree(conf.supybot.directories.log())
|
|
|
|
shutil.rmtree(conf.supybot.directories.conf())
|
|
|
|
shutil.rmtree(conf.supybot.directories.data())
|
|
|
|
|
2005-03-23 16:32:35 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=78:
|