-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
27 additions
and
80 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 |
---|---|---|
|
@@ -2,4 +2,4 @@ guessit | |
aiohttp | ||
jsons | ||
black-with-tabs | ||
watchdog | ||
watchfiles |
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 |
---|---|---|
@@ -1,74 +1,19 @@ | ||
import asyncio | ||
from functools import wraps | ||
from watchdog.observers import Observer | ||
from watchdog.events import ( | ||
FileSystemEventHandler, | ||
DirCreatedEvent, | ||
FileCreatedEvent, | ||
DirMovedEvent, | ||
FileMovedEvent, | ||
DirDeletedEvent, | ||
FileDeletedEvent, | ||
) | ||
|
||
from scanner.utils import log_errors | ||
|
||
import logging | ||
from watchfiles import awatch, Change | ||
from .utils import ProviderError | ||
from .scanner import Scanner | ||
|
||
task_list = [] | ||
event = asyncio.Event() | ||
|
||
|
||
async def monitor(path: str, scanner: Scanner): | ||
global task_list | ||
|
||
observer = Observer() | ||
handler = EventHandler(scanner) | ||
observer.schedule(handler, path, recursive=True) | ||
observer.start() | ||
|
||
while True: | ||
if any(task_list): | ||
tl = task_list | ||
task_list = [] | ||
await asyncio.gather(*tl) | ||
await event.wait() | ||
event.clear() | ||
# Should call .join() if the while stops one day. | ||
# observer.join() | ||
|
||
|
||
def async_event(f): | ||
# Log errors of f and catch them to prevent the gather to throw. | ||
f = log_errors(f) | ||
|
||
@wraps(f) | ||
def internal(*args, **kwargs): | ||
task_list.append(f(*args, **kwargs)) | ||
event.set() | ||
|
||
return internal | ||
|
||
|
||
class EventHandler(FileSystemEventHandler): | ||
def __init__(self, scanner: Scanner): | ||
self._scanner = scanner | ||
|
||
@async_event | ||
async def on_created(self, event: DirCreatedEvent | FileCreatedEvent): | ||
if event.is_directory: | ||
return | ||
await self._scanner.identify(event.src_path) | ||
|
||
# TODO: Implement the following two methods | ||
def on_moved(self, event: DirMovedEvent | FileMovedEvent): | ||
if event.is_directory: | ||
# TODO: Check if this event is also called for files in the directory or not. | ||
return | ||
print(event.src_path, event.dest_path) | ||
|
||
def on_deleted(self, event: DirDeletedEvent | FileDeletedEvent): | ||
if event.is_directory: | ||
# TODO: Check if this event is also called for files in the directory or not. | ||
return | ||
print(event.src_path) | ||
async for changes in awatch(path): | ||
for (event, file) in changes: | ||
try: | ||
if event == Change.added: | ||
await scanner.identify(file) | ||
else: | ||
print(f"Change {event} occured for file {file}") | ||
except ProviderError as e: | ||
logging.error(str(e)) | ||
except Exception as e: | ||
logging.exception("Unhandled error", exc_info=e) | ||
print("end", flush=True) |
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