From e6efe751115b47eb1939a9dfd46b08c80f64af83 Mon Sep 17 00:00:00 2001 From: Aditya <106031961+adityaomar3@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:57:08 +0530 Subject: [PATCH 1/6] Refactored/added some exceptions avogadro-remote.py Refactored the complete code into connection class and added the check for socket connection Signed-off-by: Omar Signed-off-by: aditya Signed-off-by: Omar --- scripts/avogadro-remote.py | 56 ++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/scripts/avogadro-remote.py b/scripts/avogadro-remote.py index 711c950a..fb078825 100755 --- a/scripts/avogadro-remote.py +++ b/scripts/avogadro-remote.py @@ -8,7 +8,6 @@ import struct import tempfile - class Connection: '''Process a JSON-RPC request''' @@ -22,7 +21,13 @@ def __init__(self, name="avogadro"): self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) # connect - self.sock.connect(tempfile.gettempdir() + "/" + name) + try: + self.sock.connect(tempfile.gettempdir() + "/" + name) + # print the connection statement + print("reply:" + str(self.receive_message())) + except Exception as exception: + print("error while connecting: " + str(exception)) + sys.exit(1) def send_json(self, obj): """ @@ -62,43 +67,34 @@ def recv_json(self): except Exception as exception: print("error: " + str(exception)) return {} - - 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( + + def open_file(self,file): + self.send_json( { "jsonrpc": "2.0", "id": 0, "method": "openFile", - "params": {"fileName": str(sys.argv[2])}, + "params": {"fileName": file}, } ) - elif method == "saveGraphic": - conn.send_json( + def save_graphic(self,file): + self.send_json( { "jsonrpc": "2.0", "id": 0, "method": "saveGraphic", - "params": {"fileName": str(sys.argv[2])}, + "params": {"fileName": file}, } ) - - 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() + def kill(self): + self.send_json( + { + "jsonrpc": "2.0", + "id": 0, + "method": "kill" + } + ) + + def close(self): + '''Close the socket to the named pipe''' + self.sock.close() From 69ac1c10da5cf54340528e284390456b8bee3804 Mon Sep 17 00:00:00 2001 From: Omar Date: Fri, 22 Sep 2023 20:00:25 +0530 Subject: [PATCH 2/6] Some minute syntax changes and added doc strings Signed-off-by: Omar Signed-off-by: aditya Signed-off-by: Omar --- scripts/avogadro-remote.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/scripts/avogadro-remote.py b/scripts/avogadro-remote.py index fb078825..a2af3330 100755 --- a/scripts/avogadro-remote.py +++ b/scripts/avogadro-remote.py @@ -66,9 +66,10 @@ def recv_json(self): return json.loads(msg) except Exception as exception: print("error: " + str(exception)) - return {} - - def open_file(self,file): + return {} + + def open_file(self, file): + """Opens file""" self.send_json( { "jsonrpc": "2.0", @@ -77,7 +78,9 @@ def open_file(self,file): "params": {"fileName": file}, } ) - def save_graphic(self,file): + + def save_graphic(self, file): + """Save Graphic""" self.send_json( { "jsonrpc": "2.0", @@ -86,15 +89,17 @@ def save_graphic(self,file): "params": {"fileName": file}, } ) + def kill(self): + """To kill the current operation""" self.send_json( { - "jsonrpc": "2.0", - "id": 0, - "method": "kill" + "jsonrpc": "2.0", + "id": 0, + "method": "kill" } - ) - + ) + def close(self): '''Close the socket to the named pipe''' - self.sock.close() + self.sock.close() \ No newline at end of file From 780e3bf4543354a72e7fa576d82b1b71befdfd25 Mon Sep 17 00:00:00 2001 From: Omar Date: Sun, 1 Oct 2023 00:08:10 +0530 Subject: [PATCH 3/6] refactored the json send recv msg methods to one private method Signed-off-by: Omar Signed-off-by: aditya Signed-off-by: Omar --- scripts/avogadro-remote.py | 94 ++++++++++++++------------------------ 1 file changed, 35 insertions(+), 59 deletions(-) diff --git a/scripts/avogadro-remote.py b/scripts/avogadro-remote.py index a2af3330..9fafbb5c 100755 --- a/scripts/avogadro-remote.py +++ b/scripts/avogadro-remote.py @@ -24,81 +24,57 @@ def __init__(self, name="avogadro"): try: self.sock.connect(tempfile.gettempdir() + "/" + name) # print the connection statement - print("reply:" + str(self.receive_message())) + print("reply:" + str(self.__json("recv_msg",None))) except Exception as exception: print("error while connecting: " + str(exception)) sys.exit(1) - def send_json(self, obj): + 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) + return packet[4:] + # try: + # return json.loads(msg_packet) + # except Exception as exception: + # print("error: " + str(exception)) + # return {} + 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""" - self.send_json( - { - "jsonrpc": "2.0", - "id": 0, - "method": "openFile", - "params": {"fileName": file}, - } - ) - + # param: file is filename input by the user in string + method = "openFile" + self.__json(method,file) + def save_graphic(self, file): """Save Graphic""" - self.send_json( - { - "jsonrpc": "2.0", - "id": 0, - "method": "saveGraphic", - "params": {"fileName": file}, - } - ) + method = "saveGraphic" + self.__json(method,file) def kill(self): """To kill the current operation""" - self.send_json( - { - "jsonrpc": "2.0", - "id": 0, - "method": "kill" - } - ) + method = "kill" + self.__json(method,None) def close(self): '''Close the socket to the named pipe''' From 686d94e687e234e266f3e5e0d1fdfbbd8d9da889 Mon Sep 17 00:00:00 2001 From: aditya Date: Tue, 3 Oct 2023 21:07:42 +0530 Subject: [PATCH 4/6] Future class not needed in python3, revc_msg bug fix, The file is now completely functional Signed-off-by: aditya Signed-off-by: Omar --- scripts/avogadro-remote.py | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/scripts/avogadro-remote.py b/scripts/avogadro-remote.py index 9fafbb5c..b19a6f97 100755 --- a/scripts/avogadro-remote.py +++ b/scripts/avogadro-remote.py @@ -1,16 +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 @@ -19,16 +14,14 @@ def __init__(self, name="avogadro"): """ # create socket self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - # connect try: self.sock.connect(tempfile.gettempdir() + "/" + name) # print the connection statement - print("reply:" + str(self.__json("recv_msg",None))) + 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. @@ -38,15 +31,9 @@ def __json(self, method, file): """ if method == "recv_msg": - size = 1024 packet = self.sock.recv(size) - return packet[4:] - # try: - # return json.loads(msg_packet) - # except Exception as exception: - # print("error: " + str(exception)) - # return {} + print("reply:" + str(packet[4: ])) else: msg = { "jsonrpc": "2.0", @@ -58,24 +45,24 @@ def __json(self, method, file): size = len(json_msg) header = struct.pack(">I", size) packet = header + json_msg.encode("ascii") - self.sock.send(packet) - + 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(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() \ No newline at end of file + self.sock.close() + print("CONNECTION CLOSED SUCCESSFULLY") \ No newline at end of file From c48eeedb9545fd1b90bafe9e198471b688c964ed Mon Sep 17 00:00:00 2001 From: aditya Date: Tue, 3 Oct 2023 21:31:29 +0530 Subject: [PATCH 5/6] removed trailing spaces Signed-off-by: aditya Signed-off-by: Omar --- scripts/avogadro-remote.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/avogadro-remote.py b/scripts/avogadro-remote.py index b19a6f97..9f7c2f36 100755 --- a/scripts/avogadro-remote.py +++ b/scripts/avogadro-remote.py @@ -34,7 +34,7 @@ def __json(self, method, file): size = 1024 packet = self.sock.recv(size) print("reply:" + str(packet[4: ])) - else: + else: msg = { "jsonrpc": "2.0", "id": 0, @@ -45,13 +45,13 @@ def __json(self, method, file): size = len(json_msg) header = struct.pack(">I", size) packet = header + json_msg.encode("ascii") - self.sock.send(packet) + 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) + self.__json(method,file) + self.__json("recv_msg",None) def save_graphic(self, file): """Save Graphic""" method = "saveGraphic" @@ -61,7 +61,7 @@ def kill(self): """To kill the current operation""" method = "kill" self.__json(method,None) - self.__json("recv_msg",None) + self.__json("recv_msg",None) def close(self): '''Close the socket to the named pipe''' self.sock.close() From 875ae9b178625da3af40b394738143432c209b95 Mon Sep 17 00:00:00 2001 From: Omar Date: Tue, 24 Oct 2023 15:32:13 +0530 Subject: [PATCH 6/6] resolved warnings by codacy Signed-off-by: Omar --- scripts/avogadro-remote.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/scripts/avogadro-remote.py b/scripts/avogadro-remote.py index 9f7c2f36..91a82c7b 100755 --- a/scripts/avogadro-remote.py +++ b/scripts/avogadro-remote.py @@ -36,10 +36,10 @@ def __json(self, method, file): print("reply:" + str(packet[4: ])) else: msg = { - "jsonrpc": "2.0", - "id": 0, - "method": method, - "params": {"fileName": file}, + "jsonrpc": "2.0", + "id": 0, + "method": method, + "params": {"fileName": file}, } json_msg = json.dumps(msg) size = len(json_msg) @@ -50,19 +50,19 @@ 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) + 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) + 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) + self.__json(method, None) + self.__json("recv_msg", None) def close(self): '''Close the socket to the named pipe''' self.sock.close() - print("CONNECTION CLOSED SUCCESSFULLY") \ No newline at end of file + print("CONNECTION CLOSED SUCCESSFULLY")