Skip to content

Commit

Permalink
Merge pull request #3 from pevogam/initial-lgtm-autoreview
Browse files Browse the repository at this point in the history
This branch addresses the initial alerts raised by the LGTM automated review
  • Loading branch information
pevogam authored Dec 2, 2019
2 parents 411617c + b3ad40b commit eaf6ddd
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 28 deletions.
6 changes: 2 additions & 4 deletions guibot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# You should have received a copy of the GNU Lesser General Public License
# along with guibot. If not, see <http://www.gnu.org/licenses/>.

import re
import os
import logging
log = logging.getLogger('guibot.config')

Expand Down Expand Up @@ -567,7 +565,7 @@ def configure_backend(self, backend=None, category="type", reset=False):
"""
self.__configure_backend(backend, category, reset)

def configure(self, reset=True):
def configure(self, reset=True, **kwargs):
"""
Generate configuration dictionary for all backends.
Expand Down Expand Up @@ -604,7 +602,7 @@ def synchronize_backend(self, backend=None, category="type", reset=False):
"""
self.__synchronize_backend(backend, category, reset)

def synchronize(self, reset=True):
def synchronize(self, *args, reset=True, **kwargs):
"""
Synchronize all backends with the current configuration dictionary.
Expand Down
10 changes: 7 additions & 3 deletions guibot/desktopcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
from .errors import *


__all__ = ['DesktopControl', 'AutoPyDesktopControl', 'XDoToolDesktopControl',
'VNCDoToolDesktopControl', 'QemuDesktopControl']


class DesktopControl(LocalConfig):
"""
Desktop control backend, responsible for performing desktop operations
Expand Down Expand Up @@ -972,11 +976,11 @@ def keys_toggle(self, keys, up_down):
"(" : '0x1a',
")" : '0x1b'
}
espaced_keys = []
for key in keys:
if qemu_escape_map.has_key(key):
key = qemu_escape_map[key]
espaced_keys += [qemu_escape_map[key] if qemu_escape_map.has_key(key) else key]
# TODO: test and handle longer hold
self._backend_obj.sendkey("-".join(keys), hold_time=1)
self._backend_obj.sendkey("-".join(espaced_keys), hold_time=1)

def keys_type(self, text, modifiers):
"""
Expand Down
8 changes: 8 additions & 0 deletions guibot/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
# You should have received a copy of the GNU Lesser General Public License
# along with guibot. If not, see <http://www.gnu.org/licenses/>.


__all__ = ['GuiBotError', 'FileNotFoundError',
'IncompatibleTargetError', 'IncompatibleTargetFileError',
'FindError', 'NotFindError',
'UnsupportedBackendError', 'MissingHotmapError',
'UninitializedBackendError']


class GuiBotError(Exception):
"""GuiBot exception base class"""

Expand Down
31 changes: 17 additions & 14 deletions guibot/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
log = logging.getLogger('guibot.finder')


__all__ = ['CVParameter', 'Finder', 'AutoPyFinder', 'ContourFinder', 'TemplateFinder',
'FeatureFinder', 'CascadeFinder', 'TextFinder', 'TemplateFeatureFinder',
'DeepFinder', 'CustomFinder', 'HybridFinder']


class CVParameter(object):
"""A class for a single parameter used for CV backend configuration."""

Expand Down Expand Up @@ -615,11 +620,11 @@ def configure_backend(self, backend=None, category="contour", reset=False):
"""
self.__configure_backend(backend, category, reset)

def __configure(self, threshold_filter=None, reset=True):
def __configure(self, threshold_filter=None, reset=True, **kwargs):
self.__configure_backend(category="contour", reset=reset)
self.__configure_backend(threshold_filter, "threshold")

def configure(self, threshold_filter=None, reset=True):
def configure(self, threshold_filter=None, reset=True, **kwargs):
"""
Custom implementation of the base method.
Expand Down Expand Up @@ -1166,14 +1171,14 @@ def configure_backend(self, backend=None, category="feature", reset=False):
self.__configure_backend(backend, category, reset)

def __configure(self, feature_detect=None, feature_extract=None,
feature_match=None, reset=True):
feature_match=None, reset=True, **kwargs):
self.__configure_backend(category="feature", reset=reset)
self.__configure_backend(feature_detect, "fdetect")
self.__configure_backend(feature_extract, "fextract")
self.__configure_backend(feature_match, "fmatch")

def configure(self, feature_detect=None, feature_extract=None,
feature_match=None, reset=True):
feature_match=None, reset=True, **kwargs):
"""
Custom implementation of the base method.
Expand Down Expand Up @@ -1372,7 +1377,6 @@ def _detect_features(self, ngray, hgray, detect, extract):
Detect all keypoints and calculate their respective decriptors.
"""
nkeypoints, hkeypoints = [], []
nfactor = self.params["fdetect"]["nzoom"].value
hfactor = self.params["fdetect"]["hzoom"].value

