diff --git a/docs/conf.py b/docs/conf.py index 2c8928e..81de350 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -9,7 +9,7 @@ import re import sys from pathlib import Path -import tomllib +from importlib import metadata DOCS_DIR = Path(__file__).resolve().parent ROOT_DIR = DOCS_DIR.parent @@ -17,22 +17,11 @@ sys.path.append(str(ROOT_DIR)) sys.path.append(str(ROOT_DIR / "src")) - -def _read_release() -> str: - pyproject_file = ROOT_DIR / "pyproject.toml" - data = tomllib.loads(pyproject_file.read_text(encoding="utf-8")) - - try: - return data["project"]["version"] - except KeyError as exc: - raise RuntimeError( - f"Could not determine khisto version from {pyproject_file}" - ) from exc - project = 'khisto-python' copyright = '2026, The Khiops Team' author = 'The Khiops Team' -release = _read_release() +# We want to make sure the docs are built on an installed package only +release = metadata.version("khisto") # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration diff --git a/pyproject.toml b/pyproject.toml index fdb47a1..61b6f48 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "khisto" -version = "0.2.0" +version = "0.3.0" description = "Optimal histogram visualization using the Khiops algorithm" readme = "README.md" license = "BSD-3-Clause-Clear" @@ -80,6 +80,7 @@ dev = [ {include-group = "test"}, {include-group = "lint"}, {include-group = "docs"}, + "tomli>=2.0.0; python_version < '3.11'" # TODO : Remove on Python 3.10 EOL ] [build-system] diff --git a/src/khisto/__init__.py b/src/khisto/__init__.py index ed8f3ad..5fd5450 100644 --- a/src/khisto/__init__.py +++ b/src/khisto/__init__.py @@ -10,11 +10,24 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) -ROOT_DIR = Path(os.path.dirname(os.path.abspath(__file__))).parent.absolute() - +ROOT_DIR = Path(__file__).resolve().parent.parent KHISTO_BIN_DIR = os.environ.get("KHISTO_BIN_DIR", "khisto") -__version__ = version("khisto") +if (ROOT_DIR / "pyproject.toml").exists(): + # Development mode: package not installed; pyproject.toml present + # TODO : Remove on Python 3.10 EOL + try: + import tomllib as tomli + except ModuleNotFoundError: + import tomli + + with open(ROOT_DIR / "pyproject.toml", "rt") as f: + __version__ = tomli.load(f)["project"]["version"] +else: + # User mode: package installed; pyproject.toml not directly accessible + from importlib.metadata import version # noqa: E402 + + __version__ = version("khisto") from .array import histogram # noqa: E402 from .core import HistogramResult # noqa: E402