-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code for resolving version information
- Loading branch information
1 parent
845aa9e
commit 81cf47c
Showing
3 changed files
with
36 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,8 @@ | ||
import importlib.metadata | ||
import tomllib | ||
from fastboot.version import VERSION | ||
|
||
__all__ = ["__version__"] | ||
|
||
# Project Information | ||
__version__ = VERSION | ||
__author__ = ["Arpan Mahanty <arpan.mahanty.007@gmail.com>"] | ||
__license__ = "MIT" | ||
|
||
try: | ||
from fastboot.settings import PROJECT_DIR | ||
|
||
# Read version from pyproject file during development | ||
with open(PROJECT_DIR / "pyproject.toml", mode="rb") as pyproject_file: | ||
__version__: str = tomllib.load(pyproject_file)["tool"]["poetry"]["version"] | ||
|
||
except FileNotFoundError: # pragma: no cover | ||
# Read from the package metadata | ||
__version__: str = importlib.metadata.version(__package__ or __name__.split(".", maxsplit=1)[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import importlib.metadata | ||
import tomllib | ||
from typing import Any | ||
|
||
from fastboot.settings import DEVELOP, PROJECT_CONFIG_TOML | ||
|
||
|
||
def read_version_from_pyproject() -> str: | ||
"""Reads version string from the pyproject file located at the project root. Mostly used during development only.""" | ||
|
||
with open(PROJECT_CONFIG_TOML, mode="rb") as f: | ||
pyproject: dict[str, Any] = tomllib.load(f) | ||
|
||
return pyproject["tool"]["poetry"]["version"] | ||
|
||
|
||
def read_version_from_package_metadata() -> str: # pragma: no cover | ||
"""Reads version string from the package metadata for the current project.""" | ||
|
||
pkg_name: str = __package__ or __name__.split(".", maxsplit=1)[0] | ||
return importlib.metadata.version(pkg_name) | ||
|
||
|
||
# Version Information | ||
VERSION: str = read_version_from_pyproject() if DEVELOP else read_version_from_package_metadata() |