diff options
author | Florent Le Coz <louiz@louiz.org> | 2014-08-01 17:56:43 +0200 |
---|---|---|
committer | Florent Le Coz <louiz@louiz.org> | 2014-08-01 17:56:43 +0200 |
commit | 0e2bba640e1ffcfaa7ce4bc6782f24b0f10b8c08 (patch) | |
tree | ae025658207884b8a6d985e6c296ea31ae534208 /plugins | |
parent | 30b31b32152d0f9ee4356016a88f75e64aabb632 (diff) | |
download | poezio-0e2bba640e1ffcfaa7ce4bc6782f24b0f10b8c08.tar.gz poezio-0e2bba640e1ffcfaa7ce4bc6782f24b0f10b8c08.tar.bz2 poezio-0e2bba640e1ffcfaa7ce4bc6782f24b0f10b8c08.tar.xz poezio-0e2bba640e1ffcfaa7ce4bc6782f24b0f10b8c08.zip |
Update the pipe_cmd plugin to use asyncio and be awesome
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/pipe_cmd.py | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/plugins/pipe_cmd.py b/plugins/pipe_cmd.py index f554a71d..762501ae 100644 --- a/plugins/pipe_cmd.py +++ b/plugins/pipe_cmd.py @@ -6,10 +6,10 @@ This plugins allows commands to be sent to poezio via a named pipe. from plugin import BasePlugin -import threading import os import stat import logging +import asyncio log = logging.getLogger(__name__) @@ -25,19 +25,30 @@ class Plugin(BasePlugin): os.mkfifo(self.pipename) if not stat.S_ISFIFO(os.stat(self.pipename).st_mode): - log.error("File %s is not a fifo file" % self.pipename) - raise TypeError - - thread = threading.Thread(target=self.main_loop) - thread.setDaemon(True) - thread.start() - - def main_loop(self): - while not self.stop: - fd = open(self.pipename, 'r') - line = fd.read().strip() - self.api.run_command(line) - fd.close() + raise TypeError("File %s is not a fifo file" % self.pipename) + + self.fd = os.open(self.pipename, os.O_RDONLY|os.O_NONBLOCK) + + self.data = b"" + asyncio.get_event_loop().add_reader(self.fd, self.read_from_fifo) + + def read_from_fifo(self): + data = os.read(self.fd, 512) + if not data: + # EOF, close the fifo. And reopen it + asyncio.get_event_loop().remove_reader(self.fd) + os.close(self.fd) + self.fd = os.open(self.pipename, os.O_RDONLY|os.O_NONBLOCK) + asyncio.get_event_loop().add_reader(self.fd, self.read_from_fifo) + self.data = b'' + else: + self.data += data + l = self.data.split(b'\n', 1) + if len(l) == 2: + line, self.data = l + log.debug("run: %s" % (line.decode().strip())) + self.api.run_command(line.decode().strip()) def cleanup(self): - self.stop = True + asyncio.get_event_loop().remove_reader(self.fd) + os.close(self.fd) |