-
Notifications
You must be signed in to change notification settings - Fork 12
/
make_conf.py
53 lines (38 loc) · 1.41 KB
/
make_conf.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
51
52
53
"""Generate configuration file (conf.py) for sphinx-doc.
Based on:
- hypermodern python cookiecutter conf.py template
https://github.com/cjolowicz/cookiecutter-hypermodern-python/blob/master/%7B%7Bcookiecutter.project_name%7D%7D/docs/conf.py
- poetry mechanism to split authors name and email from pyproject.toml
https://github.com/python-poetry/poetry/blob/35058edf97e24ef1ac2b77563c84eed66d46939e/poetry/packages/package.py
To run:
python make_conf.py > docs/conf.py
Caution: this will overwrite existing conf.py file.
"""
import re
from datetime import datetime
import toml
AUTHOR_REGEX = re.compile(r"(?u)^(?P<name>[- .,\w\d'’\"()]+)(?: <(?P<email>.+?)>)?$")
def get_author(s: str) -> dict:
m = AUTHOR_REGEX.match(s)
name = m.group("name")
email = m.group("email")
return {"name": name, "email": email}
def conf_py(pyproject_filename: str) -> str:
d = toml.load(pyproject_filename)["tool"]["poetry"]
author = get_author(d["authors"][0])["name"]
friendly_name = d["name"]
return f"""\"\"\"Sphinx configuration.\"\"\"
project = "{friendly_name}"
author = "{author}"
copyright = "{datetime.now().year}, {author}"
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
"sphinx_click",
"sphinx_rtd_theme",
]
autodoc_typehints = "description"
html_theme = "sphinx_rtd_theme"
"""
if __name__ == "__main__":
print(conf_py("pyproject.toml"))