Skip to content

Commit

Permalink
Add black linting to the CI and perform initial auto-reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
laracroft37 committed Aug 27, 2024
1 parent 017cd33 commit d1125f3
Show file tree
Hide file tree
Showing 18 changed files with 2,811 additions and 1,254 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Lint Check
on: [push, pull_request]

jobs:
mypy:
lint:
runs-on: ubuntu-latest

steps:
Expand All @@ -17,8 +17,9 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install mypy
pip install mypy black
- name: Run mypy
- name: Run linters
run: |
mypy guibot
black --check --diff --color guibot
234 changes: 178 additions & 56 deletions guibot/calibrator.py

Large diffs are not rendered by default.

122 changes: 93 additions & 29 deletions guibot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from .errors import *

log = logging.getLogger('guibot.config')
log = logging.getLogger("guibot.config")


class GlobalConfig(type):
Expand Down Expand Up @@ -91,6 +91,7 @@ def toggle_delay(self, value: float = None) -> float | None:
else:
GlobalConfig._toggle_delay = value
return None

#: time interval between mouse down and up in a click
toggle_delay = property(fget=toggle_delay, fset=toggle_delay)

Expand All @@ -105,10 +106,11 @@ def click_delay(self, value: float = None) -> float | None:
else:
GlobalConfig._click_delay = value
return None

#: time interval after a click (in a double or n-click)
click_delay = property(fget=click_delay, fset=click_delay)

def delay_after_drag(self, value: float = None) -> float | None:
def delay_after_drag(self, value: float = None) -> float | None:
"""
Same as :py:func:`GlobalConfig.toggle_delay` but with
Expand All @@ -119,6 +121,7 @@ def delay_after_drag(self, value: float = None) -> float | None:
else:
GlobalConfig._drag_delay = value
return None

#: timeout before drag operation
delay_after_drag = property(fget=delay_after_drag, fset=delay_after_drag)

Expand All @@ -133,6 +136,7 @@ def delay_before_drop(self, value: float = None) -> float | None:
else:
GlobalConfig._drop_delay = value
return None

#: timeout before drop operation
delay_before_drop = property(fget=delay_before_drop, fset=delay_before_drop)

Expand All @@ -147,6 +151,7 @@ def delay_before_keys(self, value: float = None) -> float | None:
else:
GlobalConfig._keys_delay = value
return None

#: timeout before key press operation
delay_before_keys = property(fget=delay_before_keys, fset=delay_before_keys)

Expand All @@ -161,6 +166,7 @@ def delay_between_keys(self, value: float = None) -> float | None:
else:
GlobalConfig._type_delay = value
return None

#: time interval between two consecutively typed keys
delay_between_keys = property(fget=delay_between_keys, fset=delay_between_keys)

Expand All @@ -176,8 +182,11 @@ def rescan_speed_on_find(self, value: float = None) -> float | None:
else:
GlobalConfig._rescan_speed_on_find = value
return None

#: time interval between two image matching attempts (used to reduce overhead on the CPU)
rescan_speed_on_find = property(fget=rescan_speed_on_find, fset=rescan_speed_on_find)
rescan_speed_on_find = property(
fget=rescan_speed_on_find, fset=rescan_speed_on_find
)

def wait_for_animations(self, value: bool = None) -> bool | None:
"""
Expand All @@ -198,6 +207,7 @@ def wait_for_animations(self, value: bool = None) -> bool | None:
return None
else:
raise ValueError

#: whether to wait for animations to complete and match only static (not moving) targets
wait_for_animations = property(fget=wait_for_animations, fset=wait_for_animations)

Expand All @@ -219,6 +229,7 @@ def smooth_mouse_drag(self, value: bool = None) -> bool | None:
return None
else:
raise ValueError

#: whether to move the mouse cursor to a location instantly or smoothly
smooth_mouse_drag = property(fget=smooth_mouse_drag, fset=smooth_mouse_drag)

Expand All @@ -239,8 +250,11 @@ def preprocess_special_chars(self, value: bool = None) -> bool | None:
return None
else:
raise ValueError

#: whether to preprocess capital and special characters and handle them internally
preprocess_special_chars = property(fget=preprocess_special_chars, fset=preprocess_special_chars)
preprocess_special_chars = property(
fget=preprocess_special_chars, fset=preprocess_special_chars
)

