| 111 | 111 | raise argparse.ArgumentTypeError(msg) from e |
| 112 | 112 | |
| 113 | 113 | |
| 114 | def _regex_csv_split(value: str) -> list[str]: | |
| 115 | """Split a comma-separated list of regex patterns, respecting regex syntax. | |
| 116 | ||
| 117 | This function handles commas within regex metacharacters like: | |
| 118 | - Quantifiers: {1,3}, {2,} | |
| 119 | - Character classes: [a,b,c] | |
| 120 | - Escaped commas: \\, | |
| 121 | """ | |
| 122 | if not value: | |
| 123 | return [] | |
| 124 | ||
| 125 | result = [] | |
| 126 | current = "" | |
| 127 | i = 0 | |
| 128 | while i < len(value): | |
| 129 | char = value[i] | |
| 130 | ||
| 131 | # Handle escaped characters | |
| 132 | if char == "\\": | |
| 133 | if i + 1 < len(value): | |
| 134 | current += char + value[i + 1] | |
| 135 | i += 2 | |
| 136 | continue | |
| 137 | else: | |
| 138 | current += char | |
| 139 | i += 1 | |
| 140 | continue | |
| 141 | ||
| 142 | # Handle character classes [...] | |
| 143 | if char == "[": | |
| 144 | bracket_depth = 1 | |
| 145 | current += char | |
| 146 | i += 1 | |
| 147 | while i < len(value) and bracket_depth > 0: | |
| 148 | if value[i] == "\\": | |
| 149 | if i + 1 < len(value): | |
| 150 | current += value[i] + value[i + 1] | |
| 151 | i += 2 | |
| 152 | continue | |
| 153 | else: | |
| 154 | current += value[i] | |
| 155 | i += 1 | |
| 156 | continue | |
| 157 | elif value[i] == "[": | |
| 158 | bracket_depth += 1 | |
| 159 | elif value[i] == "]": | |
| 160 | bracket_depth -= 1 | |
| 161 | current += value[i] | |
| 162 | i += 1 | |
| 163 | continue | |
| 164 | ||
| 165 | # Handle groups (...) | |
| 166 | if char == "(": | |
| 167 | paren_depth = 1 | |
| 168 | current += char | |
| 169 | i += 1 | |
| 170 | while i < len(value) and paren_depth > 0: | |
| 171 | if value[i] == "\\": | |
| 172 | if i + 1 < len(value): | |
| 173 | current += value[i] + value[i + 1] | |
| 174 | i += 2 | |
| 175 | continue | |
| 176 | else: | |
| 177 | current += value[i] | |
| 178 | i += 1 | |
| 179 | continue | |
| 180 | elif value[i] == "(": | |
| 181 | paren_depth += 1 | |
| 182 | elif value[i] == ")": | |
| 183 | paren_depth -= 1 | |
| 184 | current += value[i] | |
| 185 | i += 1 | |
| 186 | continue | |
| 187 | ||
| 188 | # Handle quantifiers {...} | |
| 189 | if char == "{": | |
| 190 | brace_depth = 1 | |
| 191 | current += char | |
| 192 | i += 1 | |
| 193 | while i < len(value) and brace_depth > 0: | |
| 194 | if value[i] == "\\": | |
| 195 | if i + 1 < len(value): | |
| 196 | current += value[i] + value[i + 1] | |
| 197 | i += 2 | |
| 198 | continue | |
| 199 | else: | |
| 200 | current += value[i] | |
| 201 | i += 1 | |
| 202 | continue | |
| 203 | elif value[i] == "{": | |
| 204 | brace_depth += 1 | |
| 205 | elif value[i] == "}": | |
| 206 | brace_depth -= 1 | |
| 207 | current += value[i] | |
| 208 | i += 1 | |
| 209 | continue | |
| 210 | ||
| 211 | # Handle comma separation at top level | |
| 212 | if char == ",": | |
| 213 | # Only split on commas not inside regex constructs | |
| 214 | result.append(current.strip()) | |
| 215 | current = "" | |
| 216 | else: | |
| 217 | current += char | |
| 218 | ||
| 219 | i += 1 | |
| 220 | ||
| 221 | # Add the last pattern | |
| 222 | if current.strip(): | |
| 223 | result.append(current.strip()) | |
| 224 | ||
| 225 | return result | |
| 226 | ||
| 227 | ||
| 114 | 228 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | 229 | """Transforms a comma separated list of regular expressions.""" |
| 116 | 230 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 231 | for pattern in _regex_csv_split(value): | |
| 118 | 232 | patterns.append(_regex_transformer(pattern)) |
| 119 | 233 | return patterns |
| 120 | 234 |
| Test Name | Status |
|---|---|
tests/config/test_config.py::test_csv_regex_error | 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_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.