From b92a122f330caed2d4ce22b1ac4aab3f1b3e7011 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Fri, 14 Jul 2017 14:21:46 +0200 Subject: Clean up doc source generators --- docs/source/generate_commands.py | 125 ++++++++++++++++++--------------------- docs/source/generate_configs.py | 36 +++++++---- 2 files changed, 82 insertions(+), 79 deletions(-) (limited to 'docs') diff --git a/docs/source/generate_commands.py b/docs/source/generate_commands.py index 99607fbd..c3db693f 100755 --- a/docs/source/generate_commands.py +++ b/docs/source/generate_commands.py @@ -1,24 +1,22 @@ from __future__ import absolute_import + +import argparse import sys import os + HERE = os.path.dirname(__file__) sys.path.insert(0, os.path.join(HERE, '..', '..')) + from alot.commands import * from alot.commands import COMMANDS import alot.buffers -from argparse import HelpFormatter, SUPPRESS, OPTIONAL, ZERO_OR_MORE, ONE_OR_MORE, PARSER, REMAINDER from alot.utils.argparse import BooleanAction -from gettext import gettext as _ -import collections as _collections -import copy as _copy -import os as _os -import re as _re -import sys as _sys -import textwrap as _textwrap + NOTE = ".. CAUTION: THIS FILE IS AUTO-GENERATED!\n\n\n" -class HF(HelpFormatter): + +class HF(argparse.HelpFormatter): def _metavar_formatter(self, action, default_metavar): if action.metavar is not None: result = action.metavar @@ -34,67 +32,60 @@ class HF(HelpFormatter): def rstify_parser(parser): - #header = parser.format_usage().strip() - #print '\n\n%s\n' % header + '_' * len(header) - parser.formatter_class = HF - #parser.print_help() - #continue - - formatter = parser._get_formatter() - out = "" - - # usage - usage = formatter._format_usage(None, parser._actions, - parser._mutually_exclusive_groups, - '').strip() - usage = usage.replace('--','---') - - # section header - out += '.. describe:: %s\n\n' % parser.prog - - # description - out += ' '*4 + parser.description + parser.formatter_class = HF + + formatter = parser._get_formatter() + out = "" + + # usage + usage = formatter._format_usage(None, parser._actions, + parser._mutually_exclusive_groups, + '').strip() + usage = usage.replace('--', '---') + + # section header + out += '.. describe:: %s\n\n' % parser.prog + + # description + out += ' '*4 + parser.description + out += '\n\n' + + if len(parser._positionals._group_actions) == 1: + out += " argument\n" + a = parser._positionals._group_actions[0] + out += ' '*8 + str(parser._positionals._group_actions[0].help) + if a.choices: + out += ". valid choices are: %s." % ','.join(['\`%s\`' % s for s + in a.choices]) + if a.default: + out += ". defaults to: '%s'." % a.default out += '\n\n' - - if len(parser._positionals._group_actions) == 1: - out += " argument\n" - a = parser._positionals._group_actions[0] - out += ' '*8 + str(parser._positionals._group_actions[0].help) + elif len(parser._positionals._group_actions) > 1: + out += " positional arguments\n" + for index, a in enumerate(parser._positionals._group_actions): + out += " %s: %s" % (index, a.help) if a.choices: out += ". valid choices are: %s." % ','.join(['\`%s\`' % s for s in a.choices]) if a.default: out += ". defaults to: '%s'." % a.default - out += '\n\n' - elif len(parser._positionals._group_actions) > 1: - out += " positional arguments\n" - for index, a in enumerate(parser._positionals._group_actions): - out += " %s: %s" % (index, a.help) - if a.choices: - out += ". valid choices are: %s." % ','.join(['\`%s\`' % s for s - in a.choices]) - if a.default: - out += ". defaults to: '%s'." % a.default - out += '\n' - out += '\n\n' - - if parser._optionals._group_actions: - out += " optional arguments\n" - for a in parser._optionals._group_actions: - switches = [s.replace('--','---') for s in a.option_strings] - out += " :%s: %s" % (', '.join(switches), a.help) - if a.choices and not isinstance(a, BooleanAction): - out += ". Valid choices are: %s" % ','.join(['\`%s\`' % s for s - in a.choices]) - if a.default: - out += " (Defaults to: '%s')" % a.default - out += '.\n' - out += '\n' - - # epilog - #out += formatter.add_text(parser.epilog) + out += '\n' + out += '\n\n' - return out + if parser._optionals._group_actions: + out += " optional arguments\n" + for a in parser._optionals._group_actions: + switches = [s.replace('--', '---') for s in a.option_strings] + out += " :%s: %s" % (', '.join(switches), a.help) + if a.choices and not isinstance(a, BooleanAction): + out += ". Valid choices are: %s" % ','.join(['\`%s\`' % s for s + in a.choices]) + if a.default: + out += " (Defaults to: '%s')" % a.default + out += '.\n' + out += '\n' + + return out def get_mode_docs(): docs = {} @@ -111,18 +102,20 @@ if __name__ == "__main__": modes = [] for mode, modecommands in COMMANDS.items(): modefilename = mode+'.rst' - modefile = open(os.path.join(HERE, 'usage', 'modes', modefilename), 'w') + modefile = open(os.path.join(HERE, 'usage', 'modes', modefilename), + 'w') modefile.write(NOTE) if mode != 'global': modes.append(mode) header = 'Commands in `%s` mode' % mode modefile.write('%s\n%s\n' % (header, '-' * len(header))) - modefile.write('The following commands are available in %s mode\n\n' % mode) + modefile.write('The following commands are available in %s mode' + '\n\n' % mode) else: header = 'Global Commands' modefile.write('%s\n%s\n' % (header, '-' * len(header))) modefile.write('The following commands are available globally\n\n') - for cmdstring,struct in modecommands.items(): + for cmdstring, struct in modecommands.items(): cls, parser, forced_args = struct labelline = '.. _cmd.%s.%s:\n\n' % (mode, cmdstring.replace('_', '-')) diff --git a/docs/source/generate_configs.py b/docs/source/generate_configs.py index 2b9cc806..a5427792 100755 --- a/docs/source/generate_configs.py +++ b/docs/source/generate_configs.py @@ -1,12 +1,18 @@ from __future__ import absolute_import + import sys import os +import re + +from configobj import ConfigObj +from validate import Validator + HERE = os.path.dirname(__file__) sys.path.insert(0, os.path.join(HERE, '..', '..')) + from alot.commands import COMMANDS -from configobj import ConfigObj -from validate import Validator -import re + + NOTE = """ .. CAUTION: THIS FILE IS AUTO-GENERATED from the inline comments of specfile %s. @@ -14,6 +20,8 @@ NOTE = """ If you want to change its content make your changes to that spec to ensure they woun't be overwritten later. """ + + def rewrite_entries(config, path, specpath, sec=None, sort=False): file = open(path, 'w') file.write(NOTE % specpath) @@ -24,10 +32,6 @@ def rewrite_entries(config, path, specpath, sec=None, sort=False): sec.scalars.sort() for entry in sec.scalars: v = Validator() - #config.validate(v) - #print config[entry] - #etype = re.sub('\(.*\)','', config[entry]) - ##if etype == 'option': etype, eargs, ekwargs, default = v._parse_check(sec[entry]) if default is not None: default = config._quote(default) @@ -49,22 +53,28 @@ def rewrite_entries(config, path, specpath, sec=None, sort=False): description += '\n :type: %s\n' % etype if default is not None: - default = default.replace('*','\\*') - if etype in ['string', 'string_list', 'gpg_key_hint'] and default != 'None': + default = default.replace('*', '\\*') + if etype in ['string', 'string_list', 'gpg_key_hint'] and \ + default != 'None': description += ' :default: "%s"\n\n' % (default) else: description += ' :default: %s\n\n' % (default) file.write(description) file.close() + if __name__ == "__main__": - specpath = os.path.join(HERE, '..','..', 'alot', 'defaults', 'alot.rc.spec') - config = ConfigObj(None, configspec=specpath, stringify=False, list_values=False) + specpath = os.path.join(HERE, '..', '..', 'alot', 'defaults', + 'alot.rc.spec') + config = ConfigObj(None, configspec=specpath, stringify=False, + list_values=False) config.validate(Validator()) alotrc_table_file = os.path.join(HERE, 'configuration', 'alotrc_table') - rewrite_entries(config.configspec, alotrc_table_file, 'defaults/alot.rc.spec', sort=True) + rewrite_entries(config.configspec, alotrc_table_file, + 'defaults/alot.rc.spec', sort=True) - rewrite_entries(config, os.path.join(HERE, 'configuration', 'accounts_table'), + rewrite_entries(config, + os.path.join(HERE, 'configuration', 'accounts_table'), 'defaults/alot.rc.spec', sec=config.configspec['accounts']['__many__']) -- cgit v1.2.3