-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to support collection backends, allowing for changing output…
… path
- Loading branch information
Showing
13 changed files
with
291 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from dataclasses import dataclass | ||
from functools import lru_cache | ||
|
||
@lru_cache | ||
def get_backends() -> dict[str, type['CollectionTask']]: | ||
# do this in a cached function to avoid circular import | ||
from .url import UrlCollectionTask | ||
from .path import PathCollectionTask | ||
|
||
return { | ||
'url': UrlCollectionTask, | ||
'path': PathCollectionTask, | ||
} | ||
|
||
@dataclass | ||
class CollectionTask: | ||
@classmethod | ||
def from_dict(cls, data: dict) -> 'CollectionTask': | ||
backend = data.pop('backend') | ||
return get_backends()[backend](**data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
from dataclasses import dataclass | ||
from ..utils import get_unique_path | ||
from .base import CollectionTask | ||
|
||
@dataclass | ||
class PathCollectionTask(CollectionTask): | ||
"""Collect files or directories from the local filesystem.""" | ||
path: Path | ||
output: Path | None = None | ||
hard_links: bool = False | ||
|
||
ignore_patterns = ['.DS_Store'] | ||
|
||
def __post_init__(self): | ||
"""Validate the path and ensure it's a Path object.""" | ||
self.path = Path(self.path) # Coerce to Path if it's a string | ||
if not self.path.exists(): | ||
raise ValueError(f'Path "{self.path}" does not exist') | ||
if self.output is not None: | ||
self.output = Path(self.output) # Also coerce output if provided | ||
|
||
def collect(self, files_dir: Path) -> None: | ||
"""Copy paths to a destination directory, optionally using hard links.""" | ||
path = self.path | ||
dest_path = get_unique_path(files_dir / path.name) | ||
# can only use hard links if source and destination are on the same device | ||
use_hard_links = self.hard_links and os.stat(path).st_dev == os.stat(files_dir).st_dev | ||
if path.is_file(): | ||
if use_hard_links: | ||
os.link(path, dest_path) | ||
else: | ||
shutil.copy2(path, dest_path) | ||
else: | ||
copy_function = os.link if use_hard_links else shutil.copy2 | ||
# link directory contents recursively | ||
shutil.copytree( | ||
path, | ||
dest_path, | ||
dirs_exist_ok=True, | ||
copy_function=copy_function, | ||
ignore=shutil.ignore_patterns(*self.ignore_patterns) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.