#!/usr/bin/env python3 # # Copyright © 2019-2020 Adélie Linux team. All rights reserved. # NCSA license. # import datetime 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""" APKINDEX analysis - %(date)s

APKINDEX analysis - %(date)s

Base URL: %(url)s
Repositories: %(repos)s
Architectures: %(arches)s

""" _FOOTER = r""" """ 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 % { "url": opts.url, "repos": ", ".join(opts.repos), "arches": ", ".join(opts.arches), "date": datetime.datetime.now().strftime("%c"), }) first = True for row in rows: if first: print("") print( "", sep="" ) print("") 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("", sep="") if not first: print("") print("
", "".join(row), "
", "
") print(_FOOTER) FORMATTERS = { "tab": format_tab, "pretty": format_pretty, "html": format_html, }