Skip to content

Commit

Permalink
ENH: Allow to pass directly a version object to misc.compare_version
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-braun committed Aug 13, 2024
1 parent 04de7f8 commit 097b022
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.42.0 (2024-mm-dd)

- **ENH: Add a function `snap.get_snap_version` to retrieve current SNAP version** ([#172](https://github.com/sertit/eoreader/issues/172))
- **ENH: Allow to pass directly a version object to `misc.compare_version`**

## 1.41.0 (2024-08-06)

Expand Down
7 changes: 3 additions & 4 deletions CI/SCRIPTS/test_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import shutil

import pytest
from packaging.version import Version

from sertit import ci, misc, snap
from sertit.snap import TILE_SIZE
Expand Down Expand Up @@ -53,9 +52,9 @@ def test_snap():
assert substr in cli


def snap_version():
def test_snap_version():
"""Test SNAP version"""
snap_version = snap.get_snap_version()
assert misc.compare(
snap_version, Version("10.0.0"), "=="
assert misc.compare_version(
snap_version, "10.0.0", "=="
), f"Unexpected SNAP version: {snap_version}."
17 changes: 14 additions & 3 deletions sertit/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from enum import Enum, unique
from typing import Any, Union

from packaging.version import Version

from sertit import AnyPath
from sertit.logs import SU_NAME
from sertit.types import AnyPathStrType
Expand Down Expand Up @@ -470,7 +472,9 @@ def compare(a, b, operation: str) -> bool:
return ops[operation](a, b)


def compare_version(lib: str, version_to_check: str, operator: str) -> bool:
def compare_version(
lib: Union[str, Version], version_to_check: str, operator: str
) -> bool:
"""
Compare the version of a librarie to a reference, giving the operator.
Expand All @@ -489,6 +493,13 @@ def compare_version(lib: str, version_to_check: str, operator: str) -> bool:
"""
from importlib.metadata import version

from packaging.version import Version
if isinstance(lib, Version):
lib_version = lib
elif isinstance(lib, str):
lib_version = Version(version(lib))
else:
raise TypeError(
"'lib' should either be the name of your library as a string or directly a 'Version' object."
)

return compare(Version(version(lib)), Version(version_to_check), operator)
return compare(lib_version, Version(version_to_check), operator)

0 comments on commit 097b022

Please sign in to comment.