diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2020-05-03 13:02:01 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2020-05-03 14:40:55 +0200 |
commit | cce4e0578aa4806a31a0a00268bf0b1497d955b9 (patch) | |
tree | 4a20901c8637ef5a6b716155a80aa99f1171694d | |
parent | 02a402e22b8479437067cac52339d4dc0ae56321 (diff) | |
download | pylint-git-cce4e0578aa4806a31a0a00268bf0b1497d955b9.tar.gz |
Create a file for ManPageFormatter
-rw-r--r-- | pylint/config/__init__.py | 151 | ||||
-rw-r--r-- | pylint/config/man_help_formatter.py | 139 |
2 files changed, 149 insertions, 141 deletions
diff --git a/pylint/config/__init__.py b/pylint/config/__init__.py index 6bb118cac..f87a7360f 100644 --- a/pylint/config/__init__.py +++ b/pylint/config/__init__.py @@ -48,13 +48,13 @@ import os import pickle import re import sys -import time from typing import Any, Dict, Tuple import toml from pylint import utils from pylint.config.find_default_config_files import find_default_config_files +from pylint.config.man_help_formatter import _ManHelpFormatter USER_HOME = os.path.expanduser("~") if "PYLINTHOME" in os.environ: @@ -355,130 +355,6 @@ class OptionParser(optparse.OptionParser): return opt -# pylint: disable=abstract-method; by design? -class _ManHelpFormatter(optparse.HelpFormatter): - def __init__( - self, indent_increment=0, max_help_position=24, width=79, short_first=0 - ): - optparse.HelpFormatter.__init__( - self, indent_increment, max_help_position, width, short_first - ) - - def format_heading(self, heading): - return ".SH %s\n" % heading.upper() - - def format_description(self, description): - return description - - def format_option(self, option): - try: - optstring = option.option_strings - except AttributeError: - optstring = self.format_option_strings(option) - if option.help: - help_text = self.expand_default(option) - help_string = " ".join([l.strip() for l in help_text.splitlines()]) - help_string = help_string.replace("\\", "\\\\") - help_string = help_string.replace("[current:", "[default:") - else: - help_string = "" - return """.IP "%s" -%s -""" % ( - optstring, - help_string, - ) - - def format_head(self, optparser, pkginfo, section=1): - long_desc = "" - try: - pgm = optparser._get_prog_name() - except AttributeError: - # py >= 2.4.X (dunno which X exactly, at least 2) - pgm = optparser.get_prog_name() - short_desc = self.format_short_description(pgm, pkginfo.description) - if hasattr(pkginfo, "long_desc"): - long_desc = self.format_long_description(pgm, pkginfo.long_desc) - return "%s\n%s\n%s\n%s" % ( - self.format_title(pgm, section), - short_desc, - self.format_synopsis(pgm), - long_desc, - ) - - @staticmethod - def format_title(pgm, section): - date = "%d-%02d-%02d" % time.localtime()[:3] - return '.TH %s %s "%s" %s' % (pgm, section, date, pgm) - - @staticmethod - def format_short_description(pgm, short_desc): - return """.SH NAME -.B %s -\\- %s -""" % ( - pgm, - short_desc.strip(), - ) - - @staticmethod - def format_synopsis(pgm): - return ( - """.SH SYNOPSIS -.B %s -[ -.I OPTIONS -] [ -.I <arguments> -] -""" - % pgm - ) - - @staticmethod - def format_long_description(pgm, long_desc): - long_desc = "\n".join(line.lstrip() for line in long_desc.splitlines()) - long_desc = long_desc.replace("\n.\n", "\n\n") - if long_desc.lower().startswith(pgm): - long_desc = long_desc[len(pgm) :] - return """.SH DESCRIPTION -.B %s -%s -""" % ( - pgm, - long_desc.strip(), - ) - - @staticmethod - def format_tail(pkginfo): - tail = """.SH SEE ALSO -/usr/share/doc/pythonX.Y-%s/ - -.SH BUGS -Please report bugs on the project\'s mailing list: -%s - -.SH AUTHOR -%s <%s> -""" % ( - getattr(pkginfo, "debian_name", "pylint"), - pkginfo.mailinglist, - pkginfo.author, - pkginfo.author_email, - ) - - if hasattr(pkginfo, "copyright"): - tail += ( - """ -.SH COPYRIGHT -%s -""" - % pkginfo.copyright - ) - - return tail - - class OptionsManagerMixIn: """Handle configuration from both a configuration file and command line options""" @@ -645,15 +521,17 @@ class OptionsManagerMixIn: ) printed = True - def generate_manpage(self, pkginfo, section=1, stream=None): + def generate_manpage(self, pkginfo, section=1, stream=sys.stdout): with _patch_optparse(): - _generate_manpage( - self.cmdline_parser, - pkginfo, - section, - stream=stream or sys.stdout, - level=self._maxlevel, + formatter = _ManHelpFormatter() + formatter.output_level = self._maxlevel + formatter.parser = self.cmdline_parser + print( + formatter.format_head(self.cmdline_parser, pkginfo, section), + file=stream, ) + print(self.cmdline_parser.format_option_help(formatter), file=stream) + print(formatter.format_tail(pkginfo), file=stream) def load_provider_defaults(self): """initialize configuration using default values""" @@ -902,12 +780,3 @@ class ConfigurationMixIn(OptionsManagerMixIn, OptionsProviderMixIn): if gdef not in self.option_groups: self.option_groups.append(gdef) self.register_options_provider(self, own_group=False) - - -def _generate_manpage(optparser, pkginfo, section=1, stream=sys.stdout, level=0): - formatter = _ManHelpFormatter() - formatter.output_level = level - formatter.parser = optparser - print(formatter.format_head(optparser, pkginfo, section), file=stream) - print(optparser.format_option_help(formatter), file=stream) - print(formatter.format_tail(pkginfo), file=stream) diff --git a/pylint/config/man_help_formatter.py b/pylint/config/man_help_formatter.py new file mode 100644 index 000000000..4c4607c18 --- /dev/null +++ b/pylint/config/man_help_formatter.py @@ -0,0 +1,139 @@ +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/master/COPYING + +import optparse +import sys +import time + + +# pylint: disable=abstract-method; by design? +class _ManHelpFormatter(optparse.HelpFormatter): + def __init__( + self, indent_increment=0, max_help_position=24, width=79, short_first=0 + ): + optparse.HelpFormatter.__init__( + self, indent_increment, max_help_position, width, short_first + ) + + def format_heading(self, heading): + return ".SH %s\n" % heading.upper() + + def format_description(self, description): + return description + + def format_option(self, option): + try: + optstring = option.option_strings + except AttributeError: + optstring = self.format_option_strings(option) + if option.help: + help_text = self.expand_default(option) + help_string = " ".join([l.strip() for l in help_text.splitlines()]) + help_string = help_string.replace("\\", "\\\\") + help_string = help_string.replace("[current:", "[default:") + else: + help_string = "" + return """.IP "%s" +%s +""" % ( + optstring, + help_string, + ) + + def format_head(self, optparser, pkginfo, section=1): + long_desc = "" + try: + pgm = optparser._get_prog_name() + except AttributeError: + # py >= 2.4.X (dunno which X exactly, at least 2) + pgm = optparser.get_prog_name() + short_desc = self.format_short_description(pgm, pkginfo.description) + if hasattr(pkginfo, "long_desc"): + long_desc = self.format_long_description(pgm, pkginfo.long_desc) + return "%s\n%s\n%s\n%s" % ( + self.format_title(pgm, section), + short_desc, + self.format_synopsis(pgm), + long_desc, + ) + + @staticmethod + def format_title(pgm, section): + date = "%d-%02d-%02d" % time.localtime()[:3] + return '.TH %s %s "%s" %s' % (pgm, section, date, pgm) + + @staticmethod + def format_short_description(pgm, short_desc): + return """.SH NAME +.B %s +\\- %s +""" % ( + pgm, + short_desc.strip(), + ) + + @staticmethod + def format_synopsis(pgm): + return ( + """.SH SYNOPSIS +.B %s +[ +.I OPTIONS +] [ +.I <arguments> +] +""" + % pgm + ) + + @staticmethod + def format_long_description(pgm, long_desc): + long_desc = "\n".join(line.lstrip() for line in long_desc.splitlines()) + long_desc = long_desc.replace("\n.\n", "\n\n") + if long_desc.lower().startswith(pgm): + long_desc = long_desc[len(pgm) :] + return """.SH DESCRIPTION +.B %s +%s +""" % ( + pgm, + long_desc.strip(), + ) + + @staticmethod + def format_tail(pkginfo): + tail = """.SH SEE ALSO +/usr/share/doc/pythonX.Y-%s/ + +.SH BUGS +Please report bugs on the project\'s mailing list: +%s + +.SH AUTHOR +%s <%s> +""" % ( + getattr(pkginfo, "debian_name", "pylint"), + pkginfo.mailinglist, + pkginfo.author, + pkginfo.author_email, + ) + + if hasattr(pkginfo, "copyright"): + tail += ( + """ +.SH COPYRIGHT +%s +""" + % pkginfo.copyright + ) + + return tail + + +def _generate_manpage(optparser, pkginfo, section=1, stream=sys.stdout, level=0): + formatter = _ManHelpFormatter() + formatter.output_level = level + formatter.parser = optparser + print(formatter.format_head(optparser, pkginfo, section), file=stream) + print(optparser.format_option_help(formatter), file=stream) + print(formatter.format_tail(pkginfo), file=stream) |