| 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 | ||
| 117 | :param string: an optionally quoted string | |
| 118 | :return: the unquoted string (or the input string if it wasn't quoted) | |
| 119 | """ | |
| 120 | if not string: | |
| 121 | return string | |
| 122 | if len(string) >= 2 and string[0] == string[-1] and string[0] in "\"'": | |
| 123 | return string[1:-1] | |
| 124 | return string | |
| 125 | ||
| 126 | def _splitstrip_quoted(string: str, sep: str = ",") -> list[str]: | |
| 127 | """Return a list of stripped string by splitting the string given as | |
| 128 | argument on `sep` (',' by default), respecting quoted strings. | |
| 129 | Empty strings are discarded. | |
| 130 | ||
| 131 | This function handles quoted strings, so commas within quotes are not | |
| 132 | treated as separators. | |
| 133 | ||
| 134 | >>> _splitstrip_quoted('a, b, "c, d", e') | |
| 135 | ['a', 'b', 'c, d', 'e'] | |
| 136 | >>> _splitstrip_quoted('pattern1, "(foo{1,3})"') | |
| 137 | ['pattern1', '(foo{1,3})'] | |
| 138 | ||
| 139 | :param string: a csv line that may contain quoted strings | |
| 140 | :param sep: field separator, default to the comma (',') | |
| 141 | :return: list of split and stripped strings | |
| 142 | """ | |
| 143 | import shlex | |
| 144 | try: | |
| 145 | # Use shlex to properly handle quoted strings | |
| 146 | parts = shlex.split(string, posix=True) | |
| 147 | # Filter out empty strings and unquote | |
| 148 | return [_unquote(part.strip()) for part in parts if part.strip()] | |
| 149 | except ValueError: | |
| 150 | # Fallback to simple split if shlex fails (e.g., unmatched quotes) | |
| 151 | return [_unquote(word.strip()) for word in string.split(sep) if word.strip()] | |
| 152 | ||
| 153 | ||
| 114 | 154 | def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]: |
| 115 | """Transforms a comma separated list of regular expressions.""" | |
| 155 | """Transforms a comma separated list of regular expressions. | |
| 156 | ||
| 157 | This function respects quoted strings, so regex patterns containing | |
| 158 | commas can be quoted to prevent them from being split. | |
| 159 | """ | |
| 116 | 160 | patterns: list[Pattern[str]] = [] |
| 117 | for pattern in _csv_transformer(value): | |
| 161 | for pattern in _splitstrip_quoted(value): | |
| 118 | 162 | patterns.append(_regex_transformer(pattern)) |
| 119 | 163 | return patterns |
| 120 | 164 |
| 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,bar-expected1] | 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_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.