summaryrefslogtreecommitdiff
path: root/output.py
blob: c8bb542db853454f0bb9636b77165b2e2ddccf41 (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
#!/usr/bin/env python3
#
# Copyright © 2019-2020 Adélie Linux team.  All rights reserved.
# NCSA license.
#
import functools
import subprocess

@functools.total_ordering
class _Cell:
    def __init__(self, content):
        self.content = content
    def __eq__(self, other):
        return self.content == other.content
    def __lt__(self, other):
        return self.content < other.content
    def __hash__(self):
        return hash(self.content)

_ANSI_CLEAR = "\033[0;00m"

class Yes(_Cell):
    symbol = "✓"
    cls = "cell-yes"
    color = "\033[1;32m"

class No(_Cell):
    symbol = "✗"
    cls = "cell-no"
    color = "\033[1;31m"

class Partial(_Cell):
    symbol = "?"
    cls = "cell-partial"
    color = "\033[1;33m"

PARTIAL_MISSING = Partial("missing")
YES_MISSING = Yes("missing")

_HEADER = r"""<!DOCTYPE html>
<html>
 <head>
  <title>APKINDEX analysis - {date}</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <style type="text/css">
   table {
    background-color: #F8F9FA;
    color: #222;
    margin: 1em 0;
    border: 1px solid #A2A9B1;
    border-collapse: collapse;
   }
   td, th {
    border: 1px solid #A2A9B1;
    padding: 0.2em 0.4em;
   }
   th {
    background-color: #EAECF0;
    text-align: center;
    position: sticky;
    top: 0;
   }
   .cell-yes { background-color: #DFF0D8; color: black; }
   .cell-no { background-color: #F2DEDE; color: black; }
   .cell-partial { background: #FFF176; color: black; }
  </style>
 </head>
 <body>
  <h1>APKINDEX analysis - {date}</h1>
  <p>
   Base URL: <a href="{url}">{url}</a><br>
   Repositories: {repos}<br>
   Architectures: {arches}<br>
  </p>
"""
_FOOTER = r""" </body>
</html>
"""

def format_tab(_, rows):
    for row in rows:
        for i, j in enumerate(row):
            if isinstance(j, _Cell):
                row[i] = f"{j.symbol} {j.content}"

        print(*row, sep="\t")

def format_pretty(_, rows):
    proc = subprocess.Popen(
        ("column", "-ts", "\t"),
        stdin=subprocess.PIPE,
        encoding="utf-8",
    )

    for row in rows:
        for i, j in enumerate(row):
            if isinstance(j, _Cell):
                row[i] = f"{j.color}{j.symbol} {j.content}{_ANSI_CLEAR}"
            else:
                # Work around column(1) not liking ANSI escapes by
                # always printing some
                row[i] = f"{_ANSI_CLEAR}{j}{_ANSI_CLEAR}"

        print(*row, file=proc.stdin, sep="\t")

    proc.communicate()

def format_html(opts, rows):
    print(_HEADER)

    first = True
    for row in rows:
        if first:
            print("<table>")
            print(
                "<thead><tr><th>",
                "</th><th>".join(row),
                "</th></tr></thead>", sep=""
            )
            print("<tbody>")
            first = False
            continue

        for i, j in enumerate(row[1:]):
            i += 1

            if isinstance(j, _Cell):
                row[i] = f" class='{j.cls}'>{j.symbol} {j.content}"
            else:
                row[i] = ">" + j

        print("<tr><td>", "</td><td".join(row), "</td></tr>", sep="")

    if not first:
        print("</tbody>")
        print("</table>")

    print(_FOOTER)

FORMATTERS = {
    "tab": format_tab,
    "pretty": format_pretty,
    "html": format_html,
}