87 lines
2.3 KiB
Python
Raw Normal View History

2025-11-04 05:17:27 +01:00
import os
2025-11-04 08:09:12 +01:00
2025-11-04 05:17:27 +01:00
from pr.core.exceptions import ValidationError
def validate_file_path(path: str, must_exist: bool = False) -> str:
if not path:
raise ValidationError("File path cannot be empty")
if must_exist and not os.path.exists(path):
raise ValidationError(f"File does not exist: {path}")
if must_exist and os.path.isdir(path):
raise ValidationError(f"Path is a directory, not a file: {path}")
return os.path.abspath(path)
2025-11-04 08:10:37 +01:00
def validate_directory_path(path: str, must_exist: bool = False, create: bool = False) -> str:
2025-11-04 05:17:27 +01:00
if not path:
raise ValidationError("Directory path cannot be empty")
abs_path = os.path.abspath(path)
if must_exist and not os.path.exists(abs_path):
if create:
os.makedirs(abs_path, exist_ok=True)
else:
raise ValidationError(f"Directory does not exist: {abs_path}")
if os.path.exists(abs_path) and not os.path.isdir(abs_path):
raise ValidationError(f"Path is not a directory: {abs_path}")
return abs_path
def validate_model_name(model: str) -> str:
if not model:
raise ValidationError("Model name cannot be empty")
if len(model) < 2:
raise ValidationError("Model name too short")
return model
def validate_api_url(url: str) -> str:
if not url:
raise ValidationError("API URL cannot be empty")
2025-11-04 08:09:12 +01:00
if not url.startswith(("http://", "https://")):
2025-11-04 05:17:27 +01:00
raise ValidationError("API URL must start with http:// or https://")
return url
def validate_session_name(name: str) -> str:
if not name:
raise ValidationError("Session name cannot be empty")
2025-11-04 08:09:12 +01:00
invalid_chars = ["/", "\\", ":", "*", "?", '"', "<", ">", "|"]
2025-11-04 05:17:27 +01:00
for char in invalid_chars:
if char in name:
raise ValidationError(f"Session name contains invalid character: {char}")
if len(name) > 255:
raise ValidationError("Session name too long (max 255 characters)")
return name
def validate_temperature(temp: float) -> float:
if not 0.0 <= temp <= 2.0:
raise ValidationError("Temperature must be between 0.0 and 2.0")
return temp
def validate_max_tokens(tokens: int) -> int:
if tokens < 1:
raise ValidationError("Max tokens must be at least 1")
if tokens > 100000:
raise ValidationError("Max tokens too high (max 100000)")
return tokens