Check if a path should be ignored (gitignore style)
Parameters:
| Name |
Type |
Description |
Default |
path |
str
|
|
required
|
ignore |
Union[str, List[str]]
|
List of ignore patterns or a single pattern string
|
required
|
Returns:
| Name | Type |
Description |
bool |
bool
|
True if the path matches any ignore pattern, False otherwise
|
Source code in src\omoospace\validators.py
| def is_ignore(path: str, ignore: Union[str, List[str]]) -> bool:
"""Check if a path should be ignored (gitignore style)
Args:
path: Path to check
ignore: List of ignore patterns or a single pattern string
Returns:
bool: True if the path matches any ignore pattern, False otherwise
"""
# Convert single string to list
if isinstance(ignore, str):
ignore = [ignore]
# Normalize path separators to forward slashes
path = path.replace("\\", "/")
# Remove trailing slash unless it's the root directory
if path != "/" and path.endswith("/"):
path = path.rstrip("/")
# Iterate through all ignore patterns
for pattern in ignore:
# Skip empty patterns
if not pattern or pattern.strip() == "":
continue
# Normalize pattern separators
pattern = pattern.replace("\\", "/")
# Remove trailing slash from pattern unless it's the root directory
if pattern != "/" and pattern.endswith("/"):
pattern = pattern.rstrip("/")
# Case 1: Exact match of the entire path
if fnmatch.fnmatch(path, pattern):
return True
# Case 2: Pattern matches a directory in the path (e.g., "content" matches "content/file.txt")
path_parts = path.split("/")
for i in range(len(path_parts)):
partial_path = "/".join(path_parts[: i + 1])
if fnmatch.fnmatch(partial_path, pattern):
return True
# Case 3: Directory pattern (with /* suffix added)
if fnmatch.fnmatch(path, f"{pattern}/*"):
return True
return False
|