From cc113eb66457f4a1788ced1ca169b58482a3119d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Tue, 19 Jul 2016 10:50:43 +0300 Subject: [PATCH] new plugin listener --- .../base/plugin/plugin_install_listener.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 opt/ahenk/base/plugin/plugin_install_listener.py diff --git a/opt/ahenk/base/plugin/plugin_install_listener.py b/opt/ahenk/base/plugin/plugin_install_listener.py new file mode 100644 index 0000000..c31f51e --- /dev/null +++ b/opt/ahenk/base/plugin/plugin_install_listener.py @@ -0,0 +1,47 @@ +import os +import signal +import time + +from watchdog.events import FileSystemEventHandler +from watchdog.observers import Observer + +from base.command.commander import Commander +from base.system.system import System + + +class FileEventHandler(FileSystemEventHandler): + def __init__(self, plugin_path): + self.path = plugin_path + + def process(self, event): + + if event.event_type == 'created': + result = Commander().set_event([None, 'load', '-p', event.src_path.replace(self.path, '')]) + if result is True: + if System.Ahenk.is_running() is True: + os.kill(int(System.Ahenk.get_pid_number()), signal.SIGALRM) + elif event.event_type == 'deleted': + # TODO + print('plugin removed') + + def on_created(self, event): + if event.is_directory: + self.process(event) + + def on_deleted(self, event): + if event.is_directory: + self.process(event) + + +class PluginInstallListener: + def listen(self, path): + observer = Observer() + event_handler = FileEventHandler(path) + observer.schedule(event_handler, path, recursive=False) + observer.start() + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + observer.stop() + observer.join()