| 111 | 111 | raise argparse.ArgumentTypeError(msg) from e |
| 112 | 112 | |
| 113 | 113 | |
| 114 | def _smart_csv_split(value: str) -> list[str]: | |
| 115 | """Split a comma-separated string, respecting quoted strings. | |
| 116 | ||
| 117 | This function handles quoted strings that may contain commas. | |
| 118 | For example: 'pattern1, "pattern,with,commas", pattern3' | |
| 119 | Returns: ['pattern1', 'pattern,with,commas', 'pattern3'] | |
| 120 | """ | |
| 121 | if not value: | |
| 122 | return [] | |
| 123 | ||
| 124 | result: list[str] = [] | |
| 125 | current = "" | |
| 126 | in_quotes = False | |
| 127 | quote_char = None | |
| 128 | i = 0 | |
| 129 | ||
| 130 | while i < len(value): | |
| 131 | char = value[i] | |
| 132 | ||
| 133 | # Handle escape sequences | |
| 134 | if char == "\\" and i + 1 < len(value): | |
| 135 | # Next character is escaped, add it directly | |
| 136 | current += value[i + 1] | |
| 137 | i += 2 | |
| 138 | continue | |
| 139 | ||
| 140 | # Handle quote characters | |
| 141 | if char in '\'"' and not in_quotes: | |
| 142 | in_quotes = True | |
| 143 | quote_char = char | |
| 144 | elif char == quote_char and in_quotes: | |
| 145 | in_quotes = False | |
| 146 | quote_char = None | |
| 147 | elif char == ',' and not in_quotes: | |
| 148 | # Comma outside quotes is a separator | |
| 149 | result.append(current.strip()) | |
| 150 | current = "" | |
| 151 | else: | |
| 152 | current += char | |
| 153 | ||
| 154 | i += 1 | |
| 155 | ||
| 156 | # Add the last item | |
| 157 | if current or value.endswith(','): | |
| 158 | result.append(current.strip()) | |
| 159 | ||
| 160 | # Remove empty strings and unquote quoted strings | |
| 161 | final_result = [] | |
| 162 | for item in result: | |
| 163 | if item: # Skip empty strings | |
| 164 | # Remove quotes if the item is quoted | |
| 165 | if len(item) >= 2 and item[0] in '\'"' and item[-1] == item[0]: | |
| 166 | item = item[1:-1] | |
| 167 | final_result.append(item) | |
| 168 | ||
| 169 | return final_result | |
| 170 | ||
| 171 | ||
| 114 | 172 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | """Transforms a comma separated list of regular expressions.""" | |
| 173 | """Transforms a comma separated list of regular expressions. | |
| 174 | ||
| 175 | Handles quoted patterns that may contain commas. | |
| 176 | For example: "(foo{1,3})", "another_pattern" | |
| 177 | ||
| 178 | Also handles the case where a single pattern contains commas | |
| 179 | by trying to treat the whole input as one pattern if splitting fails. | |
| 180 | """ | |
| 116 | 181 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 118 | patterns.append(_regex_transformer(pattern)) | |
| 119 | return patterns | |
| 182 | ||
| 183 | # First try the smart CSV split | |
| 184 | try: | |
| 185 | for pattern in _smart_csv_split(value): | |
| 186 | patterns.append(_regex_transformer(pattern)) | |
| 187 | return patterns | |
| 188 | except (re.error, argparse.ArgumentTypeError): | |
| 189 | # If smart splitting fails, try treating the whole input as a single pattern | |
| 190 | # This handles cases like "(foo{1,3})" where the user intends a single pattern | |
| 191 | if value.strip(): | |
| 192 | try: | |
| 193 | patterns.append(_regex_transformer(value.strip())) | |
| 194 | return patterns | |
| 195 | except (re.error, argparse.ArgumentTypeError): | |
| 196 | # If that also fails, re-raise the original error | |
| 197 | pass | |
| 198 | ||
| 199 | # Re-raise the original error if neither approach worked | |
| 200 | raise | |
| 120 | 201 | |
| 121 | 202 | |
| 122 | 203 | def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| Test Name | Status |
|---|---|
tests/config/test_config.py::test_csv_regex_error | Fail |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo, | Fail |
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_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.