File: manpage_from_python.py

package info (click to toggle)
osm2pgsql 1.8.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,536 kB
  • sloc: cpp: 46,707; ansic: 1,804; python: 797; sh: 25; makefile: 14
file content (55 lines) | stat: -rw-r--r-- 1,853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of osm2pgsql (https://osm2pgsql.org/).
#
# Copyright (C) 2023 by the osm2pgsql developer community.
# For a full list of authors see the git log.
""" Create a man page for osm2pgsql helper scripts in python.
"""
from argparse import ArgumentParser, RawDescriptionHelpFormatter
import re

from build_manpages.manpage import Manpage
from build_manpages.build_manpage import get_parser_from_file

SEE_ALSO = [
 "osm2pgsql website (https://osm2pgsql.org)",
 "osm2pgsql manual (https://osm2pgsql.org/doc/manual.html)"
]

def create_manpage_for(args):
    """ Create a man page for the given script.
    """
    parser = get_parser_from_file(args.script, 'get_parser', 'function')
    parser.man_short_description = args.description
    parser._manpage = [
      {'heading': 'SEE ALSO',
       'content': '\n'.join(f"* {s}" for s in SEE_ALSO)}
    ]

    manpage = str(Manpage(parser))
    manpage = re.sub(r'.TH.*',
                     f'.TH "{parser.prog.upper()}" "1" "{args.version}" "" ""',
                     manpage)
    # Correct quoting for single quotes. See groff manpage.
    manpage = manpage.replace('`', '\\(cq')

    return manpage


if __name__ == "__main__":
    parser = ArgumentParser(usage='%(prog)s [options] <script>',
                            formatter_class=RawDescriptionHelpFormatter,
                            description=__doc__)
    parser.add_argument('--version', default="Manual",
                        help='Version of the software')
    parser.add_argument('--description',
                        help='Short description added to the name')
    parser.add_argument('script',
                        help='Name of the script to generate the manpage for.')

    args = parser.parse_args()

    manpage = create_manpage_for(args)

    print(manpage)