Skip to content

Commit

Permalink
apply checks and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
daringer committed Oct 16, 2024
1 parent 6c55f8d commit ff65458
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 136 deletions.
2 changes: 0 additions & 2 deletions nitrokeyapp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Experimental Nitrokey GUI Application - based on the Nitrokey Python SDK"""

import importlib.metadata
import pathlib
import sys

try:
__version__ = importlib.metadata.version(__name__)
Expand Down
4 changes: 1 addition & 3 deletions nitrokeyapp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

@contextmanager
def exception_handler(
hook: Callable[
[Type[BaseException], BaseException, Optional[TracebackType]], Any
],
hook: Callable[[Type[BaseException], BaseException, Optional[TracebackType]], Any],
) -> Generator[None, None, None]:
old_hook = sys.excepthook
sys.excepthook = hook
Expand Down
2 changes: 1 addition & 1 deletion nitrokeyapp/common_ui.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass

from PySide6.QtCore import Signal, QObject
from PySide6.QtCore import QObject, Signal

from nitrokeyapp.information_box import InfoUi
from nitrokeyapp.progress_box import ProgressUi
Expand Down
4 changes: 1 addition & 3 deletions nitrokeyapp/device_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ def open(self) -> NK3:
return device
else:
# TODO: improve error handling
raise RuntimeError(
f"Failed to open device {self.uuid} at {self.path}"
)
raise RuntimeError(f"Failed to open device {self.uuid} at {self.path}")

def update(
self,
Expand Down
2 changes: 1 addition & 1 deletion nitrokeyapp/device_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import List, Iterator
from typing import Iterator, List

from nitrokeyapp.device_data import DeviceData

Expand Down
30 changes: 8 additions & 22 deletions nitrokeyapp/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,19 @@ def __init__(self, qt_app: QtWidgets.QApplication, log_file: str):
view.common_ui.touch.stop.connect(self.touch_dialog.stop)

view.common_ui.info.info.connect(self.info_box.set_status)
view.common_ui.info.error.connect(
self.info_box.set_error_status
)
view.common_ui.info.pin_cached.connect(
self.info_box.set_pin_icon
)
view.common_ui.info.pin_cleared.connect(
self.info_box.unset_pin_icon
)
self.info_box.pin_pressed.connect(
view.common_ui.info.pin_pressed
)
view.common_ui.info.error.connect(self.info_box.set_error_status)
view.common_ui.info.pin_cached.connect(self.info_box.set_pin_icon)
view.common_ui.info.pin_cleared.connect(self.info_box.unset_pin_icon)
self.info_box.pin_pressed.connect(view.common_ui.info.pin_pressed)

view.common_ui.prompt.confirm.connect(self.prompt_box.confirm)
self.prompt_box.confirmed.connect(
view.common_ui.prompt.confirmed
)
self.prompt_box.confirmed.connect(view.common_ui.prompt.confirmed)

view.common_ui.progress.start.connect(self.progress_box.show)
view.common_ui.progress.stop.connect(self.progress_box.hide)
view.common_ui.progress.progress.connect(
self.progress_box.update
)
view.common_ui.progress.progress.connect(self.progress_box.update)

view.common_ui.gui.refresh_devices.connect(
self.refresh_devices
)
view.common_ui.gui.refresh_devices.connect(self.refresh_devices)

# main window widgets
self.home_button = self.ui.btn_home
Expand Down Expand Up @@ -204,7 +190,7 @@ def update_devices(self) -> None:
"""update device button view based on `self.device_manager` contents"""

for widget in self.device_buttons:
widget.setParent(None)
widget.setParent(None) # type: ignore [call-overload]
widget.destroy()
self.device_buttons.clear()

Expand Down
8 changes: 2 additions & 6 deletions nitrokeyapp/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

@contextmanager
def init_logging() -> Generator[str, None, None]:
log_file = NamedTemporaryFile(
prefix="nitrokey-app2.", suffix=".log", delete=False
)
log_file = NamedTemporaryFile(prefix="nitrokey-app2.", suffix=".log", delete=False)
log_format = "%(relativeCreated)-8d %(levelname)6s %(name)10s %(message)s"

try:
Expand All @@ -33,9 +31,7 @@ def init_logging() -> Generator[str, None, None]:
if log_to_console:
handlers.append(console_handler) # type: ignore

logging.basicConfig(
format=log_format, level=logging.DEBUG, handlers=handlers
)
logging.basicConfig(format=log_format, level=logging.DEBUG, handlers=handlers)

yield log_file.name
finally:
Expand Down
14 changes: 4 additions & 10 deletions nitrokeyapp/nk3_button.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Callable
from typing import Callable, Optional

from PySide6 import QtCore, QtGui, QtWidgets

Expand Down Expand Up @@ -73,22 +73,16 @@ def stop_touch(self) -> None:
self.effect.setStrength(0)

def fold(self) -> None:
self.setText(
self.data.uuid_prefix if not self.data.is_bootloader else "BL"
)
self.setText(self.data.uuid_prefix if not self.data.is_bootloader else "BL")
self.setMinimumWidth(58)
self.setMaximumWidth(58)
self.setIconSize(QtCore.QSize(40, 40))
self.setToolButtonStyle(
QtCore.Qt.ToolButtonStyle.ToolButtonTextUnderIcon
)
self.setToolButtonStyle(QtCore.Qt.ToolButtonStyle.ToolButtonTextUnderIcon)

def unfold(self) -> None:
self.setChecked(False)
self.setText(self.data.name)
self.setMinimumWidth(178)
self.setMaximumWidth(178)
self.setIconSize(QtCore.QSize(32, 32))
self.setToolButtonStyle(
QtCore.Qt.ToolButtonStyle.ToolButtonTextBesideIcon
)
self.setToolButtonStyle(QtCore.Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
8 changes: 2 additions & 6 deletions nitrokeyapp/overview_tab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,12 @@ def more_options(self) -> None:
self.hide_more_options()

def show_more_options(self) -> None:
self.ui.btn_more_options.setIcon(
QtUtilsMixIn.get_qicon("down_arrow.svg")
)
self.ui.btn_more_options.setIcon(QtUtilsMixIn.get_qicon("down_arrow.svg"))
oSize = self.ui.frame_more_options.sizeHint()
self.ui.frame_more_options.setFixedHeight(oSize.height())

def hide_more_options(self) -> None:
self.ui.btn_more_options.setIcon(
QtUtilsMixIn.get_qicon("right_arrow.svg")
)
self.ui.btn_more_options.setIcon(QtUtilsMixIn.get_qicon("right_arrow.svg"))
self.ui.frame_more_options.setFixedHeight(0)

@Slot(bool)
Expand Down
78 changes: 19 additions & 59 deletions nitrokeyapp/secrets_tab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,14 @@ def __init__(self, parent: Optional[QWidget] = None) -> None:
self.trigger_check_device.connect(self._worker.check_device)
self.trigger_delete_credential.connect(self._worker.delete_credential)
self.trigger_generate_otp.connect(self._worker.generate_otp)
self.trigger_refresh_credentials.connect(
self._worker.refresh_credentials
)
self.trigger_refresh_credentials.connect(self._worker.refresh_credentials)
self.trigger_get_credential.connect(self._worker.get_credential)
self.trigger_edit_credential.connect(self._worker.edit_credential)

self._worker.pin_cache.pin_cleared.connect(
self.common_ui.info.pin_cleared
)
self._worker.pin_cache.pin_cleared.connect(
lambda: self.uncheck_checkbox(True)
)
self._worker.pin_cache.pin_cleared.connect(self.common_ui.info.pin_cleared)
self._worker.pin_cache.pin_cleared.connect(lambda: self.uncheck_checkbox(True))

self._worker.pin_cache.pin_cached.connect(
self.common_ui.info.pin_cached
)
self._worker.pin_cache.pin_cached.connect(self.common_ui.info.pin_cached)
self.common_ui.info.pin_pressed.connect(self._worker.pin_cache.clear)

self._worker.credential_added.connect(self.credential_added)
Expand All @@ -107,12 +99,8 @@ def __init__(self, parent: Optional[QWidget] = None) -> None:
self._worker.otp_generated.connect(self.otp_generated)
self._worker.uncheck_checkbox.connect(self.uncheck_checkbox)

self._worker.received_credential.connect(
self.handle_receive_credential
)
self.next_credential_receiver: Optional[
Callable[[Credential], None]
] = None
self._worker.received_credential.connect(self.handle_receive_credential)
self.next_credential_receiver: Optional[Callable[[Credential], None]] = None

self.data: Optional[DeviceData] = None
self.active_credential: Optional[Credential] = None
Expand Down Expand Up @@ -145,9 +133,7 @@ def __init__(self, parent: Optional[QWidget] = None) -> None:
lambda: self.act_copy_line_edit(self.ui.password)
)

self.action_password_show = self.ui.password.addAction(
icon_visibility, loc
)
self.action_password_show = self.ui.password.addAction(icon_visibility, loc)
self.action_password_show.triggered.connect(self.act_password_show)

self.action_comment_copy = self.ui.comment.addAction(icon_copy, loc)
Expand Down Expand Up @@ -195,16 +181,12 @@ def __init__(self, parent: Optional[QWidget] = None) -> None:
self.ui.username.textChanged.connect(self.check_credential)
self.ui.password.textChanged.connect(self.check_credential)
self.ui.otp.textChanged.connect(self.check_credential)
self.ui.select_algorithm.currentIndexChanged.connect(
self.check_credential
)
self.ui.select_algorithm.currentIndexChanged.connect(self.check_credential)
self.ui.comment.textChanged.connect(self.check_credential)

self.ui.btn_refresh.pressed.connect(self.refresh_credential_list)
self.ui.is_protected.stateChanged.connect(self.refresh_credential_list)
self.ui.secrets_list.currentItemChanged.connect(
self.credential_changed
)
self.ui.secrets_list.currentItemChanged.connect(self.credential_changed)
self.ui.secrets_list.itemClicked.connect(self.credential_clicked)

self.ui.btn_delete.pressed.connect(self.delete_credential)
Expand Down Expand Up @@ -299,10 +281,7 @@ def credentials_listed(self, credentials: list[Credential]) -> None:
active_item = None
for credential in credentials:
item = self.add_credential(credential)
if (
self.active_credential
and credential.id == self.active_credential.id
):
if self.active_credential and credential.id == self.active_credential.id:
active_item = item
self.ui.secrets_list.sortItems()
if active_item:
Expand Down Expand Up @@ -399,9 +378,7 @@ def show_credential(self, credential: Credential) -> None:
self.action_username_copy.setEnabled(False)

if credential.password:
self.ui.password.setText(
credential.password.decode(errors="replace")
)
self.ui.password.setText(credential.password.decode(errors="replace"))
self.action_password_copy.setEnabled(True)
self.action_password_show.setEnabled(True)
else:
Expand All @@ -410,9 +387,7 @@ def show_credential(self, credential: Credential) -> None:
self.action_password_show.setEnabled(False)

if credential.comment:
self.ui.comment.setText(
credential.comment.decode(errors="replace")
)
self.ui.comment.setText(credential.comment.decode(errors="replace"))
self.action_comment_copy.setEnabled(True)
else:
self.ui.comment.clear()
Expand All @@ -439,9 +414,7 @@ def show_credential(self, credential: Credential) -> None:

self.ui.otp.show()
self.ui.otp.setReadOnly(True)
self.ui.algorithm.setText(
str(credential.otp or credential.other) + ":"
)
self.ui.algorithm.setText(str(credential.otp or credential.other) + ":")
self.ui.otp.setPlaceholderText("<hidden>")

if credential.otp:
Expand Down Expand Up @@ -493,16 +466,12 @@ def edit_credential(self, credential: Credential) -> None:
self.ui.username.clear()

if credential.password:
self.ui.password.setText(
credential.password.decode(errors="replace")
)
self.ui.password.setText(credential.password.decode(errors="replace"))
else:
self.ui.password.clear()

if credential.comment:
self.ui.comment.setText(
credential.comment.decode(errors="replace")
)
self.ui.comment.setText(credential.comment.decode(errors="replace"))
else:
self.ui.comment.clear()
self.ui.name.setReadOnly(False)
Expand Down Expand Up @@ -544,9 +513,7 @@ def edit_credential(self, credential: Credential) -> None:
else:
self.ui.algorithm_show.show()
self.ui.algorithm_edit.hide()
self.ui.algorithm.setText(
str(credential.otp or credential.other) + ":"
)
self.ui.algorithm.setText(str(credential.otp or credential.other) + ":")
self.action_otp_edit.setVisible(False)
self.ui.otp.setPlaceholderText("<cannot edit>")

Expand Down Expand Up @@ -697,15 +664,11 @@ def check_credential(self) -> None:
self.common_ui.info.info.emit(
"The HMAC-Secret is not 32 chars long"
)
tool_Tip = (
tool_Tip + "\n- The HMAC-Secret is not 32 chars long"
)
tool_Tip = tool_Tip + "\n- The HMAC-Secret is not 32 chars long"
else:
self.hide_hmac_view()

if algo != "None" and len(check_secret) != len(
check_secret.encode()
):
if algo != "None" and len(check_secret) != len(check_secret.encode()):
can_save = False
self.common_ui.info.info.emit("Invalid character in Secret")
tool_Tip = tool_Tip + "\n- Invalid character in Secret"
Expand Down Expand Up @@ -792,10 +755,7 @@ def show_hmac_view(self) -> None:

def hide_hmac_view(self) -> None:

if (
self.active_credential is None
and self.ui.name_label.text() == "HmacSlot2"
):
if self.active_credential is None and self.ui.name_label.text() == "HmacSlot2":
self.ui.name_label.clear()
self.ui.name_label.hide()
self.ui.name.clear()
Expand Down
4 changes: 1 addition & 3 deletions nitrokeyapp/secrets_tab/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ def list(cls, secrets: SecretsApp) -> list["Credential"]:
credentials.append(cls.from_list_item(item))
return credentials

def extend_with_password_safe_entry(
self, item: PasswordSafeEntry
) -> "Credential":
def extend_with_password_safe_entry(self, item: PasswordSafeEntry) -> "Credential":
if item.login:
self.login = item.login
if item.password:
Expand Down
3 changes: 1 addition & 2 deletions nitrokeyapp/secrets_tab/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def query(self, attempts: int) -> None:
pin, ok = QInputDialog.getText(
self.parent_widget,
"Enter Passwords PIN",
"Please enter the Passwords PIN (remaining retries: "
f"{attempts}):",
"Please enter the Passwords PIN (remaining retries: " f"{attempts}):",
QLineEdit.EchoMode.Password,
)
if ok and pin:
Expand Down
Loading

0 comments on commit ff65458

Please sign in to comment.