def save_needle_on_error(self, value: bool = None) -> bool | None:
"""
Expand All @@ -255,8 +269,11 @@ def save_needle_on_error(self, value: bool = None) -> bool | None:
return None
else:
raise ValueError

#: whether to perform an extra needle dump on matching error
save_needle_on_error = property(fget=save_needle_on_error, fset=save_needle_on_error)
save_needle_on_error = property(
fget=save_needle_on_error, fset=save_needle_on_error
)

def image_logging_level(self, value: int = None) -> int | None:
"""
Expand All @@ -272,10 +289,11 @@ def image_logging_level(self, value: int = None) -> int | None:
else:
GlobalConfig._image_logging_level = value
return None

#: logging level similar to the python logging module
image_logging_level = property(fget=image_logging_level, fset=image_logging_level)

def image_logging_step_width(self, value: int = None) -> int | None:
def image_logging_step_width(self, value: int = None) -> int | None:
"""
Same as :py:func:`GlobalConfig.image_logging_level` but with
Expand All @@ -287,8 +305,11 @@ def image_logging_step_width(self, value: int = None) -> int | None:
else:
GlobalConfig._image_logging_step_width = value
return None

#: number of digits when enumerating the image logging steps, e.g. value=3 for 001, 002, etc.
image_logging_step_width = property(fget=image_logging_step_width, fset=image_logging_step_width)
image_logging_step_width = property(
fget=image_logging_step_width, fset=image_logging_step_width
)

def image_quality(self, value: int = None) -> int | None:
"""
Expand All @@ -303,6 +324,7 @@ def image_quality(self, value: int = None) -> int | None:
else:
GlobalConfig._image_quality = value
return None

#: quality of the image dumps ranging from 0 for no compression to 9 for maximum compression
# (used to save space and reduce the disk space needed for image logging)
image_quality = property(fget=image_quality, fset=image_quality)
Expand All @@ -319,8 +341,11 @@ def image_logging_destination(self, value: str = None) -> str | None:
else:
GlobalConfig._image_logging_destination = value
return None

#: relative path of the image logging steps
image_logging_destination = property(fget=image_logging_destination, fset=image_logging_destination)
image_logging_destination = property(
fget=image_logging_destination, fset=image_logging_destination
)

def display_control_backend(self, value: str = None) -> str | None:
"""
Expand Down Expand Up @@ -353,8 +378,11 @@ def display_control_backend(self, value: str = None) -> str | None:
raise ValueError("Unsupported backend for GUI actions '%s'" % value)
GlobalConfig._display_control_backend = value
return None

#: name of the display control backend
display_control_backend = property(fget=display_control_backend, fset=display_control_backend)
display_control_backend = property(
fget=display_control_backend, fset=display_control_backend
)

# these methods do not check for valid values since this
# is already done during region and target initialization
Expand Down Expand Up @@ -389,6 +417,7 @@ def find_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._find_backend = value
return None

#: name of the computer vision backend
find_backend = property(fget=find_backend, fset=find_backend)

Expand All @@ -405,8 +434,11 @@ def contour_threshold_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._contour_threshold_backend = value
return None

#: name of the contour threshold backend
contour_threshold_backend = property(fget=contour_threshold_backend, fset=contour_threshold_backend)
contour_threshold_backend = property(
fget=contour_threshold_backend, fset=contour_threshold_backend
)

def template_match_backend(self, value: str = None) -> str | None:
"""
Expand All @@ -422,8 +454,11 @@ def template_match_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._template_match_backend = value
return None

#: name of the template matching backend
template_match_backend = property(fget=template_match_backend, fset=template_match_backend)
template_match_backend = property(
fget=template_match_backend, fset=template_match_backend
)

def feature_detect_backend(self, value: str = None) -> str | None:
"""
Expand All @@ -439,8 +474,11 @@ def feature_detect_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._feature_detect_backend = value
return None

#: name of the feature detection backend
feature_detect_backend = property(fget=feature_detect_backend, fset=feature_detect_backend)
feature_detect_backend = property(
fget=feature_detect_backend, fset=feature_detect_backend
)

def feature_extract_backend(self, value: str = None) -> str | None:
"""
Expand All @@ -455,8 +493,11 @@ def feature_extract_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._feature_extract_backend = value
return None

#: name of the feature extraction backend
feature_extract_backend = property(fget=feature_extract_backend, fset=feature_extract_backend)
feature_extract_backend = property(
fget=feature_extract_backend, fset=feature_extract_backend
)

