| 111 | 111 | raise argparse.ArgumentTypeError(msg) from e |
| 112 | 112 | |
| 113 | 113 | |
| 114 | def _unquote(string: str) -> str: | |
| 115 | """Remove optional quotes (simple or double) from the string.""" | |
| 116 | if not string: | |
| 117 | return string | |
| 118 | if string[0] in "'\"" and string[0] == string[-1]: | |
| 119 | return string[1:-1] | |
| 120 | return string | |
| 121 | ||
| 122 | def _split_csv_aware_of_quotes(value: str) -> Sequence[str]: | |
| 123 | """Split a CSV string while respecting quotes. | |
| 124 | ||
| 125 | This function handles commas inside quoted strings, allowing regex patterns | |
| 126 | with commas to be properly parsed when quoted. | |
| 127 | ||
| 128 | Examples: | |
| 129 | _split_csv_aware_of_quotes('pattern1,pattern2') -> ['pattern1', 'pattern2'] | |
| 130 | _split_csv_aware_of_quotes('"pattern{1,3}",pattern2') -> ['pattern{1,3}', 'pattern2'] | |
| 131 | _split_csv_aware_of_quotes("'pattern{1,3}',pattern2") -> ['pattern{1,3}', 'pattern2'] | |
| 132 | """ | |
| 133 | if not value: | |
| 134 | return [] | |
| 135 | ||
| 136 | result = [] | |
| 137 | current = "" | |
| 138 | in_single_quote = False | |
| 139 | in_double_quote = False | |
| 140 | i = 0 | |
| 141 | ||
| 142 | while i < len(value): | |
| 143 | char = value[i] | |
| 144 | ||
| 145 | if char == '\\' and i + 1 < len(value): | |
| 146 | # Handle escaped characters | |
| 147 | current += value[i:i+2] | |
| 148 | i += 2 | |
| 149 | continue | |
| 150 | ||
| 151 | if char == "'" and not in_double_quote: | |
| 152 | in_single_quote = not in_single_quote | |
| 153 | current += char | |
| 154 | elif char == '"' and not in_single_quote: | |
| 155 | in_double_quote = not in_double_quote | |
| 156 | current += char | |
| 157 | elif char == ',' and not in_single_quote and not in_double_quote: | |
| 158 | # Comma outside of quotes, split here | |
| 159 | result.append(current.strip()) | |
| 160 | current = "" | |
| 161 | else: | |
| 162 | current += char | |
| 163 | ||
| 164 | i += 1 | |
| 165 | ||
| 166 | # Add the last item | |
| 167 | if current or not result: | |
| 168 | result.append(current.strip()) | |
| 169 | ||
| 170 | # Remove quotes from quoted items and filter out empty strings | |
| 171 | return [_unquote(item) for item in result if item.strip()] | |
| 172 | ||
| 173 | ||
| 114 | 174 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | 175 | """Transforms a comma separated list of regular expressions.""" |
| 116 | 176 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 177 | for pattern in _split_csv_aware_of_quotes(value): | |
| 118 | 178 | patterns.append(_regex_transformer(pattern)) |
| 119 | 179 | return patterns |
| 120 | 180 | |
| 122 | 182 | def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 123 | 183 | """Transforms a comma separated list of regular expressions paths.""" |
| 124 | 184 | patterns: list[Pattern[str]] = [] |
| 125 | for pattern in _csv_transformer(value): | |
| 185 | for pattern in _split_csv_aware_of_quotes(value): | |
| 126 | 186 | patterns.append( |
| 127 | 187 | re.compile( |
| 128 | 188 | str(pathlib.PureWindowsPath(pattern)).replace("\\", "\\\\") |
| 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.