From f5e453235af5e8f1f18237f6f8e856ac780a770c Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Mon, 2 Feb 2026 17:22:53 +0000 Subject: [PATCH] Add diff and utils modules --- i18n_guardian/utils/path_utils.py | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 i18n_guardian/utils/path_utils.py diff --git a/i18n_guardian/utils/path_utils.py b/i18n_guardian/utils/path_utils.py new file mode 100644 index 0000000..49e640e --- /dev/null +++ b/i18n_guardian/utils/path_utils.py @@ -0,0 +1,55 @@ +"""Path utilities.""" + +import os +from pathlib import Path +from typing import List + +try: + import pathspec + + PATHSPEC_AVAILABLE = True +except ImportError: + PATHSPEC_AVAILABLE = False + + +def glob_patterns(patterns: List[str], base_path: Path) -> List[Path]: + """Find files matching glob patterns.""" + if not PATHSPEC_AVAILABLE: + return _glob_patterns_fallback(patterns, base_path) + + spec = pathspec.PathSpec.from_lines("gitwildmatch", patterns) + matched = [] + + for root, _dirs, files in os.walk(base_path): + root_path = Path(root) + + for filename in files: + file_path = root_path / filename + relative_path = file_path.relative_to(base_path) + + if spec.match_file(str(relative_path)): + matched.append(file_path) + + return matched + + +def _glob_patterns_fallback(patterns: List[str], base_path: Path) -> List[Path]: + """Fallback glob implementation using Path.glob.""" + matched = [] + + for pattern in patterns: + matched.extend(base_path.rglob(pattern)) + + return sorted(set(matched)) + + +def is_ignored(path: Path, ignore_patterns: List[str]) -> bool: + """Check if a path should be ignored.""" + if not PATHSPEC_AVAILABLE: + for pattern in ignore_patterns: + if path.match(pattern): + return True + return False + + spec = pathspec.PathSpec.from_lines("gitwildmatch", ignore_patterns) + return spec.match_file(str(path))