aboutsummaryrefslogtreecommitdiffstats
path: root/genconfig.py
blob: 48c1b3e60b4ed88cbbb5c403770631f947d2d62e (plain)
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
#!/usr/bin/env python3
# generate man config documentation from mcelog.conf example
# genconfig.py mcelog.conf intro.html
import sys
import re
import argparse

ap = argparse.ArgumentParser(description="generate man config documentation from mcelog.conf example")
ap.add_argument('config', type=argparse.FileType('r'), help="mcelog example config file")
ap.add_argument('intro', type=argparse.FileType('r'), help="intro file")
args = ap.parse_args()

def parse(f):
  lineno = 1
  explanation = 0
  header = 1
  for line in f:
    lineno += 1

    # skip first comment
    if header:
      if not re.match('^#', line):
        header = 0
      continue

    # explanation
    m = re.match('^#\s(.*)', line)
    if m:
      explanation += 1
      s = m.group(1)
      if explanation == 1:
        s = s.capitalize()
      print(s)
      continue

    if explanation:
      print(".PP")
      explanation = 0

    # empty line: new option
    if re.match('\s+', line):
      new_option()
      continue
    # group
    m = re.match('\[(.*)\]', line)
    if m:
      start_group(m.group(1))
      continue
    # config option
    m = re.match('^(#?)([a-z-]+) = (.*)', line)
    if m:
      config_option(m.group(1), m.group(2), m.group(3))
      continue
    print("Unparseable line %d" % (lineno-1), file=sys.stderr, flush=True)

def config_option(enabled, name, value):
    print(".B %s = %s" % (name, value))
    print(".PP")

def start_group(name):
    print(".SS \"The %s config section\"" % (name))

def new_option():
    print(".PP")


print("""
.\\" Auto generated mcelog.conf manpage. Do not edit.
.TH "mcelog.conf" 5 "mcelog"
""")

print(args.intro.read())
parse(args.config)
print("""
.SH SEE ALSO
.BR mcelog (8),
.BR mcelog.triggers (5)
.B http://www.mcelog.org
""")