Skip to content

Commit bceb948

Browse files
committed
read __version__ from __init__.py
1 parent ae0df38 commit bceb948

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

babel_runner.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import argparse
6+
import ast
67
import subprocess
78
from pathlib import Path
89

@@ -17,6 +18,8 @@
1718
) from ie
1819

1920
PROJECT_DIR = Path(__file__).resolve().parent
21+
PYPROJECT_TOML = PROJECT_DIR / "pyproject.toml"
22+
INIT_PY = PROJECT_DIR / "python_docs_theme" / "__init__.py"
2023

2124
# Global variables used by pybabel below (paths relative to PROJECT_DIR)
2225
DOMAIN = "messages"
@@ -29,9 +32,23 @@
2932

3033
def get_project_info() -> dict:
3134
"""Retrieve project's info to populate the message catalog template"""
32-
with open(Path(PROJECT_DIR / "pyproject.toml"), "rb") as f:
33-
data = tomllib.load(f)
34-
return data["project"]
35+
pyproject_text = PYPROJECT_TOML.read_text(encoding="utf-8")
36+
project_data = tomllib.loads(pyproject_text)["project"]
37+
38+
# read __version__ from __init__.py
39+
for child in ast.parse(INIT_PY.read_bytes()).body:
40+
if not isinstance(child, ast.Assign):
41+
continue
42+
target = child.targets[0]
43+
if not isinstance(target, ast.Name) or target.id != "__version__":
44+
continue
45+
version_node = child.value
46+
if not isinstance(version_node, ast.Constant):
47+
continue
48+
project_data["version"] = version_node.value
49+
break
50+
51+
return project_data
3552

3653

3754
def extract_messages() -> None:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dynamic = [ "version" ]
3232
dependencies = [
3333
"sphinx>=3.4",
3434
]
35+
3536
urls.Code = "https://github.com/python/python-docs-theme"
3637
urls.Download = "https://pypi.org/project/python-docs-theme/"
3738
urls.Homepage = "https://github.com/python/python-docs-theme/"

0 commit comments

Comments
 (0)