Skip to content

Commit

Permalink
Refactor code for resolving version information
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowXBoss696 committed Mar 30, 2024
1 parent 845aa9e commit 81cf47c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
15 changes: 2 additions & 13 deletions fastboot/__init__.py
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])
10 changes: 9 additions & 1 deletion fastboot/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import pathlib
from typing import Any

PROJECT_DIR: pathlib.Path = pathlib.Path(__file__).parents[1]
# Known Paths
PROJECT_ROOT: pathlib.Path = pathlib.Path(__file__).parents[1]
PROJECT_CONFIG_TOML: pathlib.Path = PROJECT_ROOT / "pyproject.toml"

# Settings:
DEVELOP: bool = PROJECT_CONFIG_TOML.exists()


# Settings Schema

KNOWN_SETTINGS: dict[str, Any] = {
"port": {},
Expand Down
25 changes: 25 additions & 0 deletions fastboot/version.py
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()

0 comments on commit 81cf47c

Please sign in to comment.