Skip to content

Commit

Permalink
MNT: Adding method to find images and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
hhslepicka committed Jul 7, 2021
1 parent 3283663 commit 5132f07
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

# Prerequisites
* Python 3.7+
* Pillow

Python package requirements are listed in the requirements.txt file, which can
be used to install all requirements from pip: 'pip install -r requirements.txt'
Expand Down
22 changes: 22 additions & 0 deletions botcity/base/bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import pathlib
import sys
from os import path
from PIL import Image


class BaseBot:
Expand Down Expand Up @@ -30,6 +33,25 @@ def _resources_path(self, resource_folder="resources"):
path_to_class = sys.modules[self.__module__].__file__
return path.join(path.dirname(path.realpath(path_to_class)), resource_folder)

def _search_image_file(self, label):
img_path = self.state.map_images.get(label)
if img_path:
return img_path

search_path = [self.get_resource_abspath(""), os.getcwd()]
for sp in search_path:
path = pathlib.Path(sp)
found = path.glob(f"{label}.*")
for f in found:
try:
img = Image.open(f)
except Exception:
continue
else:
img.close()
return str(f.absolute())
return None

@classmethod
def main(cls):
try:
Expand Down
32 changes: 6 additions & 26 deletions botcity/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import platform
import subprocess

from .state import SingleState


def is_retina():
"""
Expand All @@ -20,41 +18,23 @@ def is_retina():
return False


def ensure_state(func):
"""
Decorator to ensure that a State instance is being provided.
If no State instance is provided it uses the singleton SingleState.
Args:
func (callable): The function to be wrapped
Returns:
wrapper (callable): The decorated function
"""
@wraps(func)
def wrapper(*args, **kwargs):
if 'state' not in kwargs:
kwargs['state'] = SingleState()
return func(*args, **kwargs)
return wrapper


def only_if_element(func):
"""
Decorator which raises if element is None.
Decorator which raises if element is None in class State.
Args:
func (callable): The function to be wrapped
Returns:
wrapper (callable): The decorated function
"""

@wraps(func)
def wrapper(*args, **kwargs):
state = kwargs.get('state', SingleState())
if state.element is None:
def wrapper(self, *args, **kwargs):
if self.state.element is None:
raise ValueError(f'Element not available. Cannot invoke {func.__name__}.')
return func(*args, **kwargs)
return func(self, *args, **kwargs)

return wrapper


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pillow

0 comments on commit 5132f07

Please sign in to comment.