Skip to content

Commit

Permalink
Using importlib
Browse files Browse the repository at this point in the history
  • Loading branch information
rfst committed Mar 7, 2024
1 parent c81bddb commit b652802
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/mobt/VersionChecker/Suppliers/LocalInstallation.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
from dataclasses import dataclass
from typing import Optional

import pkg_resources
# Updated imports
from importlib.metadata import version, PackageNotFoundError
from injector import inject
from packaging.version import Version

from mobt.VersionChecker import version_checker_logger
from mobt.VersionChecker.Suppliers.SupplierInterface import SupplierInterface


@inject
@dataclass(frozen=True)
class LocalInstallation(SupplierInterface):

def get_version(self) -> Optional[Version]:
version = pkg_resources.get_distribution('mob-tool').version
version_checker_logger().debug(f'Version installed locally: {version}')
try:
# Using importlib.metadata.version to get the package version
version_str = version('mob-tool')
version_checker_logger().debug(f'Version installed locally: {version_str}')

return Version(version)
return Version(version_str)
except PackageNotFoundError:
# Handle the case where the package is not found
version_checker_logger().debug('mob-tool package not found locally.')
return None

0 comments on commit b652802

Please sign in to comment.