import csv
import re
import typing
def parse_csv_attributes(csv_attributes: typing.Optional[str] = "") -> typing.Dict[str, str]:
"""
Parse the raw_attributes variable, which uses the CSV format:
key=value,another=something_else
Returns:
dict: Parsed key-value pairs as a dictionary.
"""
attributes = {}
if not csv_attributes:
return attributes
try:
reader = csv.reader([csv_attributes])
for row in reader:
for pair in row:
match = re.match(r"^\s*([^=]+?)\s*=\s*(.+?)\s*$", pair)
if match:
key, value = match.groups()
attributes[key.strip()] = value.strip()
else:
raise ValueError(f"Invalid attribute format: {pair}")
except Exception as e:
raise ValueError(f"Failed to parse csv_attributes='{csv_attributes}': {e}") from e
return attributes