Skip to content

Commit

Permalink
ignore files which are hidden and in gitignore
Browse files Browse the repository at this point in the history
Signed-off-by: ravi_kumar_pilla <[email protected]>
  • Loading branch information
ravi-kumar-pilla committed Feb 12, 2025
1 parent 506437f commit d052dd1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
13 changes: 3 additions & 10 deletions package/kedro_viz/autoreload_file_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
from typing import Optional, Set

from kedro_viz.utils import load_gitignore_patterns
from pathspec import GitIgnoreSpec
from watchfiles import Change, DefaultFilter

Expand Down Expand Up @@ -35,16 +36,8 @@ def __init__(self, base_path: Optional[Path] = None):
super().__init__()

# Load .gitignore patterns
gitignore_path = self.cwd / ".gitignore"
try:
with open(gitignore_path, "r", encoding="utf-8") as gitignore_file:
ignore_patterns = gitignore_file.read().splitlines()
self.gitignore_spec: Optional[GitIgnoreSpec] = GitIgnoreSpec.from_lines(
"gitwildmatch", ignore_patterns
)
except FileNotFoundError:
self.gitignore_spec = None

self.gitignore_spec = load_gitignore_patterns(self.cwd)

def __call__(self, change: Change, path: str) -> bool:
"""
Determine whether a file change should be processed.
Expand Down
10 changes: 9 additions & 1 deletion package/kedro_viz/integrations/kedro/lite_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from unittest.mock import MagicMock

from kedro_viz.integrations.utils import Spinner
from kedro_viz.utils import is_file_ignored, load_gitignore_patterns

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -243,7 +244,14 @@ def parse(self, target_path: Path) -> Union[Dict[str, Set[str]], None]:

unresolved_imports: Dict[str, Set[str]] = {}

# Load .gitignore patterns
gitignore_spec = load_gitignore_patterns(target_path)

if target_path.is_file():

if is_file_ignored(target_path):
return unresolved_imports

try:
missing_dependencies = self._get_unresolved_imports(target_path)
if len(missing_dependencies) > 0:
Expand All @@ -258,7 +266,7 @@ def parse(self, target_path: Path) -> Union[Dict[str, Set[str]], None]:
return unresolved_imports

# handling directories
_project_file_paths = set(target_path.rglob("*.py"))
_project_file_paths = set(file_path for file_path in target_path.rglob("*.py") if not is_file_ignored(file_path, target_path, gitignore_spec))

for file_path in _project_file_paths:
try:
Expand Down
26 changes: 25 additions & 1 deletion package/kedro_viz/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Transcoding related utility functions."""

import hashlib
from typing import Tuple
from pathlib import Path
from typing import Optional, Tuple

from pathspec import GitIgnoreSpec

TRANSCODING_SEPARATOR = "@"

Expand Down Expand Up @@ -57,3 +60,24 @@ def _strip_transcoding(element: str) -> str:
def is_dataset_param(dataset_name: str) -> bool:
"""Return whether a dataset is a parameter"""
return dataset_name.lower().startswith("params:") or dataset_name == "parameters"

def load_gitignore_patterns(project_path: Path) -> Optional[GitIgnoreSpec]:
gitignore_path = project_path / ".gitignore"

if not gitignore_path.exists():
return

with open(gitignore_path, "r", encoding="utf-8") as gitignore_file:
ignore_patterns = gitignore_file.read().splitlines()
gitignore_spec = GitIgnoreSpec.from_lines(
"gitwildmatch", ignore_patterns
)
return gitignore_spec

def is_file_ignored(file_path: Path, project_path: Optional[Path], gitignore_spec: Optional[GitIgnoreSpec]) -> bool:
"""Returns True if the file should be ignored."""
if file_path.name.startswith("."): # Ignore hidden files/folders
return True
if gitignore_spec and project_path and gitignore_spec.match_file(str(file_path.relative_to(project_path))):
return True
return False

0 comments on commit d052dd1

Please sign in to comment.