Skip to content

Commit

Permalink
read __version__ from __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Feb 4, 2025
1 parent ae0df38 commit bceb948
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
23 changes: 20 additions & 3 deletions babel_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import argparse
import ast
import subprocess
from pathlib import Path

Expand All @@ -17,6 +18,8 @@
) from ie

PROJECT_DIR = Path(__file__).resolve().parent
PYPROJECT_TOML = PROJECT_DIR / "pyproject.toml"
INIT_PY = PROJECT_DIR / "python_docs_theme" / "__init__.py"

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

def get_project_info() -> dict:
"""Retrieve project's info to populate the message catalog template"""
with open(Path(PROJECT_DIR / "pyproject.toml"), "rb") as f:
data = tomllib.load(f)
return data["project"]
pyproject_text = PYPROJECT_TOML.read_text(encoding="utf-8")
project_data = tomllib.loads(pyproject_text)["project"]

# read __version__ from __init__.py
for child in ast.parse(INIT_PY.read_bytes()).body:
if not isinstance(child, ast.Assign):
continue
target = child.targets[0]
if not isinstance(target, ast.Name) or target.id != "__version__":
continue
version_node = child.value
if not isinstance(version_node, ast.Constant):
continue
project_data["version"] = version_node.value
break

return project_data


def extract_messages() -> None:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dynamic = [ "version" ]
dependencies = [
"sphinx>=3.4",
]

urls.Code = "https://github.com/python/python-docs-theme"
urls.Download = "https://pypi.org/project/python-docs-theme/"
urls.Homepage = "https://github.com/python/python-docs-theme/"
Expand Down

0 comments on commit bceb948

Please sign in to comment.