File: printdoc.py

package info (click to toggle)
broctl 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,220 kB
  • sloc: python: 4,931; sh: 1,188; makefile: 70; awk: 24
file content (89 lines) | stat: -rw-r--r-- 2,444 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
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
from __future__ import print_function
import sys

from BroControl import options
from BroControl import doc
from BroControl import plugin
from BroControl import node

# Prints the command docstrings in a form suitable for direct inclusion
# into the documentation.
def print_commands(cls):
    cmds = []

    for i in cls.__dict__:
        docstr = cls.__dict__[i].__doc__
        if i.startswith("do_") and docstr:
            cmds += [(i[3:], docstr)]

    cmds.sort()

    for (cmd, docstr) in cmds:
        if docstr.startswith("- "):
            # First line are arguments.
            docstr = docstr.splitlines()
            args = docstr[0][2:]
            docstr = "\n".join(docstr[1:])
        else:
            args = ""

        if args:
            args = (" *%s*" % args)
        else:
            args = ""

        output = ""
        for line in docstr.splitlines():
            output += "    %s\n" % line.strip()

        output = output.strip()

        print()
        print(".. _%s:\n\n*%s*%s\n    %s" % (cmd, cmd, args, output))
        print()


# Print options documentation.
def print_options():
    print("User Options")
    print("~~~~~~~~~~~~")

    out, err = options.print_options(options.Option.USER)
    print(out, end="")
    if err:
        print(err, file=sys.stderr)

    print()
    print("Internal Options")
    print("~~~~~~~~~~~~~~~~")
    print()

    out, err = options.print_options(options.Option.AUTOMATIC)
    print(out, end="")
    if err:
        print(err, file=sys.stderr)


# Print plugin and node documentation.
def print_plugin():
    print(doc.print_class(plugin.Plugin, tag="no-methods"), end="")
    print(doc.print_class(plugin.Plugin, header=False), end="")
    print(doc.print_class(plugin.Plugin, "override", header=False), end="")

    print(doc.print_class(node.Node), end="")

def print_broctl_docs(mainpath, broctlclass):
    print(".. Autogenerated. Do not edit.\n")
    with open(mainpath, "r") as f:
        for line in f:
            fields = line.strip().split(None, 2)
            if len(fields) == 3 and fields[0] == ".." and fields[1] == "include::":
                if fields[2] == "commands.rst":
                    print_commands(broctlclass)
                elif fields[2] == "options.rst":
                    print_options()
                elif fields[2] == "plugins.rst":
                    print_plugin()
            else:
                print(line, end="")