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