|
1 | 1 | import os |
| 2 | +import re |
2 | 3 | import string |
3 | 4 | import uuid |
4 | 5 | from typing import Any |
@@ -77,17 +78,45 @@ def _normalize_pattern(pattern: str) -> str: |
77 | 78 |
|
78 | 79 |
|
79 | 80 | def _parse_patterns(pattern: list[str] | str) -> list[str]: |
| 81 | + """ |
| 82 | + Parse and validate file/directory patterns for inclusion or exclusion. |
| 83 | +
|
| 84 | + Takes either a single pattern string or list of pattern strings and processes them into a normalized list. |
| 85 | + Patterns are split on commas and spaces, validated for allowed characters, and normalized. |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + pattern : list[str] | str |
| 90 | + Pattern(s) to parse - either a single string or list of strings |
| 91 | +
|
| 92 | + Returns |
| 93 | + ------- |
| 94 | + list[str] |
| 95 | + List of normalized pattern strings |
| 96 | +
|
| 97 | + Raises |
| 98 | + ------ |
| 99 | + ValueError |
| 100 | + If any pattern contains invalid characters. Only alphanumeric characters, |
| 101 | + dash (-), underscore (_), dot (.), forward slash (/), plus (+), and |
| 102 | + asterisk (*) are allowed. |
| 103 | + """ |
80 | 104 | patterns = pattern if isinstance(pattern, list) else [pattern] |
81 | | - patterns = [p.strip() for p in patterns] |
82 | 105 |
|
| 106 | + parsed_patterns = [] |
83 | 107 | for p in patterns: |
| 108 | + parsed_patterns.extend(re.split(",| ", p)) |
| 109 | + |
| 110 | + parsed_patterns = [p for p in parsed_patterns if p != ""] |
| 111 | + |
| 112 | + for p in parsed_patterns: |
84 | 113 | if not all(c.isalnum() or c in "-_./+*" for c in p): |
85 | 114 | raise ValueError( |
86 | 115 | f"Pattern '{p}' contains invalid characters. Only alphanumeric characters, dash (-), " |
87 | 116 | "underscore (_), dot (.), forward slash (/), plus (+), and asterisk (*) are allowed." |
88 | 117 | ) |
89 | 118 |
|
90 | | - return [_normalize_pattern(p) for p in patterns] |
| 119 | + return [_normalize_pattern(p) for p in parsed_patterns] |
91 | 120 |
|
92 | 121 |
|
93 | 122 | def _override_ignore_patterns(ignore_patterns: list[str], include_patterns: list[str]) -> list[str]: |
|
0 commit comments