From ea4e77ce08ac3bf34e9c779c9ca1f6cb5017ccbd Mon Sep 17 00:00:00 2001 From: Aditya <106031961+adityaomar3@users.noreply.github.com> Date: Thu, 26 Oct 2023 01:48:43 +0530 Subject: [PATCH] Refactored code and added some exceptions avogadro-remote.py (#406) * Refactored/added some exceptions avogadro-remote.py Refactored the complete code into connection class and added the check for socket connection Future class not needed in python3, revc_msg bug fix, The file is now completely functional resolved warnings by codacy Signed-off-by: Omar --------- Signed-off-by: Omar Signed-off-by: aditya --- scripts/avogadro-remote.py | 122 +++++++++++++------------------------ 1 file changed, 43 insertions(+), 79 deletions(-) diff --git a/scripts/avogadro-remote.py b/scripts/avogadro-remote.py index 711c950a..91a82c7b 100755 --- a/scripts/avogadro-remote.py +++ b/scripts/avogadro-remote.py @@ -1,17 +1,11 @@ #!/usr/bin/python - -from __future__ import print_function - import sys import json import socket import struct import tempfile - - class Connection: '''Process a JSON-RPC request''' - def __init__(self, name="avogadro"): """ Connect to the local named pipe @@ -20,85 +14,55 @@ def __init__(self, name="avogadro"): """ # create socket self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - # connect - self.sock.connect(tempfile.gettempdir() + "/" + name) - - def send_json(self, obj): + try: + self.sock.connect(tempfile.gettempdir() + "/" + name) + # print the connection statement + print("CONNECTION ESTABLISHED SUCCESSFULLY") + except Exception as exception: + print("error while connecting: " + str(exception)) + sys.exit(1) + def __json(self, method, file): """ Send a JSON-RPC request to the named pipe. - - :param obj: The JSON-RPC request object. - """ - self.send_message(json.dumps(obj)) - - def send_message(self, msg): - """ + :param method: The JSON-RPC request method. Send a message to the named pipe + :param file: file corresponding to method. - :param msg: The message to send. """ - size = len(msg) - header = struct.pack(">I", size) - packet = header + msg.encode("ascii") - self.sock.send(packet) - - def receive_message(self, size=1024): - """ - Receive a message from the named pipe. - - :param size: The maximum size of the message to receive. - """ - packet = self.sock.recv(size) - - return packet[4:] - - def recv_json(self): - '''Receive a JSON-RPC response''' - msg = self.recv_message() - - try: - return json.loads(msg) - except Exception as exception: - print("error: " + str(exception)) - return {} - + if method == "recv_msg": + size = 1024 + packet = self.sock.recv(size) + print("reply:" + str(packet[4: ])) + else: + msg = { + "jsonrpc": "2.0", + "id": 0, + "method": method, + "params": {"fileName": file}, + } + json_msg = json.dumps(msg) + size = len(json_msg) + header = struct.pack(">I", size) + packet = header + json_msg.encode("ascii") + self.sock.send(packet) + def open_file(self, file): + """Opens file""" + # param: file is filename input by the user in string + method = "openFile" + self.__json(method, file) + self.__json("recv_msg", None) + def save_graphic(self, file): + """Save Graphic""" + method = "saveGraphic" + self.__json(method, file) + self.__json("recv_msg", None) + def kill(self): + """To kill the current operation""" + method = "kill" + self.__json(method, None) + self.__json("recv_msg", None) def close(self): '''Close the socket to the named pipe''' self.sock.close() - - -if __name__ == "__main__": - conn = Connection() - - method = sys.argv[1] - - if method == "openFile": - conn.send_json( - { - "jsonrpc": "2.0", - "id": 0, - "method": "openFile", - "params": {"fileName": str(sys.argv[2])}, - } - ) - elif method == "saveGraphic": - conn.send_json( - { - "jsonrpc": "2.0", - "id": 0, - "method": "saveGraphic", - "params": {"fileName": str(sys.argv[2])}, - } - ) - - elif method == "kill": - conn.send_json({"jsonrpc": "2.0", "id": 0, "method": "kill"}) - - else: - print("unknown method: " + method) - conn.close() - sys.exit(-1) - - print("reply: " + str(conn.receive_message())) - conn.close() + print("CONNECTION CLOSED SUCCESSFULLY")