def feature_match_backend(self, value: str = None) -> str | None:
"""
Expand All @@ -471,8 +512,11 @@ def feature_match_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._feature_match_backend = value
return None

#: name of the feature matching backend
feature_match_backend = property(fget=feature_match_backend, fset=feature_match_backend)
feature_match_backend = property(
fget=feature_match_backend, fset=feature_match_backend
)

def text_detect_backend(self, value: str = None) -> str | None:
"""
Expand All @@ -487,6 +531,7 @@ def text_detect_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._text_detect_backend = value
return None

#: name of the text detection backend
text_detect_backend = property(fget=text_detect_backend, fset=text_detect_backend)

Expand All @@ -503,6 +548,7 @@ def text_ocr_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._text_ocr_backend = value
return None

#: name of the optical character recognition backend
text_ocr_backend = property(fget=text_ocr_backend, fset=text_ocr_backend)

Expand All @@ -519,6 +565,7 @@ def deep_learn_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._deep_learn_backend = value
return None

#: name of the deep learning backend
deep_learn_backend = property(fget=deep_learn_backend, fset=deep_learn_backend)

Expand All @@ -535,8 +582,11 @@ def hybrid_match_backend(self, value: str = None) -> str | None:
else:
GlobalConfig._hybrid_match_backend = value
return None

#: name of the hybrid matching backend for unconfigured one-step targets
hybrid_match_backend = property(fget=hybrid_match_backend, fset=hybrid_match_backend)
hybrid_match_backend = property(
fget=hybrid_match_backend, fset=hybrid_match_backend
)


class GlobalConfig(object, metaclass=GlobalConfig): # type: ignore
Expand Down Expand Up @@ -639,24 +689,30 @@ def __init__(self, configure: bool = True, synchronize: bool = True) -> None:
if synchronize:
self.__synchronize_backend()

def __configure_backend(self, backend: str = None, category: str ="type",
reset: bool = False) -> None:
def __configure_backend(
self, backend: str = None, category: str = "type", reset: bool = False
) -> None:
if category != "type":
raise UnsupportedBackendError("Backend category '%s' is not supported" % category)
raise UnsupportedBackendError(
"Backend category '%s' is not supported" % category
)
if reset:
# reset makes no sense here since this is the base configuration
pass
if backend is None:
backend = "cv"
if backend not in self.algorithms[self.categories[category]]:
raise UnsupportedBackendError("Backend '%s' is not among the supported ones: "
"%s" % (backend, self.algorithms[self.categories[category]]))
raise UnsupportedBackendError(
"Backend '%s' is not among the supported ones: "
"%s" % (backend, self.algorithms[self.categories[category]])
)

self.params[category] = {}
self.params[category]["backend"] = backend

def configure_backend(self, backend: str = None, category: str = "type",
reset: bool = False) -> None:
def configure_backend(
self, backend: str = None, category: str = "type", reset: bool = False
) -> None:
"""
Generate configuration dictionary for a given backend.
Expand All @@ -681,20 +737,26 @@ def configure(self, reset: bool = True, **kwargs: dict[str, type]) -> None:
"""
self.configure_backend(reset=reset)

def __synchronize_backend(self, backend: str = None, category: str = "type",
reset: bool = False) -> None:
def __synchronize_backend(
self, backend: str = None, category: str = "type", reset: bool = False
) -> None:
if category != "type":
raise UnsupportedBackendError("Backend category '%s' is not supported" % category)
raise UnsupportedBackendError(
"Backend category '%s' is not supported" % category
)
if reset:
# reset makes no sense here since this is the base configuration
pass
# no backend object to sync to
backend = "cv" if backend is None else backend
if backend not in self.algorithms[self.categories[category]]:
raise UninitializedBackendError("Backend '%s' has not been configured yet" % backend)
raise UninitializedBackendError(
"Backend '%s' has not been configured yet" % backend
)

def synchronize_backend(self, backend: str = None, category: str = "type",
reset: bool = False) -> None:
def synchronize_backend(
self, backend: str = None, category: str = "type", reset: bool = False
) -> None:
"""
Synchronize a category backend with the equalizer configuration.
Expand All @@ -707,7 +769,9 @@ def synchronize_backend(self, backend: str = None, category: str = "type",
"""
self.__synchronize_backend(backend, category, reset)

def synchronize(self, *args: tuple[type, ...], reset: bool = True, **kwargs: dict[str, type]) -> None:
def synchronize(
self, *args: tuple[type, ...], reset: bool = True, **kwargs: dict[str, type]
) -> None:
"""
Synchronize all backends with the current configuration dictionary.
Expand Down
Loading

0 comments on commit d1125f3

Please sign in to comment.