Coverage for mindsdb / utilities / utils.py: 0%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1import csv 

2import re 

3import typing 

4 

5 

6def parse_csv_attributes(csv_attributes: typing.Optional[str] = "") -> typing.Dict[str, str]: 

7 """ 

8 Parse the raw_attributes variable, which uses the CSV format: 

9 key=value,another=something_else 

10 

11 Returns: 

12 dict: Parsed key-value pairs as a dictionary. 

13 """ 

14 attributes = {} 

15 

16 if not csv_attributes: 

17 return attributes # Return empty dictionary if the variable is not set 

18 

19 try: 

20 # Use CSV reader to handle parsing the input 

21 reader = csv.reader([csv_attributes]) 

22 for row in reader: 

23 for pair in row: 

24 # Match key=value pattern 

25 match = re.match(r"^\s*([^=]+?)\s*=\s*(.+?)\s*$", pair) 

26 if match: 

27 key, value = match.groups() 

28 attributes[key.strip()] = value.strip() 

29 else: 

30 raise ValueError(f"Invalid attribute format: {pair}") 

31 except Exception as e: 

32 raise ValueError(f"Failed to parse csv_attributes='{csv_attributes}': {e}") from e 

33 

34 return attributes