- Introduction of dynamic Processors - Introduction of caching system - Introduction of a better template manager - Adaptation of ModelVariables to support dynamic Processors / Retrievers / Specialists - Start adaptation of chat client
112 lines
3.8 KiB
Python
112 lines
3.8 KiB
Python
from typing import List, Union
|
|
import re
|
|
|
|
|
|
class StringListConverter:
|
|
"""Utility class for converting between comma-separated strings and lists"""
|
|
|
|
@staticmethod
|
|
def string_to_list(input_string: Union[str, None], allow_empty: bool = True) -> List[str]:
|
|
"""
|
|
Convert a comma-separated string to a list of strings.
|
|
|
|
Args:
|
|
input_string: Comma-separated string to convert
|
|
allow_empty: If True, returns empty list for None/empty input
|
|
If False, raises ValueError for None/empty input
|
|
|
|
Returns:
|
|
List of stripped strings
|
|
|
|
Raises:
|
|
ValueError: If input is None/empty and allow_empty is False
|
|
"""
|
|
if not input_string:
|
|
if allow_empty:
|
|
return []
|
|
raise ValueError("Input string cannot be None or empty")
|
|
|
|
return [item.strip() for item in input_string.split(',') if item.strip()]
|
|
|
|
@staticmethod
|
|
def list_to_string(input_list: Union[List[str], None], allow_empty: bool = True) -> str:
|
|
"""
|
|
Convert a list of strings to a comma-separated string.
|
|
|
|
Args:
|
|
input_list: List of strings to convert
|
|
allow_empty: If True, returns empty string for None/empty input
|
|
If False, raises ValueError for None/empty input
|
|
|
|
Returns:
|
|
Comma-separated string
|
|
|
|
Raises:
|
|
ValueError: If input is None/empty and allow_empty is False
|
|
"""
|
|
if not input_list:
|
|
if allow_empty:
|
|
return ''
|
|
raise ValueError("Input list cannot be None or empty")
|
|
|
|
return ', '.join(str(item).strip() for item in input_list)
|
|
|
|
@staticmethod
|
|
def validate_format(input_string: str,
|
|
allowed_chars: str = r'a-zA-Z0-9_\-',
|
|
min_length: int = 1,
|
|
max_length: int = 50) -> bool:
|
|
"""
|
|
Validate the format of items in a comma-separated string.
|
|
|
|
Args:
|
|
input_string: String to validate
|
|
allowed_chars: String of allowed characters (for regex pattern)
|
|
min_length: Minimum length for each item
|
|
max_length: Maximum length for each item
|
|
|
|
Returns:
|
|
bool: True if format is valid, False otherwise
|
|
"""
|
|
if not input_string:
|
|
return False
|
|
|
|
# Create regex pattern for individual items
|
|
pattern = f'^[{allowed_chars}]{{{min_length},{max_length}}}$'
|
|
|
|
try:
|
|
# Convert to list and check each item
|
|
items = StringListConverter.string_to_list(input_string)
|
|
return all(bool(re.match(pattern, item)) for item in items)
|
|
except Exception:
|
|
return False
|
|
|
|
@staticmethod
|
|
def validate_and_convert(input_string: str,
|
|
allowed_chars: str = r'a-zA-Z0-9_\-',
|
|
min_length: int = 1,
|
|
max_length: int = 50) -> List[str]:
|
|
"""
|
|
Validate and convert a comma-separated string to a list.
|
|
|
|
Args:
|
|
input_string: String to validate and convert
|
|
allowed_chars: String of allowed characters (for regex pattern)
|
|
min_length: Minimum length for each item
|
|
max_length: Maximum length for each item
|
|
|
|
Returns:
|
|
List of validated and converted strings
|
|
|
|
Raises:
|
|
ValueError: If input string format is invalid
|
|
"""
|
|
if not StringListConverter.validate_format(
|
|
input_string, allowed_chars, min_length, max_length
|
|
):
|
|
raise ValueError(
|
|
f"Invalid format. Items must be {min_length}-{max_length} characters "
|
|
f"long and contain only these characters: {allowed_chars}"
|
|
)
|
|
|
|
return StringListConverter.string_to_list(input_string) |