proposal_list.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Provides a list of proposal files."""
  2. __copyright__ = """
  3. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  4. Exceptions. See /LICENSE for license information.
  5. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. """
  7. import os
  8. import re
  9. import sys
  10. from typing import List, Tuple
  11. def get_title(parent_dir: str, entry: str) -> str:
  12. """Returns the title from the requested file."""
  13. path = os.path.join(parent_dir, entry)
  14. with open(path) as md:
  15. titles = [t for t in md.readlines() if t.startswith("# ")]
  16. if not titles:
  17. sys.exit("%r is missing a title." % path)
  18. return titles[0][2:-1]
  19. def get_path() -> str:
  20. return os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
  21. def get_list(proposals_path: str) -> List[Tuple[str, str]]:
  22. proposals = []
  23. proposals_list = os.listdir(proposals_path)
  24. proposals_list.sort()
  25. for f in proposals_list:
  26. match = re.match(r"^p([0-9]{4})\.md$", f)
  27. if not match:
  28. continue
  29. number = match[1]
  30. title = get_title(proposals_path, f)
  31. proposals.append(("%s - %s" % (number, title), f))
  32. return proposals