Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _getCommaToken(commandStr):
"""Gets the comma token at the start of commandStr.
Given ',' returns ','
Given ' ,', returns ' ,'
Raises an exception if a comma isn't found.
"""
pattern = re.compile(r"^((\s*),)")
mo = pattern.search(commandStr)
if mo is None:
raise PyAutoGUIException("Invalid command at index 0: a comma was expected")
return mo.group(1)
def _getParensCommandStrToken(commandStr):
"""Gets the command string token at the start of commandStr. It will also
be enclosed with parentheses.
Given "(ccc)world" returns "(ccc)"
Given " (ccc)world" returns " (ccc)"
Given "(ccf10(r))world" returns "(ccf10(r))"
Raises an exception if it can't tokenize a quoted string.
"""
# Check to make sure at least one open parenthesis exists:
pattern = re.compile(r"^\s*\(")
mo = pattern.search(commandStr)
if mo is None:
raise PyAutoGUIException("Invalid command at index 0: No open parenthesis found.")
# Check to make sure the parentheses are balanced:
i = 0
openParensCount = 0
while i < len(commandStr):
if commandStr[i] == "(":
openParensCount += 1
elif commandStr[i] == ")":
openParensCount -= 1
if openParensCount == 0:
i += 1 # Remember to increment i past the ) before breaking.
break
elif openParensCount == -1:
raise PyAutoGUIException("Invalid command at index 0: No open parenthesis for this close parenthesis.")
i += 1
if openParensCount > 0:
def linear(n):
"""
Returns ``n``, where ``n`` is the float argument between ``0.0`` and ``1.0``. This function is for the default
linear tween for mouse moving functions.
This function was copied from PyTweening module, so that it can be called even if PyTweening is not installed.
"""
# We use this function instead of pytweening.linear for the default tween function just in case pytweening couldn't be imported.
if not 0.0 <= n <= 1.0:
raise PyAutoGUIException("Argument must be between 0.0 and 1.0.")
return n
def useImageNotFoundException(value=None):
"""
When called with no arguments, PyAutoGUI will raise ImageNotFoundException when the PyScreeze locate*() functions
can't find the image it was told to locate. The default behavior is to return None. Call this function with no
arguments (or with True as the argument) to have exceptions raised, which is a better practice.
You can also disable raising exceptions by passing False for the argument.
"""
if value is None:
value = True
# TODO - this will cause a NameError if PyScreeze couldn't be imported:
try:
pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = value
except NameError:
raise PyAutoGUIException("useImageNotFoundException() ws called but pyscreeze isn't installed.")
newIndexNum = int(indexNum) + i
raise PyAutoGUIException("Invalid command at index %s:%s" % (newIndexNum, message))
# Get rid of any whitespace at the front and the quotes:
commandList.append(quotedString[1:-1].lstrip())
# Handle the arguments of the (f)or loop command:
elif individualCommand == "f":
try:
numberOfLoops = _getNumberToken(commandStr[i:])
i += len(numberOfLoops) # Increment past the number of loops.
subCommandStr = _getParensCommandStrToken(commandStr[i:])
i += len(subCommandStr) # Increment past the sub-command string.
except PyAutoGUIException as excObj:
# Exception message starts with something like "Invalid command at index 0:"
# Change the index number and reraise it.
indexPart, colon, message = str(excObj).partition(":")
indexNum = indexPart[len("Invalid command at index ") :]
newIndexNum = int(indexNum) + i
raise PyAutoGUIException("Invalid command at index %s:%s" % (newIndexNum, message))
# Get rid of any whitespace at the front:
commandList.append(numberOfLoops.lstrip())
# Get rid of any whitespace at the front and the quotes:
subCommandStr = subCommandStr.lstrip()[1:-1]
# Recursively call this function and append the list it returns:
commandList.append(_tokenizeCommandStr(subCommandStr))
def _couldNotImportPyScreeze():
"""
This function raises ``PyAutoGUIException``. It's used for the PyScreeze function names if the PyScreeze module
failed to be imported.
"""
raise PyAutoGUIException(
"PyAutoGUI was unable to import pyscreeze. (This is likely because you're running a version of Python that Pillow (which pyscreeze depends on) doesn't support currently.) Please install this module to enable the function you tried to call."
)
"""
# TODO - The swap detection hasn't been done yet. For Windows, see https://stackoverflow.com/questions/45627956/check-if-mouse-buttons-are-swapped-or-not-in-c
# TODO - We should check the OS settings to see if it's a left-hand setup, where button 1 would be "right".
# Check that `button` has a valid value:
button = button.lower()
if platform.system() == "Linux":
# Check for valid button arg on Linux:
if button not in (LEFT, MIDDLE, RIGHT, PRIMARY, SECONDARY, 1, 2, 3, 4, 5, 6, 7):
raise PyAutoGUIException(
"button argument must be one of ('left', 'middle', 'right', 'primary', 'secondary', 1, 2, 3, 4, 5, 6, 7)"
)
else:
# Check for valid button arg on Windows and macOS:
if button not in (LEFT, MIDDLE, RIGHT, PRIMARY, SECONDARY, 1, 2, 3):
raise PyAutoGUIException(
"button argument must be one of ('left', 'middle', 'right', 'primary', 'secondary', 1, 2, 3)"
)
# TODO - Check if the primary/secondary mouse buttons have been swapped:
if button in (PRIMARY, SECONDARY):
swapped = False # TODO - Add the operating system-specific code to detect mouse swap later.
if swapped:
if button == PRIMARY:
return RIGHT
elif button == SECONDARY:
return LEFT
else:
if button == PRIMARY:
return LEFT
elif button == SECONDARY:
return RIGHT
def _getParensCommandStrToken(commandStr):
"""Gets the command string token at the start of commandStr. It will also
be enclosed with parentheses.
Given "(ccc)world" returns "(ccc)"
Given " (ccc)world" returns " (ccc)"
Given "(ccf10(r))world" returns "(ccf10(r))"
Raises an exception if it can't tokenize a quoted string.
"""
# Check to make sure at least one open parenthesis exists:
pattern = re.compile(r"^\s*\(")
mo = pattern.search(commandStr)
if mo is None:
raise PyAutoGUIException("Invalid command at index 0: No open parenthesis found.")
# Check to make sure the parentheses are balanced:
i = 0
openParensCount = 0
while i < len(commandStr):
if commandStr[i] == "(":
openParensCount += 1
elif commandStr[i] == ")":
openParensCount -= 1
if openParensCount == 0:
i += 1 # Remember to increment i past the ) before breaking.
break
elif openParensCount == -1:
raise PyAutoGUIException("Invalid command at index 0: No open parenthesis for this close parenthesis.")
i += 1
if openParensCount > 0:
def _getNumberToken(commandStr):
"""Gets the number token at the start of commandStr.
Given '5hello' returns '5'
Given ' 5hello' returns ' 5'
Given '-42hello' returns '-42'
Given '+42hello' returns '+42'
Given '3.14hello' returns '3.14'
Raises an exception if it can't tokenize a number.
"""
pattern = re.compile(r"^(\s*(\+|\-)?\d+(\.\d+)?)")
mo = pattern.search(commandStr)
if mo is None:
raise PyAutoGUIException("Invalid command at index 0: a number was expected")
return mo.group(1)