-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from OpenMined/yash/refactor
Refactor watchdog
- Loading branch information
Showing
4 changed files
with
129 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from pathlib import Path | ||
from typing import Callable | ||
|
||
from watchdog.events import FileSystemEvent, FileSystemEventHandler | ||
from watchdog.observers import Observer | ||
|
||
__all__ = [ | ||
"FSWatchdog", | ||
"AnyFileSystemEventHandler", | ||
"FSEventCallbacks", | ||
"FileSystemEvent", | ||
] | ||
|
||
type FSEventCallbacks = list[Callable[[FileSystemEvent], None]] | ||
|
||
|
||
class FSWatchdog: | ||
def __init__(self, watch_dir: Path, event_handler: FileSystemEventHandler): | ||
self.watch_dir = watch_dir | ||
self.event_handler = event_handler | ||
self._observer = Observer() | ||
|
||
def start(self): | ||
# observer starts it's own thread | ||
self._observer.schedule( | ||
self.event_handler, | ||
self.watch_dir, | ||
recursive=True, | ||
) | ||
self._observer.start() | ||
|
||
def stop(self): | ||
self._observer.stop() | ||
self._observer.join() | ||
|
||
|
||
class AnyFileSystemEventHandler(FileSystemEventHandler): | ||
def __init__( | ||
self, | ||
watch_dir: Path, | ||
callbacks: FSEventCallbacks, | ||
ignored: list[str] = [], | ||
): | ||
self.watch_dir = watch_dir | ||
self.callbacks = callbacks | ||
self.ignored = [Path(self.watch_dir, ignore) for ignore in ignored] | ||
|
||
def on_any_event(self, event: FileSystemEvent) -> None: | ||
for ignore in self.ignored: | ||
if event.src_path.startswith(str(ignore)): | ||
return | ||
|
||
for cb in self.callbacks: | ||
cb(event) |
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,52 @@ | ||
import subprocess | ||
from pathlib import Path | ||
|
||
ASSETS_FOLDER = Path(__file__).parents[2] / "assets" | ||
ICONS_PKG = ASSETS_FOLDER / "icon.zip" | ||
|
||
|
||
# Function to search for Icon\r file | ||
def search_icon_file(src_path: Path) -> Path: | ||
if not src_path.exists(): | ||
return None | ||
for file_path in src_path.iterdir(): | ||
if "Icon" in file_path.name and "\r" in file_path.name: | ||
return file_path | ||
|
||
|
||
# if you knew the pain of this function | ||
def find_icon_file(src_path: Path) -> Path: | ||
# First attempt to find the Icon\r file | ||
icon_file = search_icon_file(src_path) | ||
if icon_file: | ||
return icon_file | ||
|
||
if not ICONS_PKG.exists(): | ||
# If still not found, raise an error | ||
raise FileNotFoundError(f"{ICONS_PKG} not found") | ||
|
||
try: | ||
# cant use other zip tools as they don't unpack it correctly | ||
subprocess.run( | ||
["ditto", "-xk", str(ICONS_PKG), str(src_path.parent)], | ||
check=True, | ||
) | ||
|
||
# Try to find the Icon\r file again after extraction | ||
icon_file = search_icon_file(src_path) | ||
if icon_file: | ||
return icon_file | ||
except subprocess.CalledProcessError: | ||
raise RuntimeError("Failed to unzip icon.zip using macOS CLI tool.") | ||
|
||
|
||
def copy_icon_file(icon_folder: str, dest_folder: str) -> None: | ||
dest_path = Path(dest_folder) | ||
icon_path = Path(icon_folder) | ||
src_icon_path = find_icon_file(icon_path) | ||
if not dest_path.exists(): | ||
raise FileNotFoundError(f"Destination folder '{dest_folder}' does not exist.") | ||
|
||
# shutil wont work with these special icon files | ||
subprocess.run(["cp", "-p", src_icon_path, dest_folder], check=True) | ||
subprocess.run(["SetFile", "-a", "C", dest_folder], check=True) |