-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
54 lines (43 loc) · 1.92 KB
/
Copy pathvalidators.py
File metadata and controls
54 lines (43 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys
from typing import Tuple, Union, Optional
class ValidationError(ValueError):
"""Exception raised for validation errors in the autoclicker configuration."""
pass
def validate_interval(interval: Union[int, float]) -> float:
"""Validates the click interval in seconds."""
try:
val = float(interval)
except (TypeError, ValueError):
raise ValidationError(f"Interval must be a number, got {interval}")
if val < 0.001:
raise ValidationError("Interval must be at least 0.001 seconds.")
return val
def validate_button(button: str) -> str:
"""Validates that the button is a recognized mouse button."""
allowed = {"left", "right", "middle"}
clean_button = str(button).strip().lower()
if clean_button not in allowed:
raise ValidationError(f"Button must be one of {allowed}, got '{button}'")
return clean_button
def validate_clicks(clicks: int) -> int:
"""Validates the number of clicks, where 0 represents infinite clicks."""
try:
val = int(clicks)
except (TypeError, ValueError):
raise ValidationError(f"Clicks count must be an integer, got {clicks}")
if val < 0:
raise ValidationError("Clicks count must be 0 (for infinite) or greater.")
return val
def validate_coordinates(coords: Optional[Tuple[int, int]]) -> Optional[Tuple[int, int]]:
"""Validates screen coordinates if they are provided."""
if coords is None:
return None
if not isinstance(coords, (tuple, list)) or len(coords) != 2:
raise ValidationError("Coordinates must be a tuple or list of (x, y) or None.")
try:
x, y = int(coords[0]), int(coords[1])
except (TypeError, ValueError):
raise ValidationError(f"Coordinates must contain valid integers, got {coords}")
if x < 0 or y < 0:
raise ValidationError(f"Coordinates cannot be negative, got ({x}, {y})")
return (x, y)