aboutsummaryrefslogtreecommitdiffstats
path: root/sos/report/reporting.py
blob: fc7b56a56fa06d51bd1f2d7a9105082f54aae81e (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# Copyright (C) 2014 Red Hat, Inc.,
#   Bryn M. Reeves <bmr@redhat.com>
#
# This file is part of the sos project: https://github.com/sosreport/sos
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# version 2 of the GNU General Public License.
#
# See the LICENSE file in the source distribution for further information.

""" This provides a restricted tag language to define the sosreport
    index/report
"""

try:
    import json
except ImportError:
    import simplejson as json


class Node(object):

    def __str__(self):
        return json.dumps(self.data)

    def can_add(self, node):
        return False


class Leaf(Node):
    """Marker class that can be added to a Section node"""
    pass


class Report(Node):
    """The root element of a report. This is a container for sections."""

    def __init__(self):
        self.data = {}

    def can_add(self, node):
        return isinstance(node, Section)

    def add(self, *nodes):
        for node in nodes:
            if self.can_add(node):
                self.data[node.name] = node.data


def _decode(s):
    """returns a string text for a given unicode/str input"""
    return (s if isinstance(s, str) else s.decode('utf8', 'ignore'))


class Section(Node):
    """A section is a container for leaf elements. Sections may be nested
    inside of Report objects only."""

    def __init__(self, name):
        self.name = _decode(name)
        self.data = {}

    def can_add(self, node):
        return isinstance(node, Leaf)

    def add(self, *nodes):
        for node in nodes:
            if self.can_add(node):
                self.data.setdefault(node.ADDS_TO, []).append(node.data)


class Command(Leaf):

    ADDS_TO = "commands"

    def __init__(self, name, return_code, href):
        self.data = {"name": _decode(name),
                     "return_code": return_code,
                     "href": _decode(href)}


class CopiedFile(Leaf):

    ADDS_TO = "copied_files"

    def __init__(self, name, href):
        self.data = {"name": _decode(name),
                     "href": _decode(href)}


class CreatedFile(Leaf):

    ADDS_TO = "created_files"

    def __init__(self, name, href):
        self.data = {"name": _decode(name),
                     "href": _decode(href)}


class Alert(Leaf):

    ADDS_TO = "alerts"

    def __init__(self, content):
        self.data = _decode(content)


class Note(Leaf):

    ADDS_TO = "notes"

    def __init__(self, content):
        self.data = _decode(content)


def ends_bs(string):
    """ Return True if 'string' ends with a backslash, and False otherwise.

        Define this as a named function for no other reason than that pep8
        now forbids binding of a lambda expression to a name:

        'E731 do not assign a lambda expression, use a def'
    """
    return string.endswith('\\')


class PlainTextReport(object):
    """Will generate a plain text report from a top_level Report object"""

    HEADER = ""
    FOOTER = ""
    LEAF = "  * %(name)s"
    ALERT = "  ! %s"
    NOTE = "  * %s"
    PLUGLISTHEADER = "Loaded Plugins:"
    PLUGLISTITEM = "  {name}"
    PLUGLISTSEP = "\n"
    PLUGLISTMAXITEMS = 5
    PLUGLISTFOOTER = ""
    PLUGINFORMAT = "{name}"
    PLUGDIVIDER = "=" * 72

    subsections = (
        (Command, LEAF,      "-  commands executed:", ""),
        (CopiedFile, LEAF,   "-  files copied:",      ""),
        (CreatedFile, LEAF,  "-  files created:",     ""),
        (Alert, ALERT,       "-  alerts:",            ""),
        (Note, NOTE,         "-  notes:",             ""),
    )

    line_buf = []

    def __init__(self, report_node):
        self.report_data = sorted(dict.items(report_node.data))

    def unicode(self):
        self.line_buf = line_buf = []

        if (len(self.HEADER) > 0):
            line_buf.append(self.HEADER)

        # generate section/plugin list, split long list to multiple lines
        line_buf.append(self.PLUGLISTHEADER)
        line = ""
        i = 0
        plugcount = len(self.report_data)
        for section_name, _ in self.report_data:
            line += f"  {section_name}"
            i += 1
            if (i % self.PLUGLISTMAXITEMS == 0) and (i < plugcount):
                line += self.PLUGLISTSEP
        line += self.PLUGLISTFOOTER
        line_buf.append(line)

        for section_name, section_contents in self.report_data:
            line_buf.append(self.PLUGDIVIDER)
            line_buf.append(f"{section_name}")
            for type_, format_, header, footer in self.subsections:
                self.process_subsection(section_contents, type_.ADDS_TO,
                                        header, format_, footer)

        if (len(self.FOOTER) > 0):
            line_buf.append(self.FOOTER)

        output = u'\n'.join(map(lambda i: (i if isinstance(i, str)
                                           else i.decode('utf8', 'ignore')),
                                line_buf))
        return output

    def process_subsection(self, section, key, header, format_, footer):
        if key in section:
            self.line_buf.append(header)
            for item in sorted(
                    section.get(key),
                    key=lambda x: x["name"] if isinstance(x, dict) else ''
            ):
                self.line_buf.append(format_ % item)
            if (len(footer) > 0):
                self.line_buf.append(footer)


class HTMLReport(PlainTextReport):
    """Will generate a HTML report from a top_level Report object"""

    HEADER = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        <head>
            <meta http-equiv="Content-Type" content="text/html;
                  charset=utf-8" />
            <title>Sos System Report</title>
            <style type="text/css">
                td {
                    padding: 0 5px;
                   }
            </style>
        </head>
        <body>\n"""
    FOOTER = "</body></html>"
    LEAF = '<li><a href="%(href)s">%(name)s</a></li>'
    ALERT = "<li>%s</li>"
    NOTE = "<li>%s</li>"
    PLUGLISTHEADER = "<h3>Loaded Plugins:</h3><table><tr>"
    PLUGLISTITEM = '<td><a href="#{name}">{name}</a></td>\n'
    PLUGLISTSEP = "</tr>\n<tr>"
    PLUGLISTMAXITEMS = 5
    PLUGLISTFOOTER = "</tr></table>"
    PLUGINFORMAT = '<h2 id="{name}">Plugin <em>{name}</em></h2>'
    PLUGDIVIDER = "<hr/>\n"

    subsections = (
        (Command, LEAF,      "<p>Commands executed:</p><ul>", "</ul>"),
        (CopiedFile, LEAF,   "<p>Files copied:</p><ul>",      "</ul>"),
        (CreatedFile, LEAF,  "<p>Files created:</p><ul>",     "</ul>"),
        (Alert, ALERT,       "<p>Alerts:</p><ul>",            "</ul>"),
        (Note, NOTE,         "<p>Notes:</p><ul>",             "</ul>"),
    )


class JSONReport(PlainTextReport):
    """Will generate a JSON report from a top_level Report object"""

    def unicode(self):
        output = json.dumps(self.report_data, indent=4, ensure_ascii=False)
        return output

# vim: set et ts=4 sw=4 :