2020-01-05 20:29:41 +01:00
|
|
|
#!/usr/bin/env python3
|
2005-04-01 03:18:54 +02:00
|
|
|
|
2005-03-23 16:32:35 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2005, Ali Afshar
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2009, James McCoy
|
2005-03-23 16:32:35 +01:00
|
|
|
# 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
|
2009-04-28 06:36:06 +02:00
|
|
|
import sys
|
2005-04-08 04:02:10 +02:00
|
|
|
import shutil
|
2021-04-05 11:32:09 +02:00
|
|
|
import string
|
2005-04-01 03:18:54 +02:00
|
|
|
|
|
|
|
import supybot
|
|
|
|
|
2009-04-28 06:36:06 +02:00
|
|
|
def error(s):
|
|
|
|
sys.stderr.write('%s\n' % s)
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2005-04-01 03:18:54 +02:00
|
|
|
# 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')
|
2009-04-28 06:36:06 +02:00
|
|
|
try:
|
2012-08-04 13:13:16 +02:00
|
|
|
fd = open(registryFilename, 'w')
|
2009-04-28 06:36:06 +02:00
|
|
|
fd.write("""
|
2005-04-01 03:18:54 +02:00
|
|
|
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
|
2021-04-05 11:31:27 +02:00
|
|
|
supybot.databases: sqlite3 anydbm cdb flat pickle
|
2005-04-01 03:18:54 +02:00
|
|
|
""")
|
2009-04-28 06:36:06 +02:00
|
|
|
fd.close()
|
2013-04-27 16:16:08 +02:00
|
|
|
except EnvironmentError as e:
|
2009-04-28 06:36:06 +02:00
|
|
|
error('Unable to open %s for writing.' % registryFilename)
|
2005-04-01 03:18:54 +02:00
|
|
|
|
|
|
|
import supybot.registry as registry
|
2012-08-04 13:13:16 +02:00
|
|
|
registry.open_registry(registryFilename)
|
2005-04-01 03:18:54 +02:00
|
|
|
|
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 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
|
|
|
|
2021-04-05 11:32:09 +02:00
|
|
|
TITLE_TEMPLATE = 'Documentation for the $name plugin for Supybot'
|
|
|
|
|
2005-03-22 14:58:53 +01:00
|
|
|
class PluginDoc(object):
|
2021-04-05 11:32:09 +02:00
|
|
|
def __init__(self, mod, titleTemplate):
|
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__
|
2009-05-03 16:54:04 +02:00
|
|
|
self.appendExtraBlankLine = False
|
2021-04-05 11:32:09 +02:00
|
|
|
self.titleTemplate = string.Template(titleTemplate)
|
2005-03-22 14:58:53 +01:00
|
|
|
self.lines = []
|
2005-04-01 03:18:54 +02:00
|
|
|
|
2009-04-28 01:07:01 +02:00
|
|
|
def appendLine(self, line, indent=0):
|
2005-04-01 03:18:54 +02:00
|
|
|
line = line.strip()
|
|
|
|
indent = ' ' * indent
|
2021-04-05 17:33:37 +02:00
|
|
|
# this looked like a good idea, but it's actually breaking lines
|
|
|
|
# in the middle of ReST elements:
|
|
|
|
#lines = textwrap.wrap(line, 79,
|
|
|
|
# initial_indent=indent,
|
|
|
|
# subsequent_indent=indent)
|
|
|
|
lines = [indent + line.replace('\n', '\n' + indent)]
|
2005-04-01 03:18:54 +02:00
|
|
|
self.lines.extend(lines)
|
2009-05-03 16:54:04 +02:00
|
|
|
if self.appendExtraBlankLine:
|
|
|
|
self.lines.append('')
|
|
|
|
|
|
|
|
def renderRST(self):
|
|
|
|
self.appendExtraBlankLine = False
|
2021-04-05 12:25:17 +02:00
|
|
|
self.appendLine('.. _plugin-%s:' % self.name)
|
|
|
|
self.lines.append('')
|
2021-04-05 11:32:09 +02:00
|
|
|
s = self.titleTemplate.substitute(name=self.name)
|
2009-05-03 16:54:04 +02:00
|
|
|
self.appendLine(s)
|
|
|
|
self.appendLine('=' * len(s))
|
|
|
|
self.lines.append('')
|
|
|
|
self.appendLine('Purpose')
|
|
|
|
self.appendLine('-------')
|
|
|
|
pdoc = getattr(self.mod, '__doc__',
|
|
|
|
'My author didn\'t give me a purpose.')
|
|
|
|
self.appendLine(pdoc)
|
|
|
|
self.lines.append('')
|
|
|
|
cdoc = getattr(self.mod.Class, '__doc__', None)
|
|
|
|
if cdoc is not None:
|
|
|
|
self.appendLine('Usage')
|
|
|
|
self.appendLine('-----')
|
2021-04-05 17:33:37 +02:00
|
|
|
# We add spaces at the beginning in case the docstring does not
|
|
|
|
# start with a newline (the default, unfortunately)
|
|
|
|
self.appendLine(textwrap.dedent(" "* 1000 + cdoc).lstrip())
|
2009-05-03 16:54:04 +02:00
|
|
|
self.lines.append('')
|
|
|
|
commands = self.inst.listCommands()
|
|
|
|
if len(commands):
|
2021-04-05 22:56:40 +02:00
|
|
|
self.lines.append('.. _commands-%s:\n' % self.name)
|
2009-05-03 16:54:04 +02:00
|
|
|
self.appendLine('Commands')
|
|
|
|
self.appendLine('--------')
|
|
|
|
for command in commands:
|
|
|
|
log.debug('command: %s', command)
|
2021-04-05 22:56:40 +02:00
|
|
|
self.lines.append('.. _command-%s-%s:\n' %
|
|
|
|
(self.name.lower(), command.replace(' ', '.')))
|
2009-05-03 16:54:04 +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)
|
|
|
|
self.appendLine(help, 1)
|
|
|
|
else:
|
|
|
|
self.appendLine('No help associated with this command')
|
2009-05-08 03:46:40 +02:00
|
|
|
self.lines.append('')
|
2009-05-03 16:54:04 +02:00
|
|
|
# now the config
|
|
|
|
try:
|
|
|
|
confs = conf.supybot.plugins.get(self.name)
|
2021-04-05 22:56:40 +02:00
|
|
|
self.lines.append('.. _conf-%s:\n' % self.name)
|
2009-05-08 03:46:40 +02:00
|
|
|
self.appendLine('Configuration')
|
|
|
|
self.appendLine('-------------')
|
2021-04-05 22:56:40 +02:00
|
|
|
self.lines.append('')
|
2009-05-03 16:54:04 +02:00
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
log.info('No configuration for plugin %s', plugin)
|
2009-05-08 03:46:40 +02:00
|
|
|
self.appendLine('No configuration for this plugin')
|
2009-05-03 16:54:04 +02:00
|
|
|
else:
|
2009-05-08 03:46:40 +02:00
|
|
|
for confValues in self.genConfig(confs, 0):
|
2021-04-22 00:26:30 +02:00
|
|
|
(name, kind, help, default, indent) = confValues
|
|
|
|
self.appendLine('.. _conf-%s:' % name, indent - 1)
|
|
|
|
self.lines.append('\n')
|
2009-05-08 03:46:40 +02:00
|
|
|
self.appendLine('%s' % name, indent - 1)
|
2021-04-22 00:26:30 +02:00
|
|
|
if default:
|
|
|
|
self.appendLine(
|
|
|
|
('This config variable defaults to %s, %s')
|
|
|
|
% (default, kind), indent)
|
|
|
|
else:
|
|
|
|
self.appendLine('This %s' % kind, indent)
|
|
|
|
if help:
|
|
|
|
self.lines.append('')
|
|
|
|
self.appendLine(help, indent)
|
2009-05-08 03:46:40 +02:00
|
|
|
self.lines.append('')
|
2009-05-03 16:54:04 +02:00
|
|
|
return '\n'.join(self.lines) + '\n'
|
2005-04-01 03:18:54 +02:00
|
|
|
|
2005-03-22 14:58:53 +01:00
|
|
|
def renderSTX(self):
|
2009-05-03 16:54:04 +02:00
|
|
|
self.appendExtraBlankLine = True
|
2021-04-05 11:32:09 +02:00
|
|
|
self.appendLine(self.titleTemplate.substitute(name=self.name))
|
2005-04-01 03:18:54 +02:00
|
|
|
self.appendLine('Purpose', 1)
|
|
|
|
pdoc = getattr(self.mod, '__doc__',
|
|
|
|
'My author didn\'t give me a purpose.')
|
2009-04-28 01:07:01 +02:00
|
|
|
self.appendLine(pdoc, 2)
|
2005-04-01 03:18:54 +02:00
|
|
|
cdoc = getattr(self.mod.Class, '__doc__', None)
|
|
|
|
if cdoc is not None:
|
|
|
|
self.appendLine('Usage', 1)
|
2009-04-28 01:07:01 +02:00
|
|
|
self.appendLine(cdoc, 2)
|
2005-04-01 03:18:54 +02:00
|
|
|
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)
|
2009-04-28 01:07:01 +02:00
|
|
|
line = '* %s ' % command
|
2005-04-01 03:18:54 +02:00
|
|
|
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()
|
2009-04-28 01:07:01 +02:00
|
|
|
self.appendLine(line + args, 2)
|
|
|
|
self.appendLine(help, 3)
|
2005-04-01 03:18:54 +02:00
|
|
|
else:
|
|
|
|
self.appendLine('No help associated with this command', 3)
|
|
|
|
# now the config
|
2005-03-23 17:41:07 +01:00
|
|
|
try:
|
2005-04-01 03:18:54 +02:00
|
|
|
confs = conf.supybot.plugins.get(self.name)
|
2009-05-08 03:46:40 +02:00
|
|
|
self.appendLine('Configuration', 1)
|
2005-03-23 17:41:07 +01:00
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
log.info('No configuration for plugin %s', plugin)
|
2009-05-08 03:46:40 +02:00
|
|
|
self.appendLine('No configuration for this plugin', 2)
|
2005-04-01 03:18:54 +02:00
|
|
|
else:
|
2009-05-08 03:46:40 +02:00
|
|
|
for confValues in self.genConfig(confs, 2):
|
2021-04-22 00:26:30 +02:00
|
|
|
(name, kind, help, default, indent) = confValues
|
2009-05-08 03:46:40 +02:00
|
|
|
self.appendLine('* %s' % name, indent - 1)
|
2021-04-22 00:26:30 +02:00
|
|
|
if default:
|
|
|
|
self.appendLine(
|
|
|
|
('This config variable defaults to %s, %s')
|
|
|
|
% (default, kind), indent)
|
|
|
|
else:
|
|
|
|
self.appendLine('This %s' % kind, indent)
|
2009-05-08 03:46:40 +02:00
|
|
|
self.appendLine(help, indent)
|
2005-04-01 03:18:54 +02:00
|
|
|
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:
|
2009-05-08 03:46:40 +02:00
|
|
|
name = v._name
|
2005-04-01 03:18:54 +02:00
|
|
|
indent = origindent + 1
|
2021-04-22 00:26:30 +02:00
|
|
|
if isinstance(v, registry.Value):
|
|
|
|
try:
|
|
|
|
default = utils.str.dqrepr(str(v))
|
|
|
|
help = v.help()
|
|
|
|
except registry.NonExistentRegistryEntry:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
cv = '' if v._channelValue else ' not'
|
|
|
|
nv = '' if v._networkValue else ' not'
|
|
|
|
kind = (
|
|
|
|
'is%s network-specific, and is %s channel-specific.'
|
|
|
|
% (nv, cv)
|
|
|
|
)
|
2005-04-01 03:18:54 +02:00
|
|
|
else:
|
2021-04-22 00:26:30 +02:00
|
|
|
help = ''
|
|
|
|
default = ''
|
|
|
|
kind = 'is a group of:'
|
|
|
|
yield (name, kind, help, default, indent)
|
2009-05-08 03:46:40 +02:00
|
|
|
for confValues in self.genConfig(v, indent):
|
|
|
|
yield confValues
|
2005-04-01 03:18:54 +02:00
|
|
|
|
2009-04-28 01:37:06 +02:00
|
|
|
def genDoc(m, options):
|
2021-04-05 11:32:09 +02:00
|
|
|
Plugin = PluginDoc(m, options.titleTemplate)
|
2013-04-27 16:16:08 +02:00
|
|
|
print('Generating documentation for %s...' % Plugin.name)
|
2021-04-05 12:27:31 +02:00
|
|
|
outputFilename = string.Template(options.outputFilename).substitute(
|
|
|
|
name=Plugin.name, format=options.format)
|
|
|
|
path = os.path.join(options.outputDir, outputFilename)
|
2005-04-01 03:18:54 +02:00
|
|
|
try:
|
2012-08-04 13:13:16 +02:00
|
|
|
fd = open(path, 'w')
|
2013-04-27 16:16:08 +02:00
|
|
|
except EnvironmentError as e:
|
2009-04-28 06:36:06 +02:00
|
|
|
error('Unable to open %s for writing.' % path)
|
2009-04-28 06:38:35 +02:00
|
|
|
f = getattr(Plugin, 'render%s' % options.format.upper(), None)
|
|
|
|
if f is None:
|
|
|
|
fd.close()
|
|
|
|
error('Unknown render format: `%s\'' % options.format)
|
2005-04-01 03:18:54 +02:00
|
|
|
try:
|
2009-04-28 06:38:35 +02:00
|
|
|
fd.write(f())
|
2005-04-01 03:18:54 +02:00
|
|
|
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.')
|
2009-04-28 01:37:06 +02:00
|
|
|
parser.add_option('-o', '--output-dir', dest='outputDir', default='.',
|
|
|
|
help='Specifies the directory in which to write the '
|
|
|
|
'documentation for the plugin.')
|
2021-04-05 12:27:31 +02:00
|
|
|
parser.add_option('--output-filename', dest='outputFilename',
|
|
|
|
default='$name.$format',
|
|
|
|
help='Specifies the path of individual output files. '
|
|
|
|
'If present in the value, "$name" will be substituted '
|
|
|
|
'with the plugin\'s name and "$format" with the value '
|
|
|
|
'if --format.')
|
2009-04-28 06:38:35 +02:00
|
|
|
parser.add_option('-f', '--format', dest='format', choices=['rst', 'stx'],
|
|
|
|
default='stx', help='Specifies which output format to '
|
|
|
|
'use.')
|
2005-04-01 03:18:54 +02:00
|
|
|
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.')
|
2021-04-05 11:32:09 +02:00
|
|
|
parser.add_option('--title-template',
|
|
|
|
default=TITLE_TEMPLATE, dest='titleTemplate',
|
|
|
|
help='Template string for the title of generated files')
|
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, '*')):
|
|
|
|
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:
|
2021-04-05 12:25:44 +02:00
|
|
|
if pluginName == '__pycache__':
|
|
|
|
continue
|
2005-04-01 03:18:54 +02:00
|
|
|
if pluginName.endswith('.py'):
|
|
|
|
pluginName = pluginName[:-3]
|
|
|
|
try:
|
|
|
|
pluginModule = plugin.loadPluginModule(pluginName)
|
2013-04-27 16:16:08 +02:00
|
|
|
except ImportError as e:
|
2009-04-28 06:36:06 +02:00
|
|
|
s = 'Failed to load plugin %s: %s\n' \
|
2021-04-05 12:25:44 +02:00
|
|
|
'(pluginDirs: %s)' % (pluginName, e,
|
|
|
|
conf.supybot.directories.plugins())
|
2009-04-28 06:36:06 +02:00
|
|
|
error(s)
|
2005-04-01 03:18:54 +02:00
|
|
|
plugins.add(pluginModule)
|
|
|
|
|
|
|
|
for Plugin in plugins:
|
2009-04-28 01:37:06 +02:00
|
|
|
genDoc(Plugin, options)
|
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())
|
|
|
|
|
2009-03-12 00:26:49 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=78:
|