Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Internal:
* remove `psutil` library - #531 (@emcek)
* use new API of `Pillow` Image object get_flattened_data() - #532 (@emcek)
* Use suppress from contextlib - #535 (@emcek)

## 3.7.1
* Update About dialog
Expand Down
17 changes: 5 additions & 12 deletions src/dcspy/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import re
from collections.abc import Callable, Iterator
from contextlib import suppress
from logging import getLogger
from os import makedirs
from pathlib import Path
Expand Down Expand Up @@ -178,11 +179,9 @@ def _remove_key(cfg: DcspyConfigYaml, key: str) -> None:
:param cfg: Configuration dictionary
:param key: key name
"""
try:
with suppress(KeyError):
del cfg[key]
LOG.debug(f'Remove key: {key}')
except KeyError:
pass


def _rename_key_keep_value(cfg: DcspyConfigYaml, old_name: str, new_name: str, default_value: ConfigValue) -> None:
Expand All @@ -195,10 +194,8 @@ def _rename_key_keep_value(cfg: DcspyConfigYaml, old_name: str, new_name: str, d
:param default_value: Use if a value for an old key does not exist
"""
value = cfg.get(old_name, default_value)
try:
with suppress(KeyError):
del cfg[old_name]
except KeyError:
pass
cfg[new_name] = value
LOG.debug(f'Rename key {old_name} -> {new_name} with: {value}')

Expand Down Expand Up @@ -227,11 +224,9 @@ def _copy_file(filename: str, to_path: Path, force: bool = False) -> None:
:param force: Force to overwrite an existing file
"""
if not Path(to_path / filename).is_file() or force:
try:
with suppress(SameFileError):
copy(src=DEFAULT_YAML_FILE.parent / filename, dst=to_path)
LOG.debug(f'Copy file: {filename} to {to_path}')
except SameFileError:
pass


def replace_line_in_file(filename: str, dir_path: Path, pattern: re.Pattern, new_text: str) -> None:
Expand All @@ -244,12 +239,10 @@ def replace_line_in_file(filename: str, dir_path: Path, pattern: re.Pattern, new
:param new_text: The text to replace the line matching the pattern with.
"""
yaml_filename = dir_path / filename
try:
with suppress(FileNotFoundError):
with open(yaml_filename) as yaml_file:
file_content = yaml_file.read()
LOG.debug(yaml_filename)
updated_content = re.sub(pattern, new_text, file_content)
with open(yaml_filename, 'w') as yaml_file:
yaml_file.write(updated_content)
except FileNotFoundError:
pass
9 changes: 3 additions & 6 deletions src/dcspy/qt_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import traceback
from argparse import Namespace
from collections.abc import Callable
from contextlib import suppress
from functools import partial
from importlib import import_module
from logging import DEBUG, INFO, Formatter, Handler, LogRecord, getLogger
Expand Down Expand Up @@ -336,10 +337,8 @@ def _set_ded_font_and_font_sliders(self) -> None:

for name in ['large', 'medium', 'small']:
hs: QSlider = getattr(self, f'hs_{name}_font')
try:
with suppress(RuntimeError):
hs.valueChanged.disconnect()
except RuntimeError:
pass
hs.setMinimum(minimum)
hs.setMaximum(maximum)
hs.valueChanged.connect(partial(self._set_label_and_hs_value, name=name))
Expand Down Expand Up @@ -763,7 +762,7 @@ def _checked_iface_rb_for_identifier(self, key_name: str) -> None:

