diff --git a/CHANGELOG.md b/CHANGELOG.md index b8a81029..61f39567 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Packaging changes +- New dependency: [watchdog](https://pypi.org/project/watchdog/) + +### Chamged +- Modules are now automatically unloaded after uninstallation + ### Fixed - AppImage trying to store data inside itself in portable mode diff --git a/pext/__main__.py b/pext/__main__.py index 3e098294..2c8f588d 100644 --- a/pext/__main__.py +++ b/pext/__main__.py @@ -62,6 +62,8 @@ from PyQt5.QtWidgets import QApplication, QAction, QMenu, QStyleFactory, QSystemTrayIcon from PyQt5.Qt import QClipboard, QIcon, QObject, QQmlApplicationEngine, QQmlComponent, QQmlContext, QQmlProperty, QUrl from PyQt5.QtGui import QPalette, QColor, QWindow +from watchdog.events import FileSystemEventHandler +from watchdog.observers import Observer if platform.system() == 'Darwin': import accessibility # NOQA @@ -314,6 +316,26 @@ def set_queue_count(count: List[int]) -> None: QQmlProperty.write(Logger.status_queue, "entriesLeftBackground", count[1]) +class PextFileSystemEventHandler(FileSystemEventHandler): + """Watches the file system to ensure state changes when relevant.""" + + def __init__(self, window: 'Window', modules_path: str): + """Initialize filesystem event handler.""" + self.window = window + self.modules_path = modules_path + + def on_deleted(self, event): + """Unload modules on deletion.""" + if not event.is_directory: + return + + if event.src_path.startswith(self.modules_path): + for tab_id, tab in enumerate(self.window.tab_bindings): + if event.src_path == os.path.join(self.modules_path, tab['metadata']['id'].replace('.', '_')): + print("Module {} was removed, sending unload event".format(tab['metadata']['id'])) + self.window.module_manager.unload_module(self.window, tab_id) + + class MainLoop(): """Main application loop. @@ -3632,6 +3654,12 @@ def main() -> None: # Give the window a reference to the tray window.bind_tray(tray) + # Start watching for uninstalls + event_handler = PextFileSystemEventHandler(window, os.path.join(ConfigRetriever.get_path(), 'modules')) + observer = Observer() + observer.schedule(event_handler, os.path.join(ConfigRetriever.get_path(), 'modules'), recursive=True) + observer.start() + # And run... main_loop.run() diff --git a/requirements.txt b/requirements.txt index be714f2f..5ff1ca3f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ dulwich==0.19.9 pynput==1.4 pyqt5==5.11.3 requests==2.21.0 +watchdog==0.9.0 accessibility==0.4.0; sys.platform == 'darwin' PyOpenGL==3.1.0; sys.platform == 'linux' PyOpenGL_accelerate==3.1.0; sys.platform == 'linux'