| 114 | 114 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | 115 | """Transforms a comma separated list of regular expressions.""" |
| 116 | 116 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 117 | for pattern in _split_regex_csv(value): | |
| 118 | 118 | patterns.append(_regex_transformer(pattern)) |
| 119 | 119 | return patterns |
| 120 | 120 | |
| 121 | 121 | |
| 122 | def _split_regex_csv(value: str) -> Sequence[str]: | |
| 123 | """Split a comma-separated list of regex patterns, respecting commas within regex syntax. | |
| 124 | ||
| 125 | This function parses the input string character by character, keeping track of | |
| 126 | parentheses, brackets, braces, and escape sequences to determine which commas | |
| 127 | are actual separators between patterns and which are part of regex syntax. | |
| 128 | ||
| 129 | Examples: | |
| 130 | _split_regex_csv("pattern1,pattern2") -> ["pattern1", "pattern2"] | |
| 131 | _split_regex_csv("(foo{1,3})") -> ["(foo{1,3})"] | |
| 132 | _split_regex_csv("(foo{1,3}),[bar,baz]") -> ["(foo{1,3})", "[bar,baz]"] | |
| 133 | """ | |
| 134 | if not value: | |
| 135 | return [] | |
| 136 | ||
| 137 | patterns: list[str] = [] | |
| 138 | current_pattern: list[str] = [] | |
| 139 | i = 0 | |
| 140 | n = len(value) | |
| 141 | ||
| 142 | # Track nesting levels for different types of brackets | |
| 143 | paren_depth = 0 | |
| 144 | bracket_depth = 0 | |
| 145 | brace_depth = 0 | |
| 146 | ||
| 147 | while i < n: | |
| 148 | char = value[i] | |
| 149 | ||
| 150 | # Handle escape sequences | |
| 151 | if char == '\\': | |
| 152 | # Add the backslash and the next character (if any) to the current pattern | |
| 153 | current_pattern.append(char) | |
| 154 | if i + 1 < n: | |
| 155 | next_char = value[i + 1] | |
| 156 | # If the escaped character is a comma and we're not nested, treat it as a literal comma | |
| 157 | if next_char == ',' and paren_depth == 0 and bracket_depth == 0 and brace_depth == 0: | |
| 158 | # This is an escaped comma, add it as a literal comma | |
| 159 | current_pattern.append(next_char) | |
| 160 | i += 1 | |
| 161 | else: | |
| 162 | # Regular escape sequence | |
| 163 | current_pattern.append(next_char) | |
| 164 | i += 1 | |
| 165 | # Handle opening brackets | |
| 166 | elif char == '(': | |
| 167 | paren_depth += 1 | |
| 168 | current_pattern.append(char) | |
| 169 | elif char == '[': | |
| 170 | bracket_depth += 1 | |
| 171 | current_pattern.append(char) | |
| 172 | elif char == '{': | |
| 173 | brace_depth += 1 | |
| 174 | current_pattern.append(char) | |
| 175 | # Handle closing brackets | |
| 176 | elif char == ')': | |
| 177 | paren_depth = max(0, paren_depth - 1) | |
| 178 | current_pattern.append(char) | |
| 179 | elif char == ']': | |
| 180 | bracket_depth = max(0, bracket_depth - 1) | |
| 181 | current_pattern.append(char) | |
| 182 | elif char == '}': | |
| 183 | brace_depth = max(0, brace_depth - 1) | |
| 184 | current_pattern.append(char) | |
| 185 | # Handle comma as separator (only when not nested) | |
| 186 | elif char == ',' and paren_depth == 0 and bracket_depth == 0 and brace_depth == 0: | |
| 187 | # This is a separator between patterns | |
| 188 | pattern_str = ''.join(current_pattern).strip() | |
| 189 | if pattern_str: # Only add non-empty patterns | |
| 190 | patterns.append(pattern_str) | |
| 191 | current_pattern = [] | |
| 192 | else: | |
| 193 | # Regular character, add to current pattern | |
| 194 | current_pattern.append(char) | |
| 195 | ||
| 196 | i += 1 | |
| 197 | ||
| 198 | # Add the last pattern | |
| 199 | pattern_str = ''.join(current_pattern).strip() | |
| 200 | if pattern_str: # Only add non-empty patterns | |
| 201 | patterns.append(pattern_str) | |
| 202 | ||
| 203 | return patterns | |
| 204 | ||
| 205 | ||
| 122 | 206 | def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 123 | 207 | """Transforms a comma separated list of regular expressions paths.""" |
| 124 | 208 | patterns: list[Pattern[str]] = [] |
| 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.