diff --git a/lib/DirectoryWatcher.py b/lib/DirectoryWatcher.py new file mode 100644 index 0000000..1c658dd --- /dev/null +++ b/lib/DirectoryWatcher.py @@ -0,0 +1,44 @@ +# directory_watcher.py +import threading +from watchdog.observers import Observer +from watchdog.events import FileSystemEventHandler + +class DirectoryWatcher: + def __init__(self, directory, callback): + self.directory = directory + self.callback = callback + self.is_watching = True + self.event_handler = FileSystemEventHandler() + self.event_handler.on_created = self.on_created + self.observer = Observer() + self.observer.schedule(self.event_handler, path=self.directory, recursive=False) + + def on_created(self, event): + if event.is_directory: + return + # Call your callback method here + self.callback(event.src_path) + + def start_watching(self): + # Define a thread target function + def run_observer(): + self.observer.start() + try: + while self.is_watching: + pass + except KeyboardInterrupt: + pass + finally: + self.stop_watching() + + # Create and start the thread + watcher_thread = threading.Thread(target=run_observer) + watcher_thread.start() + + # Return the thread in case you want to manage it externally + return watcher_thread + + def stop_watching(self): + self.is_watching = False + self.observer.stop() + self.observer.join() diff --git a/lib/FileWatcher.py b/lib/FileWatcher.py deleted file mode 100644 index 333fe51..0000000 --- a/lib/FileWatcher.py +++ /dev/null @@ -1,23 +0,0 @@ -import threading -import os - - -class FileWatcher: - def __init__(self, filename, interval, callback): - self._cached_stamp = os.stat(filename).st_mtime - self.filename = filename - self.interval = interval - self.callback = callback - - self.is_running = True - - def start(self): - if self.is_running: - threading.Timer(self.interval, self.start).start() - stamp = os.stat(self.filename).st_mtime - if stamp != self._cached_stamp: - self._cached_stamp = stamp - self.callback() - - def stop(self): - self.is_running = False diff --git a/main.py b/main.py index 9e4688f..b307c1a 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from lib.Exceptions.UndetectedLanguageException import ( UndetectedLanguageException, ) -from lib.FileWatcher import FileWatcher +from lib.DirectoryWatcher import DirectoryWatcher from langdetect import detect from fastapi import FastAPI, HTTPException, Request from fastapi.responses import HTMLResponse @@ -21,9 +21,17 @@ templates = Jinja2Templates(directory="public") app = FastAPI(title="API") -# @app.on_event("startup") -# async def startEvent(): -# await main() +dirWatcher = DirectoryWatcher(directory = "data_from_A", callback=lambda file_path :print("whatever" + file_path)) + + +@app.on_event("startup") +async def startEvent(): + dirWatcher.start_watching() #Starts DirectoryWatcher + +@app.on_event("shutdown") +def shutdown_event(): + print("Shutting down...") + dirWatcher.stop_watching() app.mount( "/static", @@ -78,8 +86,6 @@ async def main(): if not os.path.exists("entity_mentions.json"): open("entity_mentions.json", "w").close() - # FileWatcher(filename = "Artikel.txt", interval = 5.0, callback=lambda :print("whatever")).start() #Starts fileWatcher - text = GetSpacyData.GetText( "Artikel.txt" ) # Takes in title of article. Gets article text in string format diff --git a/requirements.txt b/requirements.txt index 0aed35e..f79b529 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ pytest-cov==3.0.0 langdetect==1.0.9 httpx fuzzywuzzy +watchdog \ No newline at end of file diff --git a/requirements_no_models.txt b/requirements_no_models.txt new file mode 100644 index 0000000..4c86b56 --- /dev/null +++ b/requirements_no_models.txt @@ -0,0 +1,14 @@ +fastapi==0.103.2 +setuptools==68.2.0 +wheel==0.41.2 +spacy>=3.7.0, <3.8.0 +uvicorn==0.23.2 +levenshtein +pytest==7.4.2 +pytest_sugar==0.9.7 +pytest-asyncio==0.21.1 +pytest-cov==3.0.0 +langdetect==1.0.9 +httpx +fuzzywuzzy +watchdog \ No newline at end of file