1#!/usr/bin/env python3
2import re
3from collections import defaultdict
4from subprocess import check_output
5
6README_FILE = "README.md"
7
8lines = check_output(["chroma", "--list"]).decode("utf-8").splitlines()
9lines = [line.strip() for line in lines if line.startswith(" ") and not line.startswith(" ")]
10lines = sorted(lines, key=lambda l: l.lower())
11
12table = defaultdict(list)
13
14for line in lines:
15 table[line[0].upper()].append(line)
16
17rows = []
18for key, value in table.items():
19 rows.append("{} | {}".format(key, ", ".join(value)))
20tbody = "\n".join(rows)
21
22with open(README_FILE, "r") as f:
23 content = f.read()
24
25with open(README_FILE, "w") as f:
26 marker = re.compile(r"(?P<start>:----: \\| --------\n).*?(?P<end>\n\n)", re.DOTALL)
27 replacement = r"\g<start>%s\g<end>" % tbody
28 updated_content = marker.sub(replacement, content)
29 f.write(updated_content)
30
31print(tbody)