Expand Down Expand Up @@ -1906,7 +1910,7 @@ def __configure(self, text_detector=None, text_recognizer=None,

def configure(self, text_detector=None, text_recognizer=None,
threshold_filter=None, threshold_filter2=None,
threshold_filter3=None, reset=True):
threshold_filter3=None, reset=True, **kwargs):
"""
Custom implementation of the base method.
Expand Down Expand Up @@ -2317,9 +2321,9 @@ def _detect_text_components(self, haystack):
raise NotImplementedError("The connected components method for text detection needs more labels")

# TODO: alternatively use cvBlobsLib
myblobs = CBlobResult(binary_image, mask, 0, True)
myblobs.filter_blobs(325, 2000)
blob_count = myblobs.GetNumBlobs()
# myblobs = CBlobResult(binary_image, mask, 0, True)
# myblobs.filter_blobs(325, 2000)
# blob_count = myblobs.GetNumBlobs()

def log(self, lvl):
"""
Expand Down Expand Up @@ -2423,7 +2427,8 @@ def __configure(self, template_match=None, feature_detect=None,
self.__configure_backend(feature_match, "fmatch")

def configure(self, template_match=None, feature_detect=None,
feature_extract=None, feature_match=None, reset=True):
feature_extract=None, feature_match=None,
reset=True, **kwargs):
"""
Custom implementation of the base methods.
Expand Down Expand Up @@ -2810,8 +2815,7 @@ def find(self, needle, haystack):
ox, oy = dx * x, dy * y
ndx, ndy = needle.center_offset.x, needle.center_offset.y

from PIL import ImageDraw
draw = ImageDraw.Draw(canvas)
draw = PIL.ImageDraw.Draw(canvas)
draw.rectangle((ox, oy, ox+dx, oy+dy), outline=(0,0,255))

self.imglog.locations.append((ox, oy))
Expand Down Expand Up @@ -2970,7 +2974,7 @@ class CustomFinder(Finder):

def __init__(self, configure=True, synchronize=True):
"""Build a CV backend using custom matching."""
super(CustomFinder, self).__init__(self, configure=False, synchronize=False)
super(CustomFinder, self).__init__(configure=False, synchronize=False)

# additional preparation (no synchronization available)
if configure:
Expand Down Expand Up @@ -3220,7 +3224,6 @@ def knnMatch(self, desc1, desc2, k=1, desc4kp=1, autostop=0.0):
descriptor = numpy.array(descriptor, dtype=numpy.float32).reshape((1, desc_size))
log.log(9, "%s %s %s", i, descriptor.shape, samples[0].shape)
kmatches = []
ratio = 1.0

for ki in range(k):
_, res, _, dists = knn.find_nearest(descriptor, ki + 1)
Expand Down
27 changes: 27 additions & 0 deletions guibot/inputmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ class AutoPyKey(Key):

def __init__(self):
"""Build an instance containing the key map for the AutoPy backend."""
super().__init__()

import autopy

# commented out keys are not supported by autopy
# TODO: this needs to be patched
self.ENTER = autopy.key.Code.RETURN
Expand Down Expand Up @@ -250,6 +253,8 @@ class XDoToolKey(Key):

def __init__(self):
"""Build an instance containing the key map for the xdotool backend."""
super().__init__()

self.ENTER = 'Return' # also 'enter'
self.TAB = 'Tab'
self.ESC = 'Escape'
Expand Down Expand Up @@ -334,6 +339,8 @@ class VNCDoToolKey(Key):

def __init__(self):
"""Build an instance containing the key map for the VNCDoTool backend."""
super().__init__()

# TODO: it would be preferable to translate directly to RBF like
# 'ENTER = rfb.KEY_Return' but this is internal for the vncdotool
self.ENTER = 'return' # also 'enter'
Expand Down Expand Up @@ -420,6 +427,8 @@ class QemuKey(Key):

def __init__(self):
"""Build an instance containing the key map for the Qemu backend."""
super().__init__()

self.ENTER = 'ret'
self.TAB = 'tab'
self.ESC = 'esc'
Expand Down Expand Up @@ -538,7 +547,10 @@ class AutoPyKeyModifier(KeyModifier):

def __init__(self):
"""Build an instance containing the modifier key map for the AutoPy backend."""
super().__init__()

import autopy

# TODO: 'none' is not available
self.MOD_NONE = None
self.MOD_CTRL = autopy.key.Modifier.CONTROL
Expand All @@ -552,6 +564,8 @@ class XDoToolKeyModifier(KeyModifier):

def __init__(self):
"""Build an instance containing the modifier key map for the xdotool backend."""
super().__init__()

# TODO: 'none' is not available
self.MOD_NONE = None
self.MOD_CTRL = 'ctrl'
Expand All @@ -565,6 +579,8 @@ class VNCDoToolKeyModifier(KeyModifier):

def __init__(self):
"""Build an instance containing the modifier key map for the VNCDoTool backend."""
super().__init__()

# TODO: 'none' is not available
self.MOD_NONE = None
self.MOD_CTRL = 'ctrl'
Expand All @@ -578,6 +594,8 @@ class QemuKeyModifier(KeyModifier):

def __init__(self):
"""Build an instance containing the modifier key map for the Qemu backend."""
super().__init__()

# TODO: 'none' is not available
self.MOD_NONE = None
self.MOD_CTRL = 'ctrl'
Expand Down Expand Up @@ -618,7 +636,10 @@ class AutoPyMouseButton(MouseButton):

def __init__(self):
"""Build an instance containing the mouse button map for the AutoPy backend."""
super().__init__()

import autopy

self.LEFT_BUTTON = autopy.mouse.Button.LEFT
self.RIGHT_BUTTON = autopy.mouse.Button.RIGHT
self.CENTER_BUTTON = autopy.mouse.Button.MIDDLE
Expand All @@ -629,6 +650,8 @@ class XDoToolMouseButton(MouseButton):

def __init__(self):
"""Build an instance containing the mouse button map for the xdotool backend."""
super().__init__()

self.LEFT_BUTTON = 1
self.RIGHT_BUTTON = 3
self.CENTER_BUTTON = 2
Expand All @@ -639,6 +662,8 @@ class VNCDoToolMouseButton(MouseButton):

def __init__(self):
"""Build an instance containing the mouse button map for the VNCDoTool backend."""
super().__init__()

self.LEFT_BUTTON = 1
self.RIGHT_BUTTON = 3
self.CENTER_BUTTON = 2
Expand All @@ -649,6 +674,8 @@ class QemuMouseButton(MouseButton):

def __init__(self):
"""Build an instance containing the mouse button map for the Qemu backend."""
super().__init__()

self.LEFT_BUTTON = 1
self.RIGHT_BUTTON = 4
self.CENTER_BUTTON = 2
14 changes: 8 additions & 6 deletions guibot/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from .errors import *


__all__ = ['Target', 'Image', 'Text', 'Pattern', 'Chain']


class Target(object):
"""
Target used to obtain screen location for clicking, typing,
Expand Down Expand Up @@ -145,7 +148,7 @@ def get_center_offset(self):
return self._center_offset
center_offset = property(fget=get_center_offset)

