Add in error functionality to the wrapper

This commit is contained in:
Bradlee Speice 2012-08-17 17:18:51 -04:00
parent 02ae3354d0
commit 147e770cd2

View File

@ -131,6 +131,21 @@ _mouse_button_default=1 #Left click
_libcvautomation_error_location = libcvautomation.cvaPoint
_libcvautomation_error_location.x = _libcvautomation_error_location.y = -1
#-------------------------------------------------------------------------------
# Set up the exceptions we will be using later
#-------------------------------------------------------------------------------
class LibcvImageNotFound(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr("Image not found: " + self.value)
class LibcvDisplayNotOpen(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr("Tried to use function " + self.value + " without opening a display.")
#-------------------------------------------------------------------------------
# Begin the actual wrapper functions
#-------------------------------------------------------------------------------
@ -141,11 +156,10 @@ _libcvautomation_error_location.x = _libcvautomation_error_location.y = -1
def mouse_down( mouse_button = _mouse_button_default ):
if _check_display():
_log_output( 'Mouse button down: ' + mouse_button )
libcvautomation.xte_mouseDown(_get_display(), mouse_button )
libcvautomation.xte_mouseDown( _get_display(), mouse_button )
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Release a mouse button
# \param mouse_button The number of the mouse button to release
@ -158,7 +172,7 @@ def mouse_up( mouse_button = _mouse_button_default ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Press and release a mouse button
# \param mouse_button The number of the mouse button to click
@ -170,7 +184,7 @@ def mouse_click( mouse_button = _mouse_button_default ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Click a mouse button at an absolute location
# \details Move the mouse to a location given by an x and y coordinate before clicking
@ -191,7 +205,7 @@ def mouse_click_xy( x_coord, y_coord, mouse_button = _mouse_button_default ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Click a mouse button at a relative location
# \details Move the mouse horizontally and vertically by an increment, and then click the mouse
@ -210,7 +224,7 @@ def mouse_click_rxy( x_inc, y_inc, mouse_button = _mouse_button_default ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Click a mouse button on an image inside the root X11 window
# \details For each image in \c image_names, search for it inside the root X11 window. Return once a match has been found, or the timeout value has been exceeded
@ -262,9 +276,12 @@ def mouse_click_image( image_names, search_method = _search_method_default,
if image_location != _libcvautomation_error_location:
#We've found our image, break out of the for loop
return True
#No image was found
raise LibcvImageNotFound( image_names )
else:
#Display not open or no image found
return False
#Display not open
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Press and release a mouse button twice
@ -278,7 +295,7 @@ def mouse_doubleclick( mouse_button = _mouse_button_default ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Click a mouse button twice at an absolute location
@ -301,7 +318,7 @@ def mouse_doubleclick_xy( x_coord, y_coord, mouse_button = _mouse_button_default
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Click a mouse button twice at a relative location
# \details Move the mouse horizontally and vertically by an increment, and then click the mouse twice
@ -321,7 +338,7 @@ def mouse_doubleclick_rxy( x_inc, y_inc, mouse_button = _mouse_button_default ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Click a mouse button twice on an image inside the root X11 window
# \details For each image in \c image_names, search for it inside the root X11 window. Return once a match has been found, or the timeout value has been exceeded
@ -379,10 +396,11 @@ def mouse_doubleclick_image( image_names, search_method = _search_method_default
if image_location != _libcvautomation_error_location:
#We've found our image, break out of the for loop
return True
#No image found
raise LibcvImageNotFound( image_names )
else:
#Display not open, or no image found
return False
#Display not open
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Move the mouse to a given coordinate, and leave it there
# \param x_coord The X-coordinate to move the mouse to
@ -400,7 +418,7 @@ def mouse_hover_xy( x_coord, y_coord ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Move the mouse by a given increment, and leave it there
# \param x_inc The increment to move the mouse horizontally
@ -416,7 +434,7 @@ def mouse_hover_rxy( x_inc, y_inc ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Move the mouse to an image inside the root X11 window
# \details For each image in \c image_names, search for it inside the root X11 window. Return once a match has been found, or the timeout value has been exceeded
@ -468,9 +486,12 @@ def mouse_hover_image( image_names, search_method = _search_method_default,
if image_location != _libcvautomation_error_location:
#We've found our image, break out of the for loop
return True
#No images were found
raise LibcvImageNotFound( image_names )
else:
#Display not open or no image found
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Jiggle the mouse in place
# \details Move the mouse right and down 1 pixel, and back. Useful for activating menu entries, etc.
@ -483,7 +504,7 @@ def mouse_jiggle():
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Scroll the mouse wheel up
# \details This is the same as pressing mouse button 4
@ -495,7 +516,7 @@ def mouse_scroll_up():
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Scroll the mouse wheel down
# \details This is the same as pressing mouse button 5
@ -507,7 +528,7 @@ def mouse_scroll_down():
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Drag and drop one image to another
# \param drag_image The image to drag from
@ -523,12 +544,17 @@ def mouse_drag_n_drop( drag_image, drag_to, use_center = True ):
mouse_down( mouse_button=1 )
successful_hover = mouse_hover_image( drag_to )
mouse_up( mouse_button=1 )
else:
raise LibcvImageNotFound( drag_image )
#Return True if both hovers are successful, False otherwise
return successful_hover
#Return True if both hovers are successful, raise an exception otherwise
if successful_hover:
return True
else:
raise LibcvImageNotFound( drag_to )
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Enter a string of text on the keyboard
# \details This function will simulate pressing the keys exactly as they are entered - unlike libcvautomation_funcs::key_down, libcvautomation_funcs::key_up, and libcvautomation_funcs::key_click, this function will display exactly what you entered: A string of \c '!' will produce a \c ! as a keypress.
@ -541,7 +567,7 @@ def key_string( string ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Press a key down on the keyboard, and leave it down
# \param key_name The name of the key to press down. Unlike libcvautomation_funcs::key_string, this command will press a single key corresponding to the string you give it - for example, \c 'a', \c 'b', or something fancy like \c 'space'.
@ -554,7 +580,7 @@ def key_down( key_name ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Release a key on the keyboard
# \param key_name The name of the key to release. Unlike libcvautomation_funcs::key_string, this command will press a single key corresponding to the string you give it - for example, \c 'a', \c 'b', or something fancy like \c 'space'.
@ -567,7 +593,7 @@ def key_up( key_name ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Press and release a key on the keyboard
# \param key_name The name of the key to click. Unlike libcvautomation_funcs::key_string, this command will press a single key corresponding to the string you give it - for example, \c 'a', \c 'b', or something fancy like \c 'space'.
@ -580,7 +606,7 @@ def key_click( key_name ):
return True
else:
#Display not open
return False
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Get the location of an image on the screen
# \param image_names List of image names to search for in the root X11 window
@ -607,8 +633,8 @@ def image_location( image_names, search_method = _search_method_default,
return location_array
else:
#Display not open, but return the same type
return {}
#Display not open
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Get the location of an image on the screen, waiting for it to show up
# \param image_names List of image names to search for in the root X11 window
@ -636,8 +662,8 @@ def wait_for( image_names, search_method = _search_method_default,
return location_array
else:
#Display not open, but return the same type
return {}
#Display not open
raise LibcvDisplayNotOpen( _get_caller() )
## \brief Execute a libcvautomation command based on a string
# \details This is a handler for the xte_commandString() function.
@ -664,7 +690,12 @@ def command_string( string, mouse_button = _mouse_button_default, search_method
result_point = libcvautomation.xte_commandString(_get_display(), string,
mouse_button, search_method,
tolerance, timeout )
return result_point
if result_point == _libcvautomation_error_location:
raise LibcvImageNotFound( string )
else:
return result_point
else:
#Display not open, return an error point
return _libcvautomation_error_location
#Display not open
raise LibcvDisplayNotOpen( _get_caller() )