Skip to content

Commit

Permalink
Manpage: don't require the data= option
Browse files Browse the repository at this point in the history
The definition of the data= is blurry, and it's better to not use it
from the outside, it could change.  Try to hide it from users (make it
optional and private), and actually revert a change that the old
Manpage() API.

Relates: #7
  • Loading branch information
praiskup committed Oct 27, 2022
1 parent 9d84d97 commit 103fb53
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion argparse_manpage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ def main():

parser = get_parser(import_type, import_from, obj_name, obj_type, prog=args.prog)
data = args_to_manpage_data(args)
manpage = Manpage(parser, data, args.format)
manpage = Manpage(parser, format=args.format, _data=data)
write_to_filename(str(manpage), args.outfile)
37 changes: 24 additions & 13 deletions argparse_manpage/manpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def get_manpage_data_from_distribution(distribution, data):

def _get_footer_lines(data):
ret = []
project_name = data["project_name"]
authors = data["authors"]
url = data["url"]
project_name = data.get("project_name", "")
authors = data.get("authors")
url = data.get("url")

needs_separator = False
if authors:
Expand Down Expand Up @@ -121,40 +121,51 @@ def get_footer(data):
return "\n".join(_get_footer_lines(data)) + "\n"


# This is already considered an API, and seems like a valid scenario:
# https://github.com/pypa/pipx/blob/fd6650bcaeca3088/scripts/generate_man.py

class Manpage(object):
# pylint: disable=too-many-instance-attributes
def __init__(self, parser, data, format='pretty'):
def __init__(self, parser, _data=None, format='pretty'):
"""
Manual page abstraction. Generates, with the help of formater, a manual
page by __str__() method. Please avoid using the private _data
argument (see https://github.com/praiskup/argparse-manpage/issues/7),
instead override the `self.<ATTRIBUTE>` when needed.
"""
self.prog = parser.prog
self.parser = parser
self.format = format
self.data = data
self._data = _data or {}
if not getattr(parser, '_manpage', None):
self.parser._manpage = []

self.formatter = self.parser._get_formatter()
self.mf = _ManpageFormatter(self.prog, self.formatter, format=self.format)
self.synopsis = self.parser.format_usage().split(':')[-1].split()

self.date = self.data.get("date")
self.date = self._data.get("date")
if not self.date:
self.date = datetime.datetime.now().strftime('%Y-%m-%d')

self.source = self.data.get("project_name")
self.source = self._data.get("project_name")
if not self.source:
self.source = self.prog

version = self.data.get("version")
version = self._data.get("version")
if version:
self.source += " " + str(version)

self.manual = self.data.get("manual_title")
self.manual = self._data.get("manual_title")
if not self.manual:
self.manual = "Generated Python Manual"

self.section = self.data.get("manual_section")
self.section = self._data.get("manual_section")
if not self.section:
self.section = 1

self.description = self._data.get("description")

def format_text(self, text):
# Wrap by parser formatter and convert to manpage format
return self.mf.format_text(self.formatter._format_text(text)).strip('\n')
Expand Down Expand Up @@ -182,8 +193,8 @@ def __str__(self):
# Let's keep this undocumented. There's a way to specify this in
# setup.cfg: 'description'
description = self.parser.man_short_description
if self.data.get("description"):
description = self.data["description"]
if self.description:
description = self.description
if description:
line += " - " + description
lines.append(_markup(line))
Expand All @@ -207,7 +218,7 @@ def __str__(self):
lines.append(self.format_text(section['content']))

lines.append("")
lines.extend(self.mf.format_footer(self.data))
lines.extend(self.mf.format_footer(self._data))
return "\n".join(lines).strip("\n") + "\n"


Expand Down
2 changes: 1 addition & 1 deletion build_manpages/build_manpages.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def run(self):
parser = get_parser(data['import_type'], data['import_from'], data['objname'], data['objtype'], data.get('prog', None))
format = data.get('format', 'pretty')
if format in ('pretty', 'single-commands-section'):
manpage = Manpage(parser, data, format)
manpage = Manpage(parser, format=format, _data=data)
write_to_filename(str(manpage), page)
elif format == 'old':
# TODO: drop the "old" format support, and stop depending on ManPageWriter
Expand Down
13 changes: 3 additions & 10 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@


class Tests(unittest.TestCase):

_fake_data = {
"project_name": "argparse-manpage-testsuite",
"authors": None,
"url": None,
}

def test_backslash_escape(self):
parser = argparse.ArgumentParser('duh')
parser.add_argument("--jej", help="c:\\something")
man = Manpage(parser, self._fake_data)
man = Manpage(parser)
assert 'c:\\\\something' in str(man).split('\n')
assert '.SH OPTIONS' in str(man).split('\n')

Expand All @@ -33,7 +26,7 @@ def test_argument_groups(self):
parser2 = parser1.add_argument_group('g2')
parser2.add_argument("--jej2", help="c:\\something")
parser2.add_argument("--else", help="c:\\something")
man = Manpage(parser1, self._fake_data)
man = Manpage(parser1)
self.assertIn('.SH G1', str(man).split('\n'))
self.assertIn('.SH G2', str(man).split('\n'))
self.assertNotIn('.SH OPTIONS', str(man).split('\n'))
Expand All @@ -50,7 +43,7 @@ def test_aliases(self):
"provided "
)

manpage_lines = str(Manpage(parser, self._fake_data)).split("\n")
manpage_lines = str(Manpage(parser)).split("\n")
exp_line = '\\fBaliases_test\\fR \\fI\\,list\\/\\fR'
not_exp_line = '\\fBaliases_test\\fR \\fI\\,ls\\/\\fR'
assert exp_line in manpage_lines
Expand Down

0 comments on commit 103fb53

Please sign in to comment.