From afc7e49cbc192d3f2c431242f9d1329895411a6d Mon Sep 17 00:00:00 2001 From: Michal Plichta Date: Thu, 14 Nov 2024 22:51:41 +0100 Subject: [PATCH] allow string or Version object as current_ver parameter --- src/dcspy/models.py | 6 ++++-- src/dcspy/qt_gui.py | 4 ++-- src/dcspy/starter.py | 4 +--- src/dcspy/utils.py | 8 ++++---- tests/test_utils.py | 12 ++++++------ 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/dcspy/models.py b/src/dcspy/models.py index aeb907860..f5c9e5c2c 100644 --- a/src/dcspy/models.py +++ b/src/dcspy/models.py @@ -1063,13 +1063,15 @@ class Release(BaseModel): assets: list[Asset] body: str - def is_latest(self, current_ver: version.Version) -> bool: + def is_latest(self, current_ver: str | version.Version) -> bool: """ Check if a release is latest. - :param current_ver: Version object + :param current_ver: string or Version object :return: True if the current version is latest, False otherwise """ + if isinstance(current_ver, str): + current_ver = version.parse(current_ver) return self.version <= current_ver def download_url(self, extension: str = '', file_name: str = '') -> str: diff --git a/src/dcspy/qt_gui.py b/src/dcspy/qt_gui.py index c25435fd8..8ed936d4d 100644 --- a/src/dcspy/qt_gui.py +++ b/src/dcspy/qt_gui.py @@ -915,7 +915,7 @@ def _set_completer_for_git_ref(self) -> None: # <=><=><=><=><=><=><=><=><=><=><=> check dcspy updates <=><=><=><=><=><=><=><=><=><=><=> def _dcspy_check_clicked(self) -> None: """Check a version of DCSpy and show message box.""" - ver_string = get_version_string(repo=DCSPY_REPO_NAME, current_ver=version.parse(__version__), check=True) + ver_string = get_version_string(repo=DCSPY_REPO_NAME, current_ver=__version__, check=True) self.statusbar.showMessage(ver_string) if 'update!' in ver_string: self.systray.showMessage('DCSpy', f'New: {ver_string}', QIcon(':/icons/img/edit-download.svg')) @@ -1468,7 +1468,7 @@ def fetch_system_data(self, silence: bool = False) -> SystemData: """ system, _, release, ver, _, proc = uname() dcs_type, dcs_ver = check_dcs_ver(Path(self.config['dcs'])) - dcspy_ver = get_version_string(repo=DCSPY_REPO_NAME, current_ver=version.parse(__version__), check=self.config['check_ver']) + dcspy_ver = get_version_string(repo=DCSPY_REPO_NAME, current_ver=__version__, check=self.config['check_ver']) bios_ver = str(self._check_local_bios()) dcs_bios_ver = self._get_bios_full_version(silence=silence) git_ver = 'Not installed' diff --git a/src/dcspy/starter.py b/src/dcspy/starter.py index f7063b12d..ae75f5b1a 100644 --- a/src/dcspy/starter.py +++ b/src/dcspy/starter.py @@ -6,8 +6,6 @@ from threading import Event from time import gmtime, time -from packaging import version - from dcspy import get_config_yaml_item from dcspy.dcsbios import ProtocolParser from dcspy.logitech import LogitechDevice @@ -119,7 +117,7 @@ def dcspy_run(model: LogitechDeviceModel, event: Event) -> None: logi_dev = LogitechDevice(parser=parser, sock=dcs_sock, model=model) LOG.info(f'Loading: {str(logi_dev)}') LOG.debug(f'Loading: {repr(logi_dev)}') - dcspy_ver = get_version_string(repo=DCSPY_REPO_NAME, current_ver=version.parse(__version__), check=bool(get_config_yaml_item('check_ver'))) + dcspy_ver = get_version_string(repo=DCSPY_REPO_NAME, current_ver=__version__, check=bool(get_config_yaml_item('check_ver'))) _handle_connection(logi_device=logi_dev, parser=parser, sock=dcs_sock, ver_string=dcspy_ver, event=event) LOG.info('DCSpy stopped.') logi_dev.display = ['DCSpy stopped', '', f'DCSpy: {dcspy_ver}', f'DCS-BIOS: {check_bios_ver(bios_path=get_config_yaml_item("dcsbios"))}'] diff --git a/src/dcspy/utils.py b/src/dcspy/utils.py index 5e54492c3..11b073b6b 100644 --- a/src/dcspy/utils.py +++ b/src/dcspy/utils.py @@ -112,12 +112,12 @@ def check_ver_at_github(repo: str) -> Release: raise ValueError(f'Unable to check {package} version online: {exc}') -def get_version_string(repo: str, current_ver: version.Version, check: bool = True) -> str: +def get_version_string(repo: str, current_ver: str | version.Version, check: bool = True) -> str: """ Generate formatted string with version number. :param repo: Format '/'. - :param current_ver: Current local Version object. + :param current_ver: string or Version object. :param check: Version online. :return: Formatted version as string. """ @@ -131,9 +131,9 @@ def get_version_string(repo: str, current_ver: version.Version, check: bool = Tr if result.is_latest(current_ver=current_ver): details = ' (latest)' - elif str(result.version) != '0.0.0': + elif result.version != version.parse('0.0.0'): details = ' (update!)' - current_ver = str(result.version) + current_ver = result.version ver_string = f'v{current_ver}{details}' return ver_string diff --git a/tests/test_utils.py b/tests/test_utils.py index f96d0436c..b337629c3 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -23,10 +23,10 @@ def test_check_ver_exception(): utils.check_ver_at_github(repo='fake3/package3') @mark.parametrize('current_ver, extension, file_name, result', [ - (version.parse('3.6.1'), 'tar.gz', 'dcspy', {'latest': True, 'dl_url': 'https://github.com/emcek/dcspy/releases/download/v3.6.1/dcspy-3.6.1.tar.gz', 'asset_file': 'dcspy-3.6.1.tar.gz'}), - (version.parse('3.5.0'), 'exe', 'dcspy_cli', {'latest': False, 'dl_url': 'https://github.com/emcek/dcspy/releases/download/v3.6.1/dcspy_cli.exe', 'asset_file': 'dcspy_cli.exe'}), - (version.parse('3.6.1'), 'exe', 'fake', {'latest': True, 'dl_url': '', 'asset_file': ''}), - (version.parse('3.5.0'), 'jpg', 'dcspy', {'latest': False, 'dl_url': '', 'asset_file': ''}), + ('3.6.1', 'tar.gz', 'dcspy', {'latest': True, 'dl_url': 'https://github.com/emcek/dcspy/releases/download/v3.6.1/dcspy-3.6.1.tar.gz', 'asset_file': 'dcspy-3.6.1.tar.gz'}), + ('3.5.0', 'exe', 'dcspy_cli', {'latest': False, 'dl_url': 'https://github.com/emcek/dcspy/releases/download/v3.6.1/dcspy_cli.exe', 'asset_file': 'dcspy_cli.exe'}), + ('3.6.1', 'exe', 'fake', {'latest': True, 'dl_url': '', 'asset_file': ''}), + ('3.5.0', 'jpg', 'dcspy', {'latest': False, 'dl_url': '', 'asset_file': ''}), ], ids=['latest', 'not latest', 'fake file', 'fake ext']) def test_new_check_ver_at_github(current_ver, extension, file_name, result, resources): import json @@ -46,8 +46,8 @@ def test_new_check_ver_at_github(current_ver, extension, file_name, result, reso @mark.parametrize('current_ver, result', [ - (version.parse('3.6.1'), 'v3.6.1 (latest)'), - (version.parse('1.1.1'), 'v3.6.1 (update!)') + ('3.6.1', 'v3.6.1 (latest)'), + ('1.1.1', 'v3.6.1 (update!)') ], ids=['No update', 'New version']) def test_get_version_string_is_possible(current_ver, result, resources): import json