|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from selenium import webdriver |
| 4 | +from selenium.webdriver.common.by import By |
| 5 | +from selenium.webdriver.support.ui import WebDriverWait |
| 6 | +from selenium.common.exceptions import WebDriverException |
| 7 | + |
| 8 | + |
| 9 | +def generate_contrib_avatars(app, config): |
| 10 | + """Render a template webpage with avatars generated by JS and a GitHub API call.""" |
| 11 | + root = Path(app.srcdir) |
| 12 | + infile = root / "sphinxext" / "_avatar_template.html" |
| 13 | + outfile = root / "_templates" / "avatars.html" |
| 14 | + try: |
| 15 | + options = webdriver.ChromeOptions() |
| 16 | + options.add_argument("--headless=new") |
| 17 | + driver = webdriver.Chrome(options=options) |
| 18 | + except WebDriverException: |
| 19 | + options = webdriver.FirefoxOptions() |
| 20 | + options.add_argument("--headless=new") |
| 21 | + driver = webdriver.Firefox(options=options) |
| 22 | + driver.get(f"file://{infile}") |
| 23 | + wait = WebDriverWait(driver, 20) |
| 24 | + wait.until(lambda d: d.find_element(by=By.ID, value="contributor-avatars")) |
| 25 | + body = driver.find_element(by=By.TAG_NAME, value="body").get_attribute("innerHTML") |
| 26 | + with open(outfile, "w") as fid: |
| 27 | + fid.write(body) |
| 28 | + driver.quit() |
| 29 | + |
| 30 | + |
| 31 | +def setup(app): |
| 32 | + """Set up the Sphinx app.""" |
| 33 | + app.connect("config-inited", generate_contrib_avatars) |
| 34 | + return |
0 commit comments