| 112 | 112 | |
| 113 | 113 | |
| 114 | 114 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | """Transforms a comma separated list of regular expressions.""" | |
| 115 | """Transforms a comma separated list of regular expressions. | |
| 116 | ||
| 117 | If the value appears to be a single regex pattern (e.g., starts with | |
| 118 | parentheses or brackets as a single unit), it will be treated as one pattern. | |
| 119 | Otherwise, it will be split on commas for multiple patterns. | |
| 120 | Commas within regex patterns can also be escaped with backslashes. | |
| 121 | """ | |
| 122 | if not value: | |
| 123 | return [] | |
| 124 | ||
| 125 | # Check if this looks like a single regex pattern | |
| 126 | def looks_like_single_pattern(s: str) -> bool: | |
| 127 | s = s.strip() | |
| 128 | if len(s) >= 2: | |
| 129 | # Check for patterns that start with parentheses or brackets | |
| 130 | if s.startswith('('): | |
| 131 | # Find the matching closing parenthesis | |
| 132 | paren_count = 0 | |
| 133 | i = 0 | |
| 134 | while i < len(s): | |
| 135 | if s[i] == '\\': | |
| 136 | i += 2 # Skip escaped character | |
| 137 | elif s[i] == '(': | |
| 138 | paren_count += 1 | |
| 139 | i += 1 | |
| 140 | elif s[i] == ')': | |
| 141 | paren_count -= 1 | |
| 142 | if paren_count == 0: | |
| 143 | # Found matching closing paren | |
| 144 | # If there's more content and it starts with comma, it's multiple patterns | |
| 145 | if i < len(s) - 1 and s[i + 1] == ',': | |
| 146 | return False | |
| 147 | # Otherwise it's a single pattern | |
| 148 | return True | |
| 149 | i += 1 | |
| 150 | else: | |
| 151 | i += 1 | |
| 152 | elif s.startswith('['): | |
| 153 | # Find the matching closing bracket | |
| 154 | bracket_count = 0 | |
| 155 | i = 0 | |
| 156 | while i < len(s): | |
| 157 | if s[i] == '\\': | |
| 158 | i += 2 # Skip escaped character | |
| 159 | elif s[i] == '[': | |
| 160 | bracket_count += 1 | |
| 161 | i += 1 | |
| 162 | elif s[i] == ']': | |
| 163 | bracket_count -= 1 | |
| 164 | if bracket_count == 0: | |
| 165 | # Found matching closing bracket | |
| 166 | # If there's more content and it starts with comma, it's multiple patterns | |
| 167 | if i < len(s) - 1 and s[i + 1] == ',': | |
| 168 | return False | |
| 169 | # Otherwise it's a single pattern | |
| 170 | return True | |
| 171 | i += 1 | |
| 172 | else: | |
| 173 | i += 1 | |
| 174 | return False | |
| 175 | ||
| 176 | # If it looks like a single pattern, treat it as such | |
| 177 | if looks_like_single_pattern(value): | |
| 178 | try: | |
| 179 | return [_regex_transformer(value)] | |
| 180 | except re.error: | |
| 181 | # If it fails to compile, fall back to CSV parsing | |
| 182 | pass | |
| 183 | ||
| 184 | # Otherwise, parse as comma-separated patterns with escape handling | |
| 116 | 185 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 118 | patterns.append(_regex_transformer(pattern)) | |
| 186 | current_pattern = [] | |
| 187 | i = 0 | |
| 188 | while i < len(value): | |
| 189 | if value[i] == '\\': | |
| 190 | # Escape sequence - include the next character as literal | |
| 191 | if i + 1 < len(value): | |
| 192 | current_pattern.append(value[i + 1]) | |
| 193 | i += 2 | |
| 194 | else: | |
| 195 | # Backslash at end, treat as literal | |
| 196 | current_pattern.append('\\') | |
| 197 | i += 1 | |
| 198 | elif value[i] == ',': | |
| 199 | # End of pattern | |
| 200 | pattern_str = ''.join(current_pattern).strip() | |
| 201 | if pattern_str: | |
| 202 | patterns.append(_regex_transformer(pattern_str)) | |
| 203 | current_pattern = [] | |
| 204 | i += 1 | |
| 205 | else: | |
| 206 | current_pattern.append(value[i]) | |
| 207 | i += 1 | |
| 208 | ||
| 209 | # Add the last pattern | |
| 210 | pattern_str = ''.join(current_pattern).strip() | |
| 211 | if pattern_str: | |
| 212 | patterns.append(_regex_transformer(pattern_str)) | |
| 213 | ||
| 119 | 214 | return patterns |
| 120 | 215 | |
| 121 | 216 |
| 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.