:param key_name: G-Key, LCD or Mouse button as string
"""
try:
with suppress(KeyError, AttributeError):
widget_iface = self.input_reqs[self.current_plane][key_name].widget_iface
if widget_iface == 'rb_custom':
self.le_custom.setText(self.input_reqs[self.current_plane][key_name].request.split(f'{RequestType.CUSTOM.value} ')[1])
Expand All @@ -773,8 +772,6 @@ def _checked_iface_rb_for_identifier(self, key_name: str) -> None:
self.le_custom.setText('')
self.hs_set_state.setValue(0)
getattr(self, widget_iface).setChecked(True)
except (KeyError, AttributeError):
pass

def _hs_set_state_moved(self, value: int) -> None:
"""
Expand Down
30 changes: 12 additions & 18 deletions src/dcspy/sdk/key_sdk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from collections.abc import Callable
from contextlib import suppress
from ctypes import CDLL, CFUNCTYPE, POINTER, Structure, c_bool, c_uint, c_void_p, c_wchar_p, pointer
from logging import getLogger
from typing import ClassVar
Expand Down Expand Up @@ -70,20 +71,17 @@ def logi_gkey_init(self) -> bool:
It must be called before your application can see G-key/button events.
:return: If the function succeeds, it returns True. Otherwise, False.
"""
try:
with suppress(AttributeError):
self.key_dll.LogiGkeyInit.restype = c_bool
self.key_dll.LogiGkeyInit.argtypes = [POINTER(LogiGkeyCBContext)]

return self.key_dll.LogiGkeyInit(self.gkey_context_ptr)
except AttributeError:
return False
return False

def logi_gkey_shutdown(self) -> None:
"""Unload the corresponding DLL and frees up any allocated resources."""
try:
with suppress(AttributeError):
self.key_dll.LogiGkeyShutdown()
except AttributeError:
pass

def logi_gkey_is_keyboard_gkey_pressed(self, g_key: int, mode: int) -> bool:
"""
Expand All @@ -93,10 +91,9 @@ def logi_gkey_is_keyboard_gkey_pressed(self, g_key: int, mode: int) -> bool:
:param mode: Number of the mode currently selected, example 1, 2 or 3
:return: True if the specified G-key for the specified Mode is currently being pressed, False otherwise
"""
try:
with suppress(AttributeError):
return self.key_dll.LogiGkeyIsKeyboardGkeyPressed(g_key, mode)
except AttributeError:
return False
return False

def logi_gkey_is_keyboard_gkey_string(self, g_key: int, mode: int) -> str:
"""
Expand All @@ -106,10 +103,9 @@ def logi_gkey_is_keyboard_gkey_string(self, g_key: int, mode: int) -> str:
:param mode: Number of the mode currently selected (1, 2 or 3)
:return: Friendly string for specified G-key and Mode number, example 'G5/M1'
"""
try:
with suppress(AttributeError):
return self.key_dll.LogiGkeyGetKeyboardGkeyString(g_key, mode)
except AttributeError:
return ''
return ''

def logi_gkey_is_mouse_pressed(self, button_number: int) -> bool:
"""
Expand All @@ -118,10 +114,9 @@ def logi_gkey_is_mouse_pressed(self, button_number: int) -> bool:
:param button_number: Number of the buttons to check, example between 6 and 20 for G600
:return: True if the specified button is currently being pressed, False otherwise
"""
try:
with suppress(AttributeError):
return self.key_dll.LogiGkeyIsMousePressed(button_number)
except AttributeError:
return False
return False

def logi_gkey_is_mouse_string(self, button_number: int) -> str:
"""
Expand All @@ -130,7 +125,6 @@ def logi_gkey_is_mouse_string(self, button_number: int) -> str:
:param button_number: Number of the button to check, example between 6 and 20 for G600
:return: Friendly string for specified button number, example 'Mouse Btn 8'
"""
try:
with suppress(AttributeError):
return self.key_dll.LogiGkeyGetMouseString(button_number)
except AttributeError:
return ''
return ''
49 changes: 19 additions & 30 deletions src/dcspy/sdk/lcd_sdk.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import suppress
from logging import getLogger

from _cffi_backend import Lib
Expand Down Expand Up @@ -36,10 +37,9 @@ def logi_lcd_init(self, name: str, lcd_type: LcdType) -> bool:
:param lcd_type: LCD type
:return: A result of execution
"""
try:
with suppress(AttributeError):
return self.lcd_dll.LogiLcdInit(FFI().new('wchar_t[]', name), lcd_type.value) # type: ignore[attr-defined]
except AttributeError:
return False
return False

def logi_lcd_is_connected(self, lcd_type: LcdType) -> bool:
"""
Expand All @@ -48,10 +48,9 @@ def logi_lcd_is_connected(self, lcd_type: LcdType) -> bool:
:param lcd_type: LCD type
:return: A result of execution
"""
try:
with suppress(AttributeError):
return self.lcd_dll.LogiLcdIsConnected(lcd_type.value) # type: ignore[attr-defined]
except AttributeError:
return False
return False

