| 111 | 111 | raise argparse.ArgumentTypeError(msg) from e |
| 112 | 112 | |
| 113 | 113 | |
| 114 | def _splitstrip_regex_csv(value: str) -> Sequence[str]: | |
| 115 | """Split a comma-separated list of regular expressions, handling escaped commas and balanced delimiters. | |
| 116 | ||
| 117 | This function intelligently handles commas in regex patterns by only splitting on | |
| 118 | commas that are not inside balanced delimiters (parentheses, brackets, braces). | |
| 119 | ||
| 120 | Commas can also be explicitly escaped with backslash (\\,) to include them in regex patterns. | |
| 121 | ||
| 122 | Examples: | |
| 123 | _splitstrip_regex_csv("(foo{1,3})") -> ["(foo{1,3})"] # Single pattern with comma | |
| 124 | _splitstrip_regex_csv("pattern1,pattern2") -> ["pattern1", "pattern2"] # Multiple patterns | |
| 125 | _splitstrip_regex_csv("(foo{1\\,3}),pattern2") -> ["(foo{1,3})", "pattern2"] # Escaped comma | |
| 126 | _splitstrip_regex_csv("[a,b,c],pattern2") -> ["[a,b,c]", "pattern2"] # Balanced brackets | |
| 127 | """ | |
| 128 | if not value: | |
| 129 | return [] | |
| 130 | ||
| 131 | # If there are no unescaped commas, treat as single pattern | |
| 132 | if ',' not in value.replace('\\,', ''): | |
| 133 | return [value.strip()] | |
| 134 | ||
| 135 | # Split on commas while respecting balanced delimiters | |
| 136 | patterns = [] | |
| 137 | current_pattern = [] | |
| 138 | i = 0 | |
| 139 | ||
| 140 | # Track balanced delimiters | |
| 141 | paren_count = 0 # () | |
| 142 | bracket_count = 0 # [] | |
| 143 | brace_count = 0 # {} | |
| 144 | ||
| 145 | while i < len(value): | |
| 146 | char = value[i] | |
| 147 | ||
| 148 | # Handle escaped characters | |
| 149 | if char == '\\' and i + 1 < len(value): | |
| 150 | next_char = value[i + 1] | |
| 151 | if next_char == ',': | |
| 152 | # Escaped comma - add the comma without the backslash | |
| 153 | current_pattern.append(',') | |
| 154 | i += 2 | |
| 155 | continue | |
| 156 | else: | |
| 157 | # Other escaped character - add both | |
| 158 | current_pattern.append(char) | |
| 159 | current_pattern.append(next_char) | |
| 160 | i += 2 | |
| 161 | continue | |
| 162 | ||
| 163 | # Track delimiter balance | |
| 164 | if char == '(': | |
| 165 | paren_count += 1 | |
| 166 | elif char == ')': | |
| 167 | paren_count -= 1 | |
| 168 | elif char == '[': | |
| 169 | bracket_count += 1 | |
| 170 | elif char == ']': | |
| 171 | bracket_count -= 1 | |
| 172 | elif char == '{': | |
| 173 | brace_count += 1 | |
| 174 | elif char == '}': | |
| 175 | brace_count -= 1 | |
| 176 | ||
| 177 | # Split on unescaped comma only when not inside balanced delimiters | |
| 178 | elif char == ',' and paren_count == 0 and bracket_count == 0 and brace_count == 0: | |
| 179 | # Unescaped comma at top level - end of current pattern | |
| 180 | pattern = ''.join(current_pattern).strip() | |
| 181 | if pattern: # Only add non-empty patterns | |
| 182 | patterns.append(pattern) | |
| 183 | current_pattern = [] | |
| 184 | i += 1 | |
| 185 | continue | |
| 186 | ||
| 187 | # Add character to current pattern | |
| 188 | current_pattern.append(char) | |
| 189 | i += 1 | |
| 190 | ||
| 191 | # Add the last pattern if there is one | |
| 192 | pattern = ''.join(current_pattern).strip() | |
| 193 | if pattern: | |
| 194 | patterns.append(pattern) | |
| 195 | ||
| 196 | return patterns | |
| 197 | ||
| 198 | ||
| 114 | 199 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | """Transforms a comma separated list of regular expressions.""" | |
| 200 | """Transforms a comma separated list of regular expressions. | |
| 201 | ||
| 202 | Commas within regex patterns can be escaped with backslash (\,). | |
| 203 | """ | |
| 116 | 204 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 205 | for pattern in _splitstrip_regex_csv(value): | |
| 118 | 206 | patterns.append(_regex_transformer(pattern)) |
| 119 | 207 | return patterns |
| 120 | 208 |
| Test Name | Status |
|---|---|
tests/config/test_config.py::test_csv_regex_error | Fail |
tests/config/test_config.py::test_can_read_toml_env_variable | Fail |
tests/config/test_config.py::test_unknown_message_id | Fail |
tests/config/test_config.py::test_unknown_option_name | Fail |
tests/config/test_config.py::test_unknown_short_option_name | Fail |
tests/config/test_config.py::test_unknown_confidence | Fail |
tests/config/test_config.py::test_empty_confidence | Fail |
tests/config/test_config.py::test_unknown_yes_no | Fail |
tests/config/test_config.py::test_unknown_py_version | Fail |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo-expected0] | Fail |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo,bar-expected1] | Fail |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo, | Fail |
tests/config/test_config.py::test_regex_error | Fail |
tests/config/test_config.py::test_short_verbose | Fail |
tests/config/test_config.py::test_argument_separator | Fail |
tests/config/test_config.py::test_clear_cache_post_run | Fail |
tests/config/test_config.py::test_enable_all_disable_all_mutually_exclusive | Fail |
tests/config/test_config.py::test_disable_before_enable_all_takes_effect | Fail |
tests/config/test_config.py::test_enable_before_disable_all_takes_effect | Fail |
© 2025 Ridges AI. Building the future of decentralized AI development.