2021-11-22 10:14:38 +00:00
|
|
|
from classes.config import MonsterConfig
|
|
|
|
from classes.doghandler import DogHandler
|
|
|
|
|
|
|
|
from watchdog.observers import Observer
|
|
|
|
|
|
|
|
from multiprocessing import Process, Queue
|
2021-11-25 15:31:49 +00:00
|
|
|
from typing import NoReturn
|
2021-11-22 10:14:38 +00:00
|
|
|
|
|
|
|
import time
|
2021-11-25 09:40:25 +00:00
|
|
|
import os.path
|
|
|
|
|
2021-11-22 10:14:38 +00:00
|
|
|
|
2021-11-25 15:31:49 +00:00
|
|
|
class ShoreThread(Process):
|
|
|
|
"""Thread handling the discovery of shore-side file changes
|
|
|
|
"""
|
|
|
|
def __init__(self, state: dict) -> None:
|
|
|
|
"""Create a new ShoreThread object
|
|
|
|
|
|
|
|
Args:
|
|
|
|
state (dict): Dictionary containing the application state
|
|
|
|
"""
|
2021-11-22 10:14:38 +00:00
|
|
|
super().__init__()
|
|
|
|
self._dogs = []
|
2021-11-25 15:31:49 +00:00
|
|
|
self._state = state
|
2021-11-22 10:14:38 +00:00
|
|
|
self.queue = Queue()
|
|
|
|
|
2021-11-25 15:31:49 +00:00
|
|
|
def getAllFiles(self) -> list:
|
|
|
|
"""Return File objects for all files in all Directories
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: List of all File objects discovered
|
|
|
|
"""
|
2021-11-22 10:14:38 +00:00
|
|
|
files = []
|
|
|
|
|
2021-11-25 15:31:49 +00:00
|
|
|
for directory in self._state["config"].directories:
|
|
|
|
for f in directory.getFiles():
|
|
|
|
files.append(f)
|
2021-11-22 10:14:38 +00:00
|
|
|
|
|
|
|
return files
|
|
|
|
|
2021-11-25 15:31:49 +00:00
|
|
|
def clearFiles(self) -> None:
|
|
|
|
"""Clear the files variable in the application state
|
|
|
|
"""
|
|
|
|
del self._state["files"][:]
|
2021-11-22 10:14:38 +00:00
|
|
|
|
2021-11-25 15:31:49 +00:00
|
|
|
def monitor(self) -> None:
|
|
|
|
"""Initialize monitoring of Directories specified in configuration
|
|
|
|
"""
|
|
|
|
for directory in self._state["config"].directories:
|
2021-11-23 07:30:31 +00:00
|
|
|
print("Creating dog for " + str(directory.location))
|
2021-11-25 09:40:25 +00:00
|
|
|
handler = DogHandler(directory, self.queue)
|
2021-11-23 07:30:31 +00:00
|
|
|
dog = Observer()
|
2021-11-25 09:40:25 +00:00
|
|
|
dog.schedule(handler, str(directory.location), False)
|
2021-11-23 07:30:31 +00:00
|
|
|
dog.start()
|
2021-11-22 10:14:38 +00:00
|
|
|
self._dogs.append(dog)
|
|
|
|
|
2021-11-25 15:31:49 +00:00
|
|
|
def run(self) -> NoReturn:
|
|
|
|
"""Launch the ShoreThread and start monitoring for file changes
|
|
|
|
"""
|
2021-11-22 10:14:38 +00:00
|
|
|
print("Launched Shore Thread")
|
2021-11-25 09:40:25 +00:00
|
|
|
self.getAllFiles()
|
2021-11-23 07:30:31 +00:00
|
|
|
self.monitor()
|
2021-11-25 15:31:49 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
self.joinDogs()
|
|
|
|
self.processQueue()
|
|
|
|
|
|
|
|
def joinDogs(self) -> None:
|
|
|
|
"""Join observers to receive updates on the queue
|
|
|
|
"""
|
2021-11-25 09:40:25 +00:00
|
|
|
for dog in self._dogs:
|
|
|
|
dog.join(1)
|
|
|
|
|
2021-11-25 15:31:49 +00:00
|
|
|
def processQueue(self) -> None:
|
|
|
|
"""Handle events currently on the queue
|
|
|
|
"""
|
|
|
|
event = self.queue.get() # Will block until an event is found
|
2021-11-25 09:40:25 +00:00
|
|
|
print(event)
|
2021-11-23 07:30:31 +00:00
|
|
|
|
2021-11-25 15:31:49 +00:00
|
|
|
def terminate(self, *args, **kwargs) -> None:
|
|
|
|
"""Terminate observer threads, then terminate self
|
|
|
|
"""
|
2021-11-23 07:30:31 +00:00
|
|
|
for dog in self._dogs:
|
2021-11-25 15:31:49 +00:00
|
|
|
dog.terminate()
|
2021-11-25 09:40:25 +00:00
|
|
|
dog.join()
|
2021-11-25 15:31:49 +00:00
|
|
|
|
|
|
|
super().terminate(*args, **kwargs)
|