Skip to content

Commit

Permalink
Automatically unload modules on uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastProject committed Jan 6, 2019
1 parent 5591cc6 commit 2a543d5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions pext/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 2a543d5

Please sign in to comment.