-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcontrib_avatars.py
50 lines (43 loc) · 1.79 KB
/
contrib_avatars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import os
from pathlib import Path
from mne.utils import _replace_md5
def generate_contrib_avatars(app, config):
"""Render a template webpage with avatars generated by JS and a GitHub API call."""
root = Path(app.srcdir)
infile = root / "sphinxext" / "_avatar_template.html"
outfile = root / "_templates" / "avatars.html.new"
if os.getenv("MNE_ADD_CONTRIBUTOR_IMAGE", "false").lower() != "true":
body = """\
<p>Contributor avators will appear here in full doc builds. Set \
MNE_ADD_CONTRIBUTOR_IMAGE=true in your environment to generate it.</p>"""
else:
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
try:
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
except WebDriverException:
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
driver = webdriver.Firefox(options=options)
driver.get(f"file://{infile}")
wait = WebDriverWait(driver, 20)
wait.until(lambda d: d.find_element(by=By.ID, value="contributor-avatars"))
body = driver.find_element(by=By.TAG_NAME, value="body").get_property(
"innerHTML"
)
assert isinstance(body, str), type(body)
driver.quit()
with open(outfile, "w") as fid:
fid.write(body)
_replace_md5(str(outfile))
def setup(app):
"""Set up the Sphinx app."""
app.connect("config-inited", generate_contrib_avatars)
return