From e3b933445fe4b18af6ec462fc40da5f482e447a0 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 23 Sep 2011 17:43:01 +0200 Subject: [teisenbe] first attempt at a plugin system. --- plugins/screen_detach.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 plugins/screen_detach.py (limited to 'plugins/screen_detach.py') diff --git a/plugins/screen_detach.py b/plugins/screen_detach.py new file mode 100644 index 00000000..6ebc77f2 --- /dev/null +++ b/plugins/screen_detach.py @@ -0,0 +1,45 @@ +import os +import stat +import pyinotify + +SCREEN_DIR = '/var/run/screen/S-%s' % (os.getlogin(),) + +class Plugin(BasePlugin): + def init(self): + self.timed_event = None + sock_path = None + self.thread = None + for f in os.listdir(SCREEN_DIR): + path = os.path.join(SCREEN_DIR, f) + if screen_attached(path): + sock_path = path + self.attached = True + break + + # Only actually do something if we found an attached screen (assuming only one) + if sock_path: + wm = pyinotify.WatchManager() + wm.add_watch(sock_path, pyinotify.EventsCodes.ALL_FLAGS['IN_ATTRIB']) + self.thread = pyinotify.ThreadedNotifier(wm, default_proc_fun=HandleScreen(plugin=self)) + self.thread.start() + + def cleanup(self): + if self.thread: + self.thread.stop() + + def update_screen_state(self, socket): + attached = screen_attached(socket) + if attached != self.attached: + self.attached = attached + status = 'available' if self.attached else 'away' + self.core.command_status(status) + +def screen_attached(socket): + return (os.stat(socket).st_mode & stat.S_IXUSR) != 0 + +class HandleScreen(pyinotify.ProcessEvent): + def my_init(self, **kwargs): + self.plugin = kwargs['plugin'] + + def process_IN_ATTRIB(self, event): + self.plugin.update_screen_state(event.path) -- cgit v1.2.3