|
| 1 | +import re |
| 2 | +import requests |
| 3 | +from dataclasses import dataclass |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +REPO_CONTENTS = "https://api.github.com/repos/conda-forge/cfep/contents/" |
| 7 | +TITLE_PATTERN = "<td>\s*Title\s*</td><td>\s*(.*)\s*</td>" |
| 8 | +STATUS_PATTERN = "<td>\s*Status\s*</td><td>\s*(.*)\s*</td>" |
| 9 | +REPO_DIR = Path(__file__).parents[1].absolute() |
| 10 | +CFEP_INDEX_RST = REPO_DIR / "src" / "orga" / "cfep-index.rst" |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class Cfep: |
| 15 | + filename: str |
| 16 | + title: str |
| 17 | + status: str |
| 18 | + html_url: str |
| 19 | + |
| 20 | + @property |
| 21 | + def name(self) -> str: |
| 22 | + return self.filename.replace(".md", "") |
| 23 | + |
| 24 | + def rst_link(self) -> str: |
| 25 | + clean_title = self.title.replace("`", "") |
| 26 | + return f"`{self.name.upper()}: {clean_title} <{self.html_url}>`_ -- *{self.status}*" |
| 27 | + |
| 28 | + def md_link(self) -> str: |
| 29 | + return ( |
| 30 | + f"[{self.name.upper()}: {self.title}]({self.html_url}) -- *{self.status}*" |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def get_cfeps(): |
| 35 | + """Generator that returns all CFEPs from GitHub repo""" |
| 36 | + response = requests.get( |
| 37 | + REPO_CONTENTS, headers={"Accept": "application/vnd.github.v3+json"} |
| 38 | + ) |
| 39 | + response.raise_for_status() |
| 40 | + for content in response.json(): |
| 41 | + if not content["name"].startswith("cfep"): |
| 42 | + continue |
| 43 | + if content["name"] == "cfep-00.md": |
| 44 | + # Hardcode title and status for CFEP-00 |
| 45 | + yield Cfep( |
| 46 | + content["name"], "CFEP Template", "Proposed", content["html_url"] |
| 47 | + ) |
| 48 | + continue |
| 49 | + cfep_response = requests.get(content["download_url"]) |
| 50 | + cfep_response.raise_for_status() |
| 51 | + cfep_text = cfep_response.text |
| 52 | + m = re.search(TITLE_PATTERN, cfep_text) |
| 53 | + title = m.group(1).strip() if m else "" |
| 54 | + m = re.search(STATUS_PATTERN, cfep_text) |
| 55 | + status = m.group(1).strip() if m else "" |
| 56 | + yield Cfep(content["name"], title, status, content["html_url"]) |
| 57 | + |
| 58 | + |
| 59 | +def write_cfep_index(): |
| 60 | + with CFEP_INDEX_RST.open("a") as f: |
| 61 | + for cfep in get_cfeps(): |
| 62 | + f.write(f"* {cfep.rst_link()}\n") |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + write_cfep_index() |
0 commit comments