Skip to content

Commit

Permalink
Merge pull request #1 from gabrielguarisa/feature/base-node
Browse files Browse the repository at this point in the history
Feature/base node
  • Loading branch information
gabrielguarisa authored Jan 3, 2022
2 parents d15f264 + c88ca4a commit 0d9d988
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 259 deletions.
22 changes: 9 additions & 13 deletions gurun/cv/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ def __init__(
threshold: float = 0.7,
single_match: bool = False,
method: int = cv2.TM_CCOEFF_NORMED,
*args: Any,
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
super().__init__(**kwargs)
if isinstance(target, str):
target = cv2.imread(target)
if target is None:
Expand All @@ -36,7 +35,7 @@ def __init__(
self._single_match = single_match
self._method = method

def __call__(
def run(
self, image: Union[np.ndarray, str], *args: Any, **kwargs: Any
) -> List[List[int]]:
if isinstance(image, str):
Expand All @@ -56,26 +55,23 @@ def __call__(
rectangles, _ = cv2.groupRectangles(rectangles, 1, 0.2)

if len(rectangles) == 0:
self._output = None
self._state = False
else:
self._output = rectangles[0] if self._single_match else rectangles
self._state = True
self.state = False
return None

return self._output
self.state = True
return rectangles[0] if self._single_match else rectangles


class TemplateDetectionFrom(TemplateDetection):
def __init__(
self,
source_node: Node,
*args: Any,
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
super().__init__(**kwargs)
self._source_node = source_node

def __call__(self, *args: Any, **kwargs: Any) -> List[List[int]]:
def run(self, *args: Any, **kwargs: Any) -> List[List[int]]:
image = self._source_node(*args, **kwargs)

return super().__call__(image, *args, **kwargs)
return super().run(image, *args, **kwargs)
21 changes: 9 additions & 12 deletions gurun/cv/transformation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List
from typing import Any

import math
import random
Expand All @@ -15,16 +15,13 @@


class Transformation(Node):
def __call__(self, detections: np.ndarray, *args: Any, **kwargs: Any) -> Any:
def run(self, detections: np.ndarray, *args: Any, **kwargs: Any) -> Any:
if detections is None:
self._output = None
self._state = False
self.state = False
return None

self._output = self._transform(detections, *args, **kwargs)
self._state = True

return self.output
self.state = True
return self._transform(detections, *args, **kwargs)

def _transform(self, detections: np.ndarray, *args: Any, **kwargs: Any) -> Any:
raise NotImplementedError()
Expand All @@ -48,8 +45,8 @@ def _transform(self, detections: np.ndarray, *args: Any, **kwargs: Any) -> Any:


class NaturalRectToPoint(Transformation):
def __init__(self, border_proportion: float = 0.25, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
def __init__(self, border_proportion: float = 0.25, **kwargs: Any):
super().__init__(**kwargs)
self._border_proportion = border_proportion

def __natural_range(self, value: int, limit: int) -> int:
Expand Down Expand Up @@ -77,8 +74,8 @@ def _transform(self, detections: np.ndarray, *args: Any, **kwargs: Any) -> Any:


class Offset(Transformation):
def __init__(self, xOffset: int = 0, yOffset: int = 0, *args, **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, xOffset: int = 0, yOffset: int = 0, **kwargs):
super().__init__(**kwargs)
self._xOffset = xOffset
self._yOffset = yOffset

Expand Down
23 changes: 22 additions & 1 deletion gurun/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
class RunnerException(Exception): # pragma: no cover
class GurunException(Exception):
"""
Base class for all exceptions in Gurun.
"""

pass


class RunnerException(GurunException):
"""
Raised when something goes wrong with the runner.
"""

pass


class GurunTypeError(GurunException, TypeError):
"""
Raised when a type error occurs.
"""

def __init__(self, var_name: str, expected_type: str, received_type: str) -> None:
super().__init__(f"{var_name} must be {expected_type}, got {received_type}")
42 changes: 20 additions & 22 deletions gurun/gui/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,54 @@


class Typewrite(WrapperNode):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(pyautogui.typewrite, *args, **kwargs)
def __init__(self, **kwargs: Any) -> None:
super().__init__(pyautogui.typewrite, **kwargs)


class Scroll(Node):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(pyautogui.scroll, *args, **kwargs)
def __init__(self, **kwargs: Any) -> None:
super().__init__(pyautogui.scroll, **kwargs)


class Click(WrapperNode):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(pyautogui.click, *args, **kwargs)
super().__init__(pyautogui.click, **kwargs)


class HotKey(WrapperNode):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(pyautogui.hotkey, *args, **kwargs)
super().__init__(pyautogui.hotkey, **kwargs)


class MoveRel(Node):
def __init__(
self,
x: int = 0,
y: int = 0,
*args: Any,
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
super().__init__(**kwargs)
self._x = x
self._y = y

def __call__(self, *args: Any, **kwargs: Any) -> Any:
pyautogui.moveRel(self._x, self._y, **self._memory)
def run(self, *args: Any, **kwargs: Any) -> Any:
pyautogui.moveRel(self._x, self._y)


class MoveTo(WrapperNode):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(pyautogui.moveTo, *args, **kwargs)
def __init__(self, **kwargs: Any) -> None:
super().__init__(pyautogui.moveTo, **kwargs)


class DragRel(WrapperNode):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(pyautogui.dragRel, *args, **kwargs)
def __init__(self, **kwargs: Any) -> None:
super().__init__(pyautogui.dragRel, **kwargs)


class MultipleClicks(Click):
def __call__(self, positions: List[List[int]], *args: Any, **kwargs: Any):
def run(self, positions: List[List[int]], *args: Any, **kwargs: Any):
for x, y in positions:
super().__call__(*args, x=x, y=y, **kwargs)
super().run(*args, x=x, y=y, **kwargs)


class NaturalClick(Click):
Expand All @@ -74,16 +73,15 @@ def __init__(
],
minimum_duration: int = 1,
maximum_duration: int = 1.5,
*args: Any,
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
super().__init__(**kwargs)
self._easing_functions = easing_functions
self._minimum_duration = minimum_duration
self._maximum_duration = maximum_duration

def __call__(self, *args: Any, **kwargs: Any):
return super().__call__(
def run(self, *args: Any, **kwargs: Any):
return super().run(
*args,
tween=random.choice(self._easing_functions),
duration=random.uniform(self._minimum_duration, self._maximum_duration),
Expand All @@ -92,6 +90,6 @@ def __call__(self, *args: Any, **kwargs: Any):


class MultipleNaturalClicks(NaturalClick):
def __call__(self, positions: List[List[int]], *args: Any, **kwargs: Any):
def run(self, positions: List[List[int]], *args: Any, **kwargs: Any):
for x, y in positions:
super().__call__(*args, x=x, y=y, **kwargs)
super().run(*args, x=x, y=y, **kwargs)
19 changes: 9 additions & 10 deletions gurun/gui/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
import subprocess

from gurun import Node
from gurun.node import WrapperNode


class Subprocess(Node):
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(**kwargs)
self._commands = args
class Subprocess(WrapperNode):
def __init__(self, *popenargs: Any, **kwargs: Any):
super().__init__(subprocess.run, **kwargs)
self._popenargs = popenargs

def __call__(self, *args: Any, **kwargs: Any) -> Any:
self._state = True
self._output = subprocess.run(self._commands, *args, **kwargs, **self._memory)
return self._output
def run(self, *args: Any, **kwargs: Any) -> Any:
return super().run(*self._popenargs, *args, **kwargs)


class Workspace(Subprocess):
def __init__(self, workspace: str, os: str, *args: Any, **kwargs: Any):
def __init__(self, workspace: str, os: str, **kwargs: Any):
if os.lower() == "linux":
super().__init__("wmctrl", "-s", workspace, *args, **kwargs)
super().__init__("wmctrl", "-s", workspace, **kwargs)
else:
raise ValueError("Workspace is only available on Linux")
18 changes: 8 additions & 10 deletions gurun/gui/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,25 @@


class ScreenshotPAG(Node):
def __call__(self, filename: str = None, *args: Any, **kwargs: Any) -> np.ndarray:
def run(self, filename: str = None, *args: Any, **kwargs: Any) -> np.ndarray:
image = pyautogui.screenshot()

if filename is not None:
image.save(filename)

self._output = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
return self.output
return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)


class ScreenshotMMS(Node):
def __init__(self, monitor: int = 0, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
def __init__(self, monitor: int = 0, **kwargs: Any) -> None:
super().__init__(**kwargs)
self._monitor = monitor

def __call__(self, filename: str = None, *args: Any, **kwargs: Any) -> np.ndarray:

def run(self, filename: str = None, *args: Any, **kwargs: Any) -> np.ndarray:
with mss.mss() as sct:
self._output = np.array(sct.grab(sct.monitors[self._monitor]))[:, :, :3]
output = np.array(sct.grab(sct.monitors[self._monitor]))[:, :, :3]

if filename is not None:
cv2.imwrite(filename, self.output)
cv2.imwrite(filename, output)

return self.output
return output
Loading

0 comments on commit 0d9d988

Please sign in to comment.