Skip to content

Commit

Permalink
allow string or Version object as current_ver parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
emcek committed Nov 14, 2024
1 parent 5f11f43 commit afc7e49
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/dcspy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/dcspy/qt_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 1 addition & 3 deletions src/dcspy/starter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))}']
8 changes: 4 additions & 4 deletions src/dcspy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<organization or user>/<package>'.
:param current_ver: Current local Version object.
:param current_ver: string or Version object.
:param check: Version online.
:return: Formatted version as string.
"""
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit afc7e49

Please sign in to comment.