Skip to content

Commit

Permalink
Merge pull request #194 from Aleph-Alpha/read-version-from-toml
Browse files Browse the repository at this point in the history
fix: read package version from pyproject.toml if not found in metadata
  • Loading branch information
moldhouse authored Nov 4, 2024
2 parents 8fd35fe + e08a472 commit c775973
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion aleph_alpha_client/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
import importlib.metadata

__version__ = importlib.metadata.version("aleph-alpha-client")
import re
from pathlib import Path
import logging


MIN_API_VERSION = "1.19.0"


def pyproject_version() -> str:
"""Return the package version specified in pyproject.toml.
Poetry inject the package version from the pyproject.toml file into the package
metadata at build time. This means any user of the package has access to the
version (it is send as User-Agent header in requests).
If this repository is cloned and the package is not installed, the version will
be not available (it is 0.0.0 per default). To also provide a version in these
cases, this function tries to read the version from the pyproject.toml file.
To not break imports in cases where both, the pyproject.toml file and the package
metadata are not available, no error is raised and a default version of 0.0.0
will be returned.
"""
NO_VERSION = "0.0.0"
pyproject_path = Path(__file__).resolve().parent.parent / "pyproject.toml"

if not pyproject_path.is_file():
logging.error("pyproject.toml file not found.")
return NO_VERSION

version_pattern = re.compile(r'^version\s*=\s*["\']([^"\']+)["\']', re.MULTILINE)

with pyproject_path.open("r", encoding="utf-8") as file:
content = file.read()

if (match := version_pattern.search(content)):
return match.group(1)

logging.error("Version not found in pyproject.toml")
return NO_VERSION


__version__ = importlib.metadata.version("aleph-alpha-client")

if __version__ == "0.0.0":
__version__ = pyproject_version()

0 comments on commit c775973

Please sign in to comment.