fix: enhance handling of '<' characters in pyautogui commands

- Refactor _fix_pyautogui_less_than_bug to improve handling of press('<') and typewrite calls.
- Introduce Unicode escape decoding for typewrite content to ensure proper '<' character processing.
- Maintain existing logic while enhancing functionality for better compatibility.
This commit is contained in:
yuanmengqi
2025-07-26 07:59:37 +00:00
parent 123f51ea4a
commit d49ca9cc2d

View File

@@ -38,47 +38,34 @@ def _fix_pyautogui_less_than_bug(command: str) -> str:
Returns:
str: The fixed command with '<' characters handled properly
"""
# Handle typewrite with '<' characters
def replace_typewrite_less_than(match):
content = match.group(1)
# Split the content by '<' and rebuild with hotkey calls
parts = content.split('<')
if len(parts) == 1:
# No '<' found, return original
return match.group(0)
# Rebuild the command
result_parts = []
for i, part in enumerate(parts):
if i == 0:
# First part, just add typewrite if not empty
if part:
result_parts.append(f"pyautogui.typewrite({repr(part)})")
else:
# Add hotkey for '<' and then typewrite for the rest if not empty
result_parts.append('pyautogui.hotkey("shift", ",")')
if part:
result_parts.append(f"pyautogui.typewrite({repr(part)})")
return '; '.join(result_parts)
# Pattern to match press('<') or press('\u003c') calls
press_pattern = r'pyautogui\.press\(["\'](?:<|\\u003c)["\']\)'
# Handle press('<') calls
def replace_press_less_than(match):
return 'pyautogui.hotkey("shift", ",")'
# Pattern to match typewrite calls with quoted strings
typewrite_pattern = r'pyautogui\.typewrite\((["\'])(.*?)\1\)'
# Pattern to match press('<') calls
press_pattern = r'pyautogui\.press\(["\']<["\']\)'
# First handle press('<') calls
command = re.sub(press_pattern, replace_press_less_than, command)
# Pattern to match typewrite calls with quoted strings
typewrite_pattern = r'pyautogui\.typewrite\((["\'])(.*?)\1\)'
# Then handle typewrite calls
def process_typewrite_match(match):
quote_char = match.group(1)
content = match.group(2)
# Preprocess: Try to decode Unicode escapes like \u003c to actual '<'
# This handles cases where '<' is represented as escaped Unicode
try:
# Attempt to decode unicode escapes
decoded_content = content.encode('utf-8').decode('unicode_escape')
content = decoded_content
except UnicodeDecodeError:
# If decoding fails, proceed with original content to avoid breaking existing logic
pass # English comment: Graceful degradation - fall back to original content if decoding fails
# Check if content contains '<'
if '<' not in content:
return match.group(0)