def logi_lcd_is_button_pressed(self, button: LcdButton) -> bool:
"""
Expand All @@ -60,24 +59,19 @@ def logi_lcd_is_button_pressed(self, button: LcdButton) -> bool:
:param button: Defines the button to check on
:return: True if a button is being pressed, False otherwise
"""
try:
with suppress(AttributeError):
return self.lcd_dll.LogiLcdIsButtonPressed(button.value) # type: ignore[attr-defined]
except AttributeError:
return False
return False

def logi_lcd_update(self) -> None:
"""Update the LCD."""
try:
with suppress(AttributeError):
self.lcd_dll.LogiLcdUpdate() # type: ignore[attr-defined]
except AttributeError:
pass

def logi_lcd_shutdown(self) -> None:
"""Kill the applet and frees memory used by the SDK."""
try:
with suppress(AttributeError):
self.lcd_dll.LogiLcdShutdown() # type: ignore[attr-defined]
except AttributeError:
pass

def logi_lcd_mono_set_background(self, pixels: list[int]) -> bool:
"""
Expand All @@ -92,10 +86,9 @@ def logi_lcd_mono_set_background(self, pixels: list[int]) -> bool:
:param pixels: List of 6880 (160x43) pixels as integer
:return: A result of execution
"""
try:
with suppress(AttributeError, CDefError): # we need catch error since BYTE[] is a Windows specific
return self.lcd_dll.LogiLcdMonoSetBackground(FFI().new('BYTE[]', pixels)) # type: ignore[attr-defined]
except (AttributeError, CDefError): # we need catch error since BYTE[] is a Windows specific
return False
return False

def logi_lcd_mono_set_text(self, line_no: int, text: str) -> bool:
"""
Expand All @@ -105,10 +98,9 @@ def logi_lcd_mono_set_text(self, line_no: int, text: str) -> bool:
:param text: The text to display
:return: A result of execution
"""
try:
with suppress(AttributeError):
return self.lcd_dll.LogiLcdMonoSetText(line_no, FFI().new('wchar_t[]', text)) # type: ignore[attr-defined]
except AttributeError:
return False
return False

def logi_lcd_color_set_background(self, pixels: list[tuple[int, int, int, int]]) -> bool:
"""
Expand All @@ -121,10 +113,9 @@ def logi_lcd_color_set_background(self, pixels: list[tuple[int, int, int, int]])
:return: A result of execution
"""
img_bytes = [byte for pixel in pixels for byte in pixel]
try:
with suppress(AttributeError, CDefError): # we need catch error since BYTE[] is a Windows specific
return self.lcd_dll.LogiLcdColorSetBackground(FFI().new('BYTE[]', img_bytes)) # type: ignore[attr-defined]
except (AttributeError, CDefError): # we need catch error since BYTE[] is Windows specific
return False
return False

def logi_lcd_color_set_title(self, text: str, rgb: tuple[int, int, int] = (255, 255, 255)) -> bool:
"""
Expand All @@ -137,10 +128,9 @@ def logi_lcd_color_set_title(self, text: str, rgb: tuple[int, int, int] = (255,
:param rgb: a tuple with integer values between 0 and 255 as red, green, blue
:return: A result of execution
"""
try:
with suppress(AttributeError):
return self.lcd_dll.LogiLcdColorSetTitle(FFI().new('wchar_t[]', text), *rgb) # type: ignore[attr-defined]
except AttributeError:
return False
return False

def logi_lcd_color_set_text(self, line_no: int, text: str, rgb: tuple[int, int, int] = (255, 255, 255)) -> bool:
"""
Expand All @@ -152,10 +142,9 @@ def logi_lcd_color_set_text(self, line_no: int, text: str, rgb: tuple[int, int,
:param rgb: tuple with integer values between 0 and 255 (interpreted as red, green or blue)
:return: A result of execution
"""
try:
with suppress(AttributeError):
return self.lcd_dll.LogiLcdColorSetText(line_no, FFI().new('wchar_t[]', text), *rgb) # type: ignore[attr-defined]
except AttributeError:
return False
return False

def update_text(self, txt: list[tuple[str, Color]]) -> None:
"""
Expand Down
Loading
Loading