Spaces:
Running
Running
File size: 2,816 Bytes
1027cfb |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
"""
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
|