Skip to content

Commit

Permalink
Added directory watcher
Browse files Browse the repository at this point in the history
- Now uses watchdog instead of custom solution
- Added watchdog to requirements.txt
- Added requirements_no_models.txt (faster to install packages if you already have the model installed)
- Remove old filewatcher.py
  • Loading branch information
FredTheNoob committed Nov 24, 2023
1 parent 3e9422d commit 05b8e40
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 29 deletions.
44 changes: 44 additions & 0 deletions lib/DirectoryWatcher.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 0 additions & 23 deletions lib/FileWatcher.py

This file was deleted.

18 changes: 12 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pytest-cov==3.0.0
langdetect==1.0.9
httpx
fuzzywuzzy
watchdog
14 changes: 14 additions & 0 deletions requirements_no_models.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 05b8e40

Please sign in to comment.