Fix up the Python frame tracing, and make it more portable

This commit is contained in:
Bradlee Speice 2012-08-23 15:43:57 -04:00
parent fc1b650298
commit 322663c32c

View File

@ -36,7 +36,7 @@ import libcvautomation
_use_frame_trace=None _use_frame_trace=None
try: try:
from sys import _current_frames #For getting the function that called us import inspect #For getting the function that called us
_use_frame_trace = True _use_frame_trace = True
except: except:
_use_frame_trace = False _use_frame_trace = False
@ -45,15 +45,27 @@ import time
def _get_caller(): def _get_caller():
if _use_frame_trace: if _use_frame_trace:
current_frame = sys._current_frames().values()[0] #Current frame caller_name = inspect.stack()[2][3] #This requires a bit of explanation -
caller_frame = current_frame.f_back.f_back #Go back two frames to make sure # The way inspect.stack works, we can actually use 'absolute'
#we don't get the name of the function that just called us. # positioning in the stack. The [2] indicates the frame
# two levels above us, not specifically the second frame.
caller_name = caller_frame.f_globals['__name__'] #And get the name # The [3] is the name of said function.
return caller_name return caller_name
else: else:
return '[frame trace disabled]' return '[frame trace disabled]'
def _get_name():
if _use_frame_trace:
caller_name = inspect.stack()[1][3] #This requires a bit of explanation -
# The way inspect.stack works, we can actually use 'absolute'
# positioning in the stack. The [1] indicates the frame
# immediately above us, not specifically the first frame.
# The [3] is the name of said function.
return caller_name
else:
return '[frame trace disabled]'
outfile = '/dev/null' outfile = '/dev/null'
def _log_output( message ): def _log_output( message ):
global outfile #Make sure we use the global outfile, global outfile #Make sure we use the global outfile,
@ -159,7 +171,7 @@ def mouse_down( mouse_button = _mouse_button_default ):
libcvautomation.xte_mouseDown( _get_display(), mouse_button ) libcvautomation.xte_mouseDown( _get_display(), mouse_button )
return True return True
else: else:
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Release a mouse button ## \brief Release a mouse button
# \param mouse_button The number of the mouse button to release # \param mouse_button The number of the mouse button to release
@ -172,7 +184,7 @@ def mouse_up( mouse_button = _mouse_button_default ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Press and release a mouse button ## \brief Press and release a mouse button
# \param mouse_button The number of the mouse button to click # \param mouse_button The number of the mouse button to click
@ -184,7 +196,7 @@ def mouse_click( mouse_button = _mouse_button_default ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Click a mouse button at an absolute location ## \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 # \details Move the mouse to a location given by an x and y coordinate before clicking
@ -205,7 +217,7 @@ def mouse_click_xy( x_coord, y_coord, mouse_button = _mouse_button_default ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Click a mouse button at a relative location ## \brief Click a mouse button at a relative location
# \details Move the mouse horizontally and vertically by an increment, and then click the mouse # \details Move the mouse horizontally and vertically by an increment, and then click the mouse
@ -224,7 +236,7 @@ def mouse_click_rxy( x_inc, y_inc, mouse_button = _mouse_button_default ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Click a mouse button on an image inside the root X11 window ## \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 # \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
@ -281,7 +293,7 @@ def mouse_click_image( image_names, search_method = _search_method_default,
raise LibcvImageNotFound( image_names ) raise LibcvImageNotFound( image_names )
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Press and release a mouse button twice ## \brief Press and release a mouse button twice
@ -295,7 +307,7 @@ def mouse_doubleclick( mouse_button = _mouse_button_default ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Click a mouse button twice at an absolute location ## \brief Click a mouse button twice at an absolute location
@ -318,7 +330,7 @@ def mouse_doubleclick_xy( x_coord, y_coord, mouse_button = _mouse_button_default
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Click a mouse button twice at a relative location ## \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 # \details Move the mouse horizontally and vertically by an increment, and then click the mouse twice
@ -338,7 +350,7 @@ def mouse_doubleclick_rxy( x_inc, y_inc, mouse_button = _mouse_button_default ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Click a mouse button twice on an image inside the root X11 window ## \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 # \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
@ -400,7 +412,7 @@ def mouse_doubleclick_image( image_names, search_method = _search_method_default
raise LibcvImageNotFound( image_names ) raise LibcvImageNotFound( image_names )
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Move the mouse to a given coordinate, and leave it there ## \brief Move the mouse to a given coordinate, and leave it there
# \param x_coord The X-coordinate to move the mouse to # \param x_coord The X-coordinate to move the mouse to
@ -418,7 +430,7 @@ def mouse_hover_xy( x_coord, y_coord ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Move the mouse by a given increment, and leave it there ## \brief Move the mouse by a given increment, and leave it there
# \param x_inc The increment to move the mouse horizontally # \param x_inc The increment to move the mouse horizontally
@ -434,7 +446,7 @@ def mouse_hover_rxy( x_inc, y_inc ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Move the mouse to an image inside the root X11 window ## \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 # \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
@ -491,7 +503,7 @@ def mouse_hover_image( image_names, search_method = _search_method_default,
raise LibcvImageNotFound( image_names ) raise LibcvImageNotFound( image_names )
else: else:
#Display not open or no image found #Display not open or no image found
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Jiggle the mouse in place ## \brief Jiggle the mouse in place
# \details Move the mouse right and down 1 pixel, and back. Useful for activating menu entries, etc. # \details Move the mouse right and down 1 pixel, and back. Useful for activating menu entries, etc.
@ -504,7 +516,7 @@ def mouse_jiggle():
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Scroll the mouse wheel up ## \brief Scroll the mouse wheel up
# \details This is the same as pressing mouse button 4 # \details This is the same as pressing mouse button 4
@ -516,7 +528,7 @@ def mouse_scroll_up():
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Scroll the mouse wheel down ## \brief Scroll the mouse wheel down
# \details This is the same as pressing mouse button 5 # \details This is the same as pressing mouse button 5
@ -528,7 +540,7 @@ def mouse_scroll_down():
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Drag and drop one image to another ## \brief Drag and drop one image to another
# \param drag_image The image to drag from # \param drag_image The image to drag from
@ -554,7 +566,7 @@ def mouse_drag_n_drop( drag_image, drag_to, use_center = True ):
raise LibcvImageNotFound( drag_to ) raise LibcvImageNotFound( drag_to )
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Get the current location of the mouse ## \brief Get the current location of the mouse
# \details This function will return the current location of the mouse as two variables - i.e. `x, y = mouse_location()`. This is a Python-specific function. # \details This function will return the current location of the mouse as two variables - i.e. `x, y = mouse_location()`. This is a Python-specific function.
@ -566,7 +578,7 @@ def mouse_location():
return current_location.x, current_location.y return current_location.x, current_location.y
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Enter a string of text on the keyboard ## \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. # \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.
@ -579,7 +591,7 @@ def key_string( string ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Press a key down on the keyboard, and leave it down ## \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'. # \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'.
@ -592,7 +604,7 @@ def key_down( key_name ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Release a key on the keyboard ## \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'. # \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'.
@ -605,7 +617,7 @@ def key_up( key_name ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Press and release a key on the keyboard ## \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'. # \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'.
@ -618,7 +630,7 @@ def key_click( key_name ):
return True return True
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Get the location of an image on the screen ## \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 # \param image_names List of image names to search for in the root X11 window
@ -646,7 +658,7 @@ def image_location( image_names, search_method = _search_method_default,
return location_array return location_array
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Get the location of an image on the screen, waiting for it to show up ## \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 # \param image_names List of image names to search for in the root X11 window
@ -675,7 +687,7 @@ def wait_for( image_names, search_method = _search_method_default,
return location_array return location_array
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )
## \brief Execute a libcvautomation command based on a string ## \brief Execute a libcvautomation command based on a string
# \details This is a handler for the xte_commandString() function. # \details This is a handler for the xte_commandString() function.
@ -710,4 +722,4 @@ def command_string( string, mouse_button = _mouse_button_default, search_method
else: else:
#Display not open #Display not open
raise LibcvDisplayNotOpen( _get_caller() ) raise LibcvDisplayNotOpen( _get_name() )