Spaces:
Running
Running
| """ | |
| Utilities for submission handling, replacing agent-eval dependencies. | |
| """ | |
| import re | |
| from pathlib import Path | |
| from typing import Optional | |
| # Constants | |
| SUBMISSION_METADATA_FILENAME = "metadata.json" | |
| # Simple SubmissionMetadata class | |
| class SubmissionMetadata: | |
| """Simple metadata for submissions.""" | |
| def __init__(self, **kwargs): | |
| self.agent_name = kwargs.get("agent_name", "") | |
| self.llm_base = kwargs.get("llm_base", "") | |
| self.openness = kwargs.get("openness", "") | |
| self.tool_usage = kwargs.get("tool_usage", "") | |
| self.submitter_name = kwargs.get("submitter_name", "") | |
| self.submitter_email = kwargs.get("submitter_email", "") | |
| def to_dict(self): | |
| return { | |
| "agent_name": self.agent_name, | |
| "llm_base": self.llm_base, | |
| "openness": self.openness, | |
| "tool_usage": self.tool_usage, | |
| "submitter_name": self.submitter_name, | |
| "submitter_email": self.submitter_email, | |
| } | |
| # Path validation functions | |
| def _validate_path_component(component: str, allow_underscores: bool = True) -> None: | |
| """ | |
| Validate a single path component. | |
| Args: | |
| component: The path component to validate | |
| allow_underscores: Whether to allow underscores in the component | |
| Raises: | |
| ValueError: If the component is invalid | |
| """ | |
| if not component: | |
| raise ValueError("Path component cannot be empty") | |
| if component in (".", ".."): | |
| raise ValueError(f"Path component cannot be '{component}'") | |
| # Check for invalid characters | |
| pattern = r'^[a-zA-Z0-9_\-\.]+$' if allow_underscores else r'^[a-zA-Z0-9\-\.]+$' | |
| if not re.match(pattern, component): | |
| raise ValueError( | |
| f"Path component '{component}' contains invalid characters. " | |
| f"Only alphanumeric, hyphens, dots{', and underscores' if allow_underscores else ''} are allowed." | |
| ) | |
| def sanitize_path_component(component: str, replacement: str = "_") -> str: | |
| """ | |
| Sanitize a path component by replacing invalid characters. | |
| Args: | |
| component: The path component to sanitize | |
| replacement: The character to use for replacing invalid characters | |
| Returns: | |
| Sanitized path component | |
| """ | |
| if not component: | |
| return "unnamed" | |
| # Replace any non-alphanumeric, non-hyphen, non-dot, non-underscore with replacement | |
| sanitized = re.sub(r'[^a-zA-Z0-9_\-\.]', replacement, component) | |
| # Remove leading/trailing dots or hyphens | |
| sanitized = sanitized.strip('.-') | |
| # Collapse multiple replacements into one | |
| sanitized = re.sub(f'{re.escape(replacement)}+', replacement, sanitized) | |
| if not sanitized: | |
| return "unnamed" | |
| return sanitized | |