diff options
Diffstat (limited to 'ui/src/plugins/python3')
-rwxr-xr-x | ui/src/plugins/python3/ansi-plugin.py | 48 | ||||
-rw-r--r-- | ui/src/plugins/python3/libmeliapi.py | 173 | ||||
-rwxr-xr-x | ui/src/plugins/python3/nntp-backend.py | 92 |
3 files changed, 313 insertions, 0 deletions
diff --git a/ui/src/plugins/python3/ansi-plugin.py b/ui/src/plugins/python3/ansi-plugin.py new file mode 100755 index 00000000..507cae72 --- /dev/null +++ b/ui/src/plugins/python3/ansi-plugin.py @@ -0,0 +1,48 @@ +#! /usr/bin/env python3 +""" +meli - sample plugin + +Copyright 2019 Manos Pitsidianakis + +This file is part of meli. + +meli is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +meli is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with meli. If not, see <http://www.gnu.org/licenses/>. +""" + +import sys +import subprocess +print(sys.path, file=sys.stderr) +from libmeliapi import Client + +if __name__ == "__main__": + server_address = './soworkfile' + client = Client(server_address) + client.connect() + try: + _bytes = client.read() + print('got bytes {!r}'.format(_bytes),file=sys.stderr, ) + + # run() returns a CompletedProcess object if it was successful + # errors in the created process are raised here too + process = subprocess.run(['tiv','-w', '120','-h', '40', _bytes[0]], check=True, stdout=subprocess.PIPE, universal_newlines=True) + output = process.stdout + print('tiv output len {}'.format(len(output)),file=sys.stderr, ) + #print('tiv output bytes {!r}'.format(output),file=sys.stderr, ) + + message = { "t": "ansi", "c": output } + #print('sending {!r}'.format(message),file=sys.stderr, ) + print('returned :', client.send(message), file=sys.stderr,) + except Exception as msg: + print(msg, file=sys.stderr,) + diff --git a/ui/src/plugins/python3/libmeliapi.py b/ui/src/plugins/python3/libmeliapi.py new file mode 100644 index 00000000..044b1a9a --- /dev/null +++ b/ui/src/plugins/python3/libmeliapi.py @@ -0,0 +1,173 @@ +""" +meli - python3 api plugin + +Copyright 2019 Manos Pitsidianakis + +This file is part of meli. + +meli is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +meli is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with meli. If not, see <http://www.gnu.org/licenses/>. +""" + +from collections import deque +import errno +import json +import msgpack +import socket +import struct +import sys +import time + +class IPCError(Exception): + pass + +class UnknownMessageClass(IPCError): + pass + +class InvalidSerialization(IPCError): + pass + +class ConnectionClosed(IPCError): + pass + + +def _read_objects(sock): + unpacker = msgpack.Unpacker() + ret = [] + #reader = socket.socket.makefile(sock, 'rb') + counter = 0 + while True: + print("[libmeliapi]: _read_objects loop = ", counter, flush=True, file=sys.stderr) + counter += 1 + try: + buf = sock.recv(1024**2) + if not buf: + break + unpacker.feed(buf) + for o in unpacker: + ret.append(o) + except: + break + return ret + + #try: + # for unpack in unpacker: + # return unpack + #except Exception as e: + # print("[libmeliapi]: ", "_read_objects error ", e, file=sys.stderr,) + # return None + #finally: + # reader.flush() + +def _write_objects(sock, objects): + sys.stderr.flush() + print("[libmeliapi]: ", "_write_objects ", objects, flush=True, file=sys.stderr, ) + data = msgpack.packb(objects) + #print("[libmeliapi]: ", "_write_objects data ", data, flush=True, file=sys.stderr, ) + sent = 0 + + while sent < len(data): + try: + _len = min(len(data[sent:]), 2048) + sent += sock.send(data[sent:sent+_len]) + except IOError as e: + print("[libmeliapi]: IOError: ", e, e.errno, flush=True, file=sys.stderr, ) + sys.stderr.flush() + if e.errno == errno.EWOULDBLOCK: + break + else: + raise + +class Client(object): + def __init__(self, server_address): + self.buffer = deque() + self.addr = server_address + address_family = socket.AF_UNIX + self.sock = socket.socket(address_family, socket.SOCK_STREAM) + self.sock.setblocking(0) + + def connect(self): + try: + self.sock.connect(self.addr) + + print("[libmeliapi]: ", "self.send({ \"version\": \"dev\" }) = ",self.send({ "version": "dev" }), flush=True, file=sys.stderr) + self.expect_ack() + self._session = self.read() + self.ack() + print("[libmeliapi]: ", "self.buffer =", self.buffer, flush=True, file=sys.stderr, ) + print("[libmeliapi]: ", "connected, session id is", self._session, flush=True, file=sys.stderr) + except socket.error as msg: + print("[libmeliapi]: ", msg, flush=True, file=sys.stderr, ) + sys.stderr.flush() + sys.exit(1) + + def close(self): + self.sock.close() + + def setblocking(self, new_val): + self.sock.setblocking(new_val) + + def __enter__(self): + self.connect() + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.close() + + def send(self, objects): + sys.stderr.flush() + print("[libmeliapi]: ", "stuck in send ", self.buffer, flush=True, file=sys.stderr, ) + _write_objects(self.sock, objects) + print("[libmeliapi]: ", "unstuck wrote objs", flush=True, file=sys.stderr, ) + #print("[libmeliapi]: ", "wrote object ", objects, file=sys.stderr) + time.sleep(0.1) + + def ack(self): + sys.stderr.flush() + _write_objects(self.sock, 0x06) + time.sleep(0.1) + + def expect_ack(self): + print("[libmeliapi]: expect_ack, ", self.buffer, flush=True, file=sys.stderr, ) + read_list = _read_objects(self.sock) + time.sleep(0.1) + self.buffer.extend(read_list) + if len(self.buffer) > 0 and self.buffer.popleft() == 0x6: + print("[libmeliapi]: got_ack, ", self.buffer, flush=True, file=sys.stderr, ) + return + else: + raise "ACK expected" + + def read(self): + sys.stderr.flush() + print("[libmeliapi]: ", "stuck in read ", self.buffer, flush=True, file=sys.stderr, ) + read_list = _read_objects(self.sock) + time.sleep(0.1) + self.buffer.extend(read_list) + print("[libmeliapi]: ", "unstuck read self.buffer =", self.buffer, flush=True, file=sys.stderr, ) + if len(self.buffer) > 0: + return self.buffer.popleft() + else: + return None + + @property + def backend_fn_type(self): + return 0 + + def backend_fn_ok_send(self, objects): + self.send({"t": "ok", "c": objects }) + self.expect_ack() + + def backend_fn_err_send(self, objects): + self.send({"t": "err", "c": objects }) + self.expect_ack() diff --git a/ui/src/plugins/python3/nntp-backend.py b/ui/src/plugins/python3/nntp-backend.py new file mode 100755 index 00000000..37331eef --- /dev/null +++ b/ui/src/plugins/python3/nntp-backend.py @@ -0,0 +1,92 @@ +#! /usr/bin/env python3 +""" +meli - sample plugin + +Copyright 2019 Manos Pitsidianakis + +This file is part of meli. + +meli is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +meli is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with meli. If not, see <http://www.gnu.org/licenses/>. +""" + +import sys +import time +import subprocess +import msgpack +import nntplib +import libmeliapi +import itertools + +def chunks(iterable, n): + while True: + try: + yield itertools.chain((next(iterable),), itertools.islice(iterable, n-1)) + except: + break + + +if __name__ == "__main__": + import importlib + importlib.reload(libmeliapi) + server_address = './soworkfile' + client = libmeliapi.Client(server_address) + client.connect() + #client.setblocking(True) + try: + counter = 0 + while True: + print("[nntp-plugin]: loop = ", counter, flush=True, file=sys.stderr) + counter += 1 + req = client.read() + if req is None: + time.sleep(0.15) + continue + #client.setblocking(True) + client.ack() + print("[nntp-plugin]: ", "req: ", req, flush=True, file=sys.stderr) + sys.stderr.flush() + if isinstance(req, msgpack.ExtType): + print("[nntp-plugin]: ", req, flush=True, file=sys.stderr) + if req.data == b'is_online': + client.backend_fn_ok_send(None) + elif req.data == b'get': + s = nntplib.NNTP('news.gmane.org') + resp, count, first, last, name = s.group('gmane.comp.python.committers') + print('Group', name, 'has', count, 'articles, range', first, 'to', last, flush=True, file=sys.stderr) + + resp, overviews = s.over((last - 9, last)) + ids = [] + for id, over in overviews: + ids.append(id) + print(id, nntplib.decode_header(over['subject']), flush=True, file=sys.stderr) + for chunk in chunks(iter(ids), 2): + ret = [] + for _id in chunk: + resp, info = s.article(_id) + #print(_id, " line0 = ", str(info.lines[0], 'utf-8', 'ignore')) + elem = b'\n'.join(info.lines) + ret.append(str(elem, 'utf-8', 'ignore')) + print("ret len = ", len(ret), flush=True,file=sys.stderr) + client.backend_fn_ok_send(ret) + time.sleep(0.85) + s.quit() + client.backend_fn_ok_send(None) + #client.setblocking(True) + time.sleep(0.15) + + + except Exception as msg: + print("[nntp-plugin]: ", msg, flush=True, file=sys.stderr,) + sys.stderr.flush() + |