|
| 1 | +import unicodedata |
| 2 | +from typing import List, Tuple |
| 3 | + |
| 4 | + |
| 5 | +class AiTaskIdentifierHelper: |
| 6 | + """Helper class for validating and generating AI task identifiers.""" |
| 7 | + |
| 8 | + @staticmethod |
| 9 | + def validate_identifier(identifier: str) -> Tuple[bool, List[str]]: |
| 10 | + """ |
| 11 | + Validates an AI task identifier. |
| 12 | +
|
| 13 | + Args: |
| 14 | + identifier: The identifier to validate. |
| 15 | +
|
| 16 | + Returns: |
| 17 | + A tuple of (is_valid, errors). If valid, errors is an empty list. |
| 18 | + """ |
| 19 | + errors: List[str] = [] |
| 20 | + |
| 21 | + if not identifier or identifier.isspace(): |
| 22 | + errors.append("Identifier cannot be empty or contain only whitespace;") |
| 23 | + return False, errors |
| 24 | + |
| 25 | + # Check that the string is already normalized (contains only a-z, 0-9 and hyphens) |
| 26 | + normalized = unicodedata.normalize("NFD", identifier) |
| 27 | + if identifier != normalized: |
| 28 | + errors.append("Identifier contains diacritical marks or non-ASCII characters;") |
| 29 | + |
| 30 | + # Check that there are no uppercase letters |
| 31 | + if any(c.isupper() for c in identifier): |
| 32 | + errors.append("Identifier contains uppercase letters;") |
| 33 | + |
| 34 | + # Check for invalid characters and collect them |
| 35 | + invalid_chars = set() |
| 36 | + for c in identifier: |
| 37 | + if not (("a" <= c <= "z") or ("0" <= c <= "9") or c == "-"): |
| 38 | + invalid_chars.add(c) |
| 39 | + |
| 40 | + if invalid_chars: |
| 41 | + chars_str = ", ".join(f"'{c}'" for c in sorted(invalid_chars)) |
| 42 | + errors.append( |
| 43 | + f"Identifier contains invalid characters: {chars_str}. " |
| 44 | + f"Only lowercase letters (a-z), numbers (0-9) and hyphens (-) are allowed." |
| 45 | + ) |
| 46 | + |
| 47 | + # Check that there are no consecutive hyphens |
| 48 | + if "--" in identifier: |
| 49 | + errors.append("Identifier contains consecutive hyphens;") |
| 50 | + |
| 51 | + # Check that the string does not end with a hyphen |
| 52 | + if identifier.endswith("-"): |
| 53 | + errors.append("Identifier ends with a hyphen;") |
| 54 | + |
| 55 | + return len(errors) == 0, errors |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def generate_identifier(input_str: str) -> str: |
| 59 | + """ |
| 60 | + Generates a valid identifier from an input string. |
| 61 | +
|
| 62 | + Args: |
| 63 | + input_str: The input string to convert to an identifier. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + A valid identifier string, or a default identifier if input is empty. |
| 67 | + """ |
| 68 | + if not input_str or input_str.isspace(): |
| 69 | + return None |
| 70 | + |
| 71 | + result = [] |
| 72 | + last_was_hyphen = False |
| 73 | + |
| 74 | + # First normalize to FormD to separate letters from their diacritics |
| 75 | + normalized = unicodedata.normalize("NFD", input_str) |
| 76 | + |
| 77 | + for c in normalized: |
| 78 | + # Check if this is a letter that needs to be preserved |
| 79 | + if "a" <= c <= "z" or "0" <= c <= "9": |
| 80 | + result.append(c) |
| 81 | + last_was_hyphen = False |
| 82 | + elif "A" <= c <= "Z": |
| 83 | + result.append(c.lower()) |
| 84 | + last_was_hyphen = False |
| 85 | + elif not last_was_hyphen and len(result) > 0: |
| 86 | + # Add hyphen for any other character |
| 87 | + result.append("-") |
| 88 | + last_was_hyphen = True |
| 89 | + |
| 90 | + # Trim any trailing hyphens |
| 91 | + final_result = "".join(result).rstrip("-") |
| 92 | + |
| 93 | + # Ensure we have at least one character |
| 94 | + return final_result if final_result else "AiConnectionStringIdentifier" |
0 commit comments