File: gendocs.py

package info (click to toggle)
cmake-format 0.6.13-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: python: 16,990; makefile: 14
file content (137 lines) | stat: -rw-r--r-- 3,467 bytes parent folder | download | duplicates (4)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
"""
Generate linter documentation page
"""

from __future__ import unicode_literals

import argparse
import io
import logging
import os
import sys
import textwrap

import cmakelang
from cmakelang.lint import lintdb
from tangent.tooling.gendoc import format_directive

HEADER = """
.. _lint-checks:

===================
Lint Code Reference
===================

"""


def setup_argparse(argparser):
  argparser.add_argument('-v', '--version', action='version',
                         version=cmakelang.__version__)
  argparser.add_argument(
      '-l', '--log-level', default="info",
      choices=["error", "warning", "info", "debug"])

  argparser.add_argument(
      '-o', '--outfile-path', default=None,
      help='Write output to this file. Default is stdout.')

  argparser.add_argument(
      '-c', '--config-files', nargs='+',
      help='path to configuration file(s)')
  argparser.add_argument('infilepaths', nargs='*')
  argparser.add_argument("command", choices=["reference", "table"])


def write_title(outfile, title, rulerchar=None, numrule=1):
  if rulerchar is None:
    rulerchar = '-'

  if numrule == 2:
    outfile.write(rulerchar * len(title))
    outfile.write("\n")
  outfile.write(title)
  outfile.write("\n")
  outfile.write(rulerchar * len(title))
  outfile.write("\n\n")


def write_ruler(outfile):
  outfile.write("+")
  outfile.write("-" * 7)
  outfile.write("+")
  outfile.write("-" * 68)
  outfile.write("+")
  outfile.write("\n")


def write_cell(outfile, idstr, msgfmt):
  lines = textwrap.wrap(msgfmt, width=66)
  outfile.write("|`{:5s}`| {:66s} |\n".format(idstr, lines.pop(0)))
  for line in lines:
    outfile.write("| {:5s} | {:66s} |\n".format("", line))


def gen_table(outfile):

  write_ruler(outfile)
  for idstr, msgfmt, _ in lintdb.LINT_DB:
    write_cell(outfile, idstr, msgfmt)
    write_ruler(outfile)
  outfile.write("\n")


def gen_reference(outfile):
  outfile.write(HEADER)
  for idstr, msgfmt, kwargs in lintdb.LINT_DB:
    # outfile.write(".. _{}:\n".format(idstr.lower()))
    outfile.write(".. _{}:\n\n".format(idstr))
    write_title(outfile, idstr, numrule=2)
    write_title(outfile, "message")
    outfile.write(format_directive(msgfmt))
    outfile.write("\n\n")
    description = kwargs.pop("description", None)
    if description:
      write_title(outfile, "description")
      outfile.write(description)
      outfile.write("\n\n")

    explain = kwargs.pop("explain", None)
    if explain:
      write_title(outfile, "explanation")
      outfile.write(explain)
      outfile.write("\n\n")


def main():
  """Parse arguments, open files, start work."""

  argparser = argparse.ArgumentParser(
      description=__doc__,
      formatter_class=argparse.RawDescriptionHelpFormatter)

  setup_argparse(argparser)
  args = argparser.parse_args()
  logging.getLogger().setLevel(getattr(logging, args.log_level.upper()))

  if args.outfile_path is None:
    args.outfile_path = '-'

  if args.outfile_path == '-':
    outfile = io.open(os.dup(sys.stdout.fileno()),
                      mode='w', encoding="utf-8", newline='')
  else:
    outfile = io.open(args.outfile_path, 'w', encoding="utf-8", newline='')

  if args.command == "reference":
    gen_reference(outfile)
  elif args.command == "table":
    gen_table(outfile)
  outfile.close()
  return 0


if __name__ == "__main__":
  logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
  sys.exit(main())