Skip to content

Commit

Permalink
Less acceptable ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Sep 17, 2023
1 parent 406bd86 commit ccbc10f
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 124 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ repos:
hooks:
- id: ruff
args: [--fix]
- repo: https://github.com/hhatto/autopep8
rev: "v2.0.4" # Must match requirements-dev.txt
hooks:
- id: autopep8
# - repo: https://github.com/hhatto/autopep8
# rev: "v2.0.4" # Must match requirements-dev.txt
# hooks:
# - id: autopep8
- repo: https://github.com/asottile/add-trailing-comma
rev: v3.1.0 # Must match requirements-dev.txt
hooks:
Expand Down
25 changes: 6 additions & 19 deletions src/AutoSplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def _show_error_signal_slot(error_message_box: Callable[..., object]):

self.setupUi(self)
self.setWindowTitle(
f"AutoSplit v{AUTOSPLIT_VERSION}" +
(" (externally controlled)" if self.is_auto_controlled else ""),
f"AutoSplit v{AUTOSPLIT_VERSION}" + (" (externally controlled)" if self.is_auto_controlled else ""),
)

# Hotkeys need to be initialized to be passed as thread arguments in hotkeys.py
Expand Down Expand Up @@ -331,11 +330,9 @@ def __start_image_function(self):
if below_flag and not self.split_below_threshold and similarity_diff >= 0:
self.split_below_threshold = True
return
if (
(below_flag and self.split_below_threshold and similarity_diff < 0 and is_valid_image(capture))
or (not below_flag and similarity_diff >= 0)
if (below_flag and self.split_below_threshold and similarity_diff < 0 and is_valid_image(capture)) or (
not below_flag and similarity_diff >= 0
):

self.timer_start_image.stop()
self.split_below_threshold = False

Expand Down Expand Up @@ -430,10 +427,7 @@ def __check_fps(self):
self.fps_value_label.setText(str(fps))

def __is_current_split_out_of_range(self):
return (
self.split_image_number < 0
or self.split_image_number > len(self.split_images_and_loop_number) - 1
)
return self.split_image_number < 0 or self.split_image_number > len(self.split_images_and_loop_number) - 1

def undo_split(self, navigate_image_only: bool = False):
"""Undo Split" and "Prev. Img." buttons connect to here."""
Expand Down Expand Up @@ -497,10 +491,7 @@ def reset(self):
# Functions for the hotkeys to return to the main thread from signals and start their corresponding functions
def start_auto_splitter(self):
# If the auto splitter is already running or the button is disabled, don't emit the signal to start it.
if (
self.is_running
or (not self.start_auto_splitter_button.isEnabled() and not self.is_auto_controlled)
):
if self.is_running or (not self.start_auto_splitter_button.isEnabled() and not self.is_auto_controlled):
return

start_label: str = self.start_image_status_value_label.text()
Expand Down Expand Up @@ -533,11 +524,7 @@ def __auto_splitter(self): # noqa: PLR0912,PLR0915

# Construct a list of images + loop count tuples.
self.split_images_and_loop_number = list(
flatten(
[(split_image, i + 1) for i in range(split_image.loops)]
for split_image
in self.split_images
),
flatten([(split_image, i + 1) for i in range(split_image.loops)] for split_image in self.split_images),
)

# Construct groups of splits
Expand Down
4 changes: 1 addition & 3 deletions src/capture_method/DesktopDuplicationCaptureMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def get_frame(self, autosplit: AutoSplit):

left_bounds, top_bounds, *_ = get_window_bounds(hwnd)
self.desktop_duplication.display = next(
display for display
in self.desktop_duplication.displays
if display.hmonitor == hmonitor
display for display in self.desktop_duplication.displays if display.hmonitor == hmonitor
)
offset_x, offset_y, *_ = win32gui.GetWindowRect(hwnd)
offset_x -= self.desktop_duplication.display.position["left"]
Expand Down
3 changes: 1 addition & 2 deletions src/capture_method/VideoCaptureDeviceCaptureMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class VideoCaptureDeviceCaptureMethod(CaptureMethodBase):
name = "Video Capture Device"
short_description = "see below"
description = (
"\nUses a Video Capture Device, like a webcam, virtual cam, or capture card. "
+ "\nYou can select one below. "
"\nUses a Video Capture Device, like a webcam, virtual cam, or capture card. " + "\nYou can select one below. "
)

capture_device: cv2.VideoCapture
Expand Down
7 changes: 3 additions & 4 deletions src/capture_method/WindowsGraphicsCaptureMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ async def coroutine():
# We were too fast and the next frame wasn't ready yet
if not frame:
return None
return await (SoftwareBitmap.create_copy_from_surface_async(frame.surface) or asyncio.sleep(0, None))
return await SoftwareBitmap.create_copy_from_surface_async(frame.surface) or asyncio.sleep(0, None)

try:
software_bitmap = asyncio.run(coroutine())
except SystemError as exception:
Expand Down Expand Up @@ -148,7 +149,5 @@ def recover_window(self, captured_window_title: str, autosplit: AutoSplit):
@override
def check_selected_region_exists(self, autosplit: AutoSplit):
return bool(
is_valid_hwnd(autosplit.hwnd)
and self.frame_pool
and self.session,
is_valid_hwnd(autosplit.hwnd) and self.frame_pool and self.session,
)
15 changes: 3 additions & 12 deletions src/capture_method/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,11 @@ async def get_camera_info(index: int, device_name: str):
# video_capture.release()

resolution = get_input_device_resolution(index)
return CameraInfo(index, device_name, False, backend, resolution) \
if resolution is not None \
else None
return CameraInfo(index, device_name, False, backend, resolution) if resolution is not None else None

# Note: Return type required https://github.com/python/typeshed/issues/2652
future = asyncio.gather(
*[
get_camera_info(index, name) for index, name
in enumerate(named_video_inputs)
],
*[get_camera_info(index, name) for index, name in enumerate(named_video_inputs)],
)

return [
camera_info for camera_info
in await future
if camera_info is not None
]
return [camera_info for camera_info in await future if camera_info is not None]
10 changes: 2 additions & 8 deletions src/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def compare_l2_norm(source: MatLike, capture: MatLike, mask: MatLike | None = No

# The L2 Error is summed across all pixels, so this normalizes
max_error = (
sqrt(source.size) * MAXBYTE
if not is_valid_image(mask)
else sqrt(cv2.countNonZero(mask) * MASK_SIZE_MULTIPLIER)
sqrt(source.size) * MAXBYTE if not is_valid_image(mask) else sqrt(cv2.countNonZero(mask) * MASK_SIZE_MULTIPLIER)
)

if not max_error:
Expand All @@ -73,11 +71,7 @@ def compare_template(source: MatLike, capture: MatLike, mask: MatLike | None = N

# matchTemplate returns the sum of square differences, this is the max
# that the value can be. Used for normalizing from 0 to 1.
max_error = (
source.size * MAXBYTE * MAXBYTE
if not is_valid_image(mask)
else cv2.countNonZero(mask)
)
max_error = source.size * MAXBYTE * MAXBYTE if not is_valid_image(mask) else cv2.countNonZero(mask)

return 1 - (min_val / max_error)

Expand Down
26 changes: 6 additions & 20 deletions src/hotkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ def _send_hotkey(hotkey_or_scan_code: int | str | None):

# Deal with regular inputs
# If an int or does not contain the following strings
if (
isinstance(hotkey_or_scan_code, int)
or not any(key in hotkey_or_scan_code for key in ("num ", "decimal", "+"))
):
if isinstance(hotkey_or_scan_code, int) or not any(key in hotkey_or_scan_code for key in ("num ", "decimal", "+")):
keyboard.send(hotkey_or_scan_code)
return

Expand All @@ -103,11 +100,7 @@ def _send_hotkey(hotkey_or_scan_code: int | str | None):
# keyboard also has issues with capitalization modifier (shift+A)
# keyboard.send(keyboard.key_to_scan_codes(key_or_scan_code)[1])
pyautogui.hotkey(
*[
"+" if key == "plus" else key
for key
in hotkey_or_scan_code.replace(" ", "").split("+")
],
*["+" if key == "plus" else key for key in hotkey_or_scan_code.replace(" ", "").split("+")],
)


Expand All @@ -130,9 +123,7 @@ def __validate_keypad(expected_key: str, keyboard_event: keyboard.KeyboardEvent)
if keyboard_event.name and is_digit(keyboard_event.name[-1]):
# Prevent "regular numbers" and "keypad numbers" from activating each other
return bool(
keyboard_event.is_keypad
if expected_key.startswith("num ")
else not keyboard_event.is_keypad,
keyboard_event.is_keypad if expected_key.startswith("num ") else not keyboard_event.is_keypad,
)

# Prevent "keypad action keys" from triggering "regular numbers" and "keypad numbers"
Expand All @@ -156,9 +147,7 @@ def __get_key_name(keyboard_event: keyboard.KeyboardEvent):
# Normally this is done by keyboard.get_hotkey_name. But our code won't always get there.
if event_name == "+":
return "plus"
return f"num {keyboard_event.name}" \
if keyboard_event.is_keypad and is_digit(keyboard_event.name) \
else event_name
return f"num {keyboard_event.name}" if keyboard_event.is_keypad and is_digit(keyboard_event.name) else event_name


def __get_hotkey_name(names: list[str]):
Expand Down Expand Up @@ -237,11 +226,8 @@ def toggle_auto_reset_image():


def is_valid_hotkey_name(hotkey_name: str):
return any(
key and not keyboard.is_modifier(keyboard.key_to_scan_codes(key)[0])
for key
in hotkey_name.split("+")
)
return any(key and not keyboard.is_modifier(keyboard.key_to_scan_codes(key)[0]) for key in hotkey_name.split("+"))


# TODO: using getattr/setattr is NOT a good way to go about this. It was only temporarily done to
# reduce duplicated code. We should use a dictionary of hotkey class or something.
Expand Down
43 changes: 20 additions & 23 deletions src/menu_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ def __init__(self, autosplit: AutoSplit):
# Don't autofocus any particular field
self.setFocus()


# region Build the Capture method combobox
# region Build the Capture method combobox
capture_method_values = CAPTURE_METHODS.values()
self.__set_all_capture_devices()

Expand All @@ -159,17 +158,13 @@ def __init__(self, autosplit: AutoSplit):
# list_view.setFixedWidth(self.capture_method_combobox.width())
# self.capture_method_combobox.setView(list_view)

self.capture_method_combobox.addItems([
f"- {method.name} ({method.short_description})"
for method in capture_method_values
])
self.capture_method_combobox.addItems(
[f"- {method.name} ({method.short_description})" for method in capture_method_values]
)
self.capture_method_combobox.setToolTip(
"\n\n".join([
f"{method.name} :\n{method.description}"
for method in capture_method_values
]),
"\n\n".join([f"{method.name} :\n{method.description}" for method in capture_method_values]),
)
# endregion
# endregion

self.__setup_bindings()

Expand Down Expand Up @@ -243,21 +238,21 @@ def __set_all_capture_devices(self):
if len(self.__video_capture_devices) > 0:
for i in range(self.capture_device_combobox.count()):
self.capture_device_combobox.removeItem(i)
self.capture_device_combobox.addItems([
f"* {device.name}"
+ (f" [{device.backend}]" if device.backend else "")
+ (" (occupied)" if device.occupied else "")
for device in self.__video_capture_devices
])
self.capture_device_combobox.addItems(
[
f"* {device.name}"
+ (f" [{device.backend}]" if device.backend else "")
+ (" (occupied)" if device.occupied else "")
for device in self.__video_capture_devices
]
)
self.__enable_capture_device_if_its_selected_method()
else:
self.capture_device_combobox.setPlaceholderText("No device found.")

def __set_readme_link(self):
self.custom_image_settings_info_label.setText(
self.custom_image_settings_info_label
.text()
.format(GITHUB_REPOSITORY=GITHUB_REPOSITORY),
self.custom_image_settings_info_label.text().format(GITHUB_REPOSITORY=GITHUB_REPOSITORY),
)
# HACK: This is a workaround because custom_image_settings_info_label
# simply will not open links with a left click no matter what we tried.
Expand Down Expand Up @@ -296,7 +291,7 @@ def hotkey_connect(hotkey: Hotkey):
set_hotkey_hotkey_button.setEnabled(False)
hotkey_input.setEnabled(False)

# region Set initial values
# region Set initial values
# Capture Settings
self.fps_limit_spinbox.setValue(self.autosplit.settings_dict["fps_limit"])
self.live_capture_region_checkbox.setChecked(self.autosplit.settings_dict["live_capture_region"])
Expand All @@ -318,8 +313,8 @@ def hotkey_connect(hotkey: Hotkey):
self.loop_splits_checkbox.setChecked(self.autosplit.settings_dict["loop_splits"])
self.start_also_resets_checkbox.setChecked(self.autosplit.settings_dict["start_also_resets"])
self.enable_auto_reset_image_checkbox.setChecked(self.autosplit.settings_dict["enable_auto_reset"])
# endregion
# region Binding
# endregion
# region Binding
# Capture Settings
self.fps_limit_spinbox.valueChanged.connect(self.__fps_limit_changed)
self.live_capture_region_checkbox.stateChanged.connect(
Expand Down Expand Up @@ -359,6 +354,8 @@ def hotkey_connect(hotkey: Hotkey):
self.enable_auto_reset_image_checkbox.stateChanged.connect(
lambda: self.__set_value("enable_auto_reset", self.enable_auto_reset_image_checkbox.isChecked()),
)


# endregion


Expand Down
15 changes: 3 additions & 12 deletions src/split_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ def parse_and_validate_images(autosplit: AutoSplit):
# Get split images
all_images = [
AutoSplitImage(os.path.join(autosplit.settings_dict["split_image_directory"], image_name))
for image_name
in os.listdir(autosplit.settings_dict["split_image_directory"])
for image_name in os.listdir(autosplit.settings_dict["split_image_directory"])
]

# Find non-split images and then remove them from the list
Expand All @@ -190,19 +189,11 @@ def parse_and_validate_images(autosplit: AutoSplit):
error_message: Callable[[], object] | None = None

# If there is no start hotkey set but a Start Image is present, and is not auto controlled, throw an error.
if (
start_image
and not autosplit.settings_dict["split_hotkey"]
and not autosplit.is_auto_controlled
):
if start_image and not autosplit.settings_dict["split_hotkey"] and not autosplit.is_auto_controlled:
error_message = error_messages.load_start_image

# If there is no reset hotkey set but a Reset Image is present, and is not auto controlled, throw an error.
elif (
reset_image
and not autosplit.settings_dict["reset_hotkey"]
and not autosplit.is_auto_controlled
):
elif reset_image and not autosplit.settings_dict["reset_hotkey"] and not autosplit.is_auto_controlled:
error_message = error_messages.reset_hotkey

# Make sure that each of the images follows the guidelines for correct format
Expand Down
14 changes: 3 additions & 11 deletions src/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,7 @@ def load_settings(autosplit: AutoSplit, from_path: str = ""):


def load_settings_on_open(autosplit: AutoSplit):
settings_files = [
file for file
in os.listdir(auto_split_directory)
if file.endswith(".toml")
]
settings_files = [file for file in os.listdir(auto_split_directory) if file.endswith(".toml")]

# Find all .tomls in AutoSplit folder, error if there is not exactly 1
error = None
Expand All @@ -206,16 +202,12 @@ def load_check_for_updates_on_open(autosplit: AutoSplit):
# TODO: Report this issue upstream
value = cast(
bool,
QtCore
.QSettings("AutoSplit", "Check For Updates On Open")
.value("check_for_updates_on_open", True, type=bool),
QtCore.QSettings("AutoSplit", "Check For Updates On Open").value("check_for_updates_on_open", True, type=bool),
)
autosplit.action_check_for_updates_on_open.setChecked(value)


def set_check_for_updates_on_open(design_window: design.Ui_MainWindow, value: bool):
"""Sets the "Check For Updates On Open" QSettings value and the checkbox state."""
design_window.action_check_for_updates_on_open.setChecked(value)
QtCore \
.QSettings("AutoSplit", "Check For Updates On Open") \
.setValue("check_for_updates_on_open", value)
QtCore.QSettings("AutoSplit", "Check For Updates On Open").setValue("check_for_updates_on_open", value)
Loading

0 comments on commit ccbc10f

Please sign in to comment.