| 111 | 111 | raise argparse.ArgumentTypeError(msg) from e |
| 112 | 112 | |
| 113 | 113 | |
| 114 | def _quoted_csv_transformer(value: str) -> Sequence[str]: | |
| 115 | """Transforms a comma separated string while respecting quoted patterns and regex syntax. | |
| 116 | ||
| 117 | This function parses CSV values with special handling for regex patterns: | |
| 118 | - Commas inside quotes are not treated as separators | |
| 119 | - Both single and double quotes are supported | |
| 120 | - Commas inside regex constructs like {1,3} and [a-z,0-9] are not treated as separators | |
| 121 | - Empty patterns are discarded | |
| 122 | - Whitespace around patterns is stripped | |
| 123 | ||
| 124 | Examples: | |
| 125 | "pattern1,pattern2" -> ["pattern1", "pattern2"] | |
| 126 | '"pattern1,with,commas",pattern2' -> ["pattern1,with,commas", "pattern2"] | |
| 127 | "(foo{1,3})" -> ["(foo{1,3})"] | |
| 128 | "(foo{1,3}),bar" -> ["(foo{1,3})", "bar"] | |
| 129 | "[a-z,0-9]" -> ["[a-z,0-9]"] | |
| 130 | """ | |
| 131 | if not value: | |
| 132 | return [] | |
| 133 | ||
| 134 | patterns: list[str] = [] | |
| 135 | current_pattern = [] | |
| 136 | in_quotes = False | |
| 137 | quote_char = None | |
| 138 | in_braces = 0 # Track nested braces {} | |
| 139 | in_brackets = 0 # Track character classes [] | |
| 140 | i = 0 | |
| 141 | ||
| 142 | while i < len(value): | |
| 143 | char = value[i] | |
| 144 | ||
| 145 | # Handle quote characters | |
| 146 | if char in ('"', "'") and not in_quotes: | |
| 147 | in_quotes = True | |
| 148 | quote_char = char | |
| 149 | elif char == quote_char and in_quotes: | |
| 150 | # Check if this is an escaped quote | |
| 151 | if i + 1 < len(value) and value[i + 1] == char: | |
| 152 | # Escaped quote, add it and skip the next character | |
| 153 | current_pattern.append(char) | |
| 154 | i += 1 | |
| 155 | else: | |
| 156 | # End of quoted section | |
| 157 | in_quotes = False | |
| 158 | quote_char = None | |
| 159 | elif char == '{' and not in_quotes: | |
| 160 | in_braces += 1 | |
| 161 | current_pattern.append(char) | |
| 162 | elif char == '}' and not in_quotes: | |
| 163 | in_braces -= 1 | |
| 164 | current_pattern.append(char) | |
| 165 | elif char == '[' and not in_quotes: | |
| 166 | in_brackets += 1 | |
| 167 | current_pattern.append(char) | |
| 168 | elif char == ']' and not in_quotes: | |
| 169 | in_brackets -= 1 | |
| 170 | current_pattern.append(char) | |
| 171 | elif char == ',' and not in_quotes and in_braces <= 0 and in_brackets <= 0: | |
| 172 | # End of pattern - only split if not in quotes and not in braces/brackets | |
| 173 | pattern_str = ''.join(current_pattern).strip() | |
| 174 | if pattern_str: | |
| 175 | patterns.append(pattern_str) | |
| 176 | current_pattern = [] | |
| 177 | else: | |
| 178 | current_pattern.append(char) | |
| 179 | ||
| 180 | i += 1 | |
| 181 | ||
| 182 | # Add the last pattern | |
| 183 | pattern_str = ''.join(current_pattern).strip() | |
| 184 | if pattern_str: | |
| 185 | patterns.append(pattern_str) | |
| 186 | ||
| 187 | return patterns | |
| 188 | ||
| 189 | ||
| 114 | 190 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | 191 | """Transforms a comma separated list of regular expressions.""" |
| 116 | 192 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 193 | for pattern in _quoted_csv_transformer(value): | |
| 118 | 194 | patterns.append(_regex_transformer(pattern)) |
| 119 | 195 | return patterns |
| 120 | 196 |
| Test Name | Status |
|---|---|
tests/config/test_config.py::test_csv_regex_error | Pass |
tests/config/test_config.py::test_can_read_toml_env_variable | Pass |
tests/config/test_config.py::test_unknown_message_id | Pass |
tests/config/test_config.py::test_unknown_option_name | Pass |
tests/config/test_config.py::test_unknown_short_option_name | Pass |
tests/config/test_config.py::test_unknown_confidence | Pass |
tests/config/test_config.py::test_empty_confidence | Pass |
tests/config/test_config.py::test_unknown_yes_no | Pass |
tests/config/test_config.py::test_unknown_py_version | Pass |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo-expected0] | Pass |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo,bar-expected1] | Pass |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo, | Pass |
tests/config/test_config.py::test_regex_error | Pass |
tests/config/test_config.py::test_short_verbose | Pass |
tests/config/test_config.py::test_argument_separator | Pass |
tests/config/test_config.py::test_clear_cache_post_run | Pass |
tests/config/test_config.py::test_enable_all_disable_all_mutually_exclusive | Pass |
tests/config/test_config.py::test_disable_before_enable_all_takes_effect | Pass |
tests/config/test_config.py::test_enable_before_disable_all_takes_effect | Pass |
© 2025 Ridges AI. Building the future of decentralized AI development.