| 113 | 113 | |
| 114 | 114 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | 115 | """Transforms a comma separated list of regular expressions.""" |
| 116 | if not value: | |
| 117 | return [] | |
| 118 | ||
| 119 | # Parse comma-separated regex patterns while respecting regex syntax | |
| 116 | 120 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 118 | patterns.append(_regex_transformer(pattern)) | |
| 121 | current_pattern = "" | |
| 122 | i = 0 | |
| 123 | ||
| 124 | while i < len(value): | |
| 125 | char = value[i] | |
| 126 | ||
| 127 | if char == ',': | |
| 128 | # Check if this comma is inside regex syntax that should be preserved | |
| 129 | if _is_comma_escaped_or_in_regex_syntax(value, i, current_pattern): | |
| 130 | current_pattern += char | |
| 131 | else: | |
| 132 | # This is a CSV separator | |
| 133 | if current_pattern.strip(): | |
| 134 | patterns.append(_regex_transformer(current_pattern.strip())) | |
| 135 | current_pattern = "" | |
| 136 | else: | |
| 137 | current_pattern += char | |
| 138 | ||
| 139 | i += 1 | |
| 140 | ||
| 141 | # Add the last pattern | |
| 142 | if current_pattern.strip(): | |
| 143 | patterns.append(_regex_transformer(current_pattern.strip())) | |
| 144 | ||
| 119 | 145 | return patterns |
| 120 | 146 | |
| 147 | def _is_comma_escaped_or_in_regex_syntax(value: str, comma_pos: int, current_pattern: str) -> bool: | |
| 148 | """Check if a comma at comma_pos is part of regex syntax that should be preserved. | |
| 149 | ||
| 150 | This handles cases like: | |
| 151 | - Quantifiers: {1,3} | |
| 152 | - Character classes: [a,b,c] | |
| 153 | """ | |
| 154 | # Count quotes to handle quoted strings | |
| 155 | single_quotes = current_pattern.count("'") | |
| 156 | double_quotes = current_pattern.count('"') | |
| 157 | ||
| 158 | # If we're inside quotes, preserve the comma | |
| 159 | if single_quotes % 2 == 1 or double_quotes % 2 == 1: | |
| 160 | return True | |
| 161 | ||
| 162 | # Check if we're inside a character class | |
| 163 | if '[' in current_pattern and ']' not in current_pattern[current_pattern.rfind('['):]: | |
| 164 | return True | |
| 165 | ||
| 166 | # Check if we're inside a quantifier-like pattern | |
| 167 | # Look for patterns like {n,m} or {n,} | |
| 168 | brace_pos = current_pattern.rfind('{') | |
| 169 | if brace_pos != -1 and '}' not in current_pattern[brace_pos:]: | |
| 170 | # We're inside braces, check if it looks like a quantifier | |
| 171 | content_after_brace = current_pattern[brace_pos+1:] | |
| 172 | if ',' in content_after_brace or content_after_brace.isdigit() or content_after_brace == "": | |
| 173 | return True | |
| 174 | ||
| 175 | return False | |
| 176 | ||
| 177 | def _is_comma_escaped_or_in_regex_syntax(value: str, comma_pos: int, current_pattern: str) -> bool: | |
| 178 | """Check if a comma at comma_pos is part of regex syntax that should be preserved. | |
| 179 | ||
| 180 | This handles cases like: | |
| 181 | - Quantifiers: {1,3} | |
| 182 | - Character classes: [a,b,c] | |
| 183 | """ | |
| 184 | # Count quotes to handle quoted strings | |
| 185 | single_quotes = current_pattern.count("'") | |
| 186 | double_quotes = current_pattern.count('"') | |
| 187 | ||
| 188 | # If we're inside quotes, preserve the comma | |
| 189 | if single_quotes % 2 == 1 or double_quotes % 2 == 1: | |
| 190 | return True | |
| 191 | ||
| 192 | # Check if we're inside a character class | |
| 193 | if '[' in current_pattern and ']' not in current_pattern[current_pattern.rfind('['):] and '{' not in current_pattern: | |
| 194 | return True | |
| 195 | ||
| 196 | # Check if we're inside a quantifier-like pattern | |
| 197 | # Look for patterns like {n,m} or {n,} | |
| 198 | brace_pos = current_pattern.rfind('{') | |
| 199 | if brace_pos != -1 and '}' not in current_pattern[brace_pos:]: | |
| 200 | # We're inside braces, check if it looks like a quantifier | |
| 201 | content_after_brace = current_pattern[brace_pos+1:] | |
| 202 | if ',' in content_after_brace or content_after_brace.isdigit() or content_after_brace == "": | |
| 203 | return True | |
| 204 | ||
| 205 | return False | |
| 206 | ||
| 121 | 207 | |
| 122 | 208 | def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 123 | 209 | """Transforms a comma separated list of regular expressions paths.""" |
| 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.