def load(self, filename):
def load(self, filename, **kwargs):
"""
Load target from a file.
Expand Down Expand Up @@ -302,7 +305,7 @@ def get_pil_image(self):
return self._pil_image
pil_image = property(fget=get_pil_image)

def load(self, filename, use_cache=True):
def load(self, filename, use_cache=True, **kwargs):
"""
Load image from a file.
Expand Down Expand Up @@ -374,7 +377,7 @@ def __str__(self):
"""Provide a part of the text value."""
return self.value[:30]

def load(self, filename):
def load(self, filename, **kwargs):
"""
Load text from a file.
Expand Down Expand Up @@ -447,7 +450,7 @@ def __str__(self):
"""Provide the data filename."""
return os.path.splitext(os.path.basename(self.data_file))[0]

def load(self, filename):
def load(self, filename, **kwargs):
"""
Load pattern from a file.
Expand Down Expand Up @@ -501,7 +504,7 @@ def __iter__(self):
"""Provide an interator over the steps."""
return self._steps.__iter__()

def load(self, steps_filename):
def load(self, steps_filename, **kwargs):
"""
Load steps from a sequence definition file.
Expand Down Expand Up @@ -548,7 +551,6 @@ def save(self, steps_filename):
save_lines = []
for data_and_config in self._steps:
config = data_and_config.match_settings
data = data_and_config.match_settings

step_backend = config.params["find"]["backend"]
if step_backend in ["autopy", "contour", "template", "feature", "tempfeat"]:
Expand Down
5 changes: 5 additions & 0 deletions lgtm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
queries:
# we are doing a lot of name mangling which requires us to disable this
- exclude: py/init-calls-subclass
# the signature containing *args and **kwargs is not correctly recognized by LGTM
- exclude: py/inheritance/incorrect-overridden-signature
3 changes: 2 additions & 1 deletion misc/generate_pytorch_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

def tensors_from_data():
"""
Get a samples and targets tensors from a folder of images and an
Get a samples and targets tensor from a folder of images and an
annotated list with their filenames and regions where the object
is detected.
"""
Expand Down Expand Up @@ -63,6 +63,7 @@ def tensors_from_data():
region_w, region_h = 0, 0
else:
raise ValueError("Corrupted line %i in text file - must be space separated with image path" % i)
print("Extracted region number", region_num, "for line", i)

# image path is relative to the list file
sample = Image.open(os.path.join(os.path.dirname(args.imglist), image_path))
Expand Down

0 comments on commit eaf6ddd

Please sign in to comment.