Skip to content

Commit

Permalink
Refactored/added some exceptions avogadro-remote.py
Browse files Browse the repository at this point in the history
Refactored the complete code into connection class and added the check for socket connection

Signed-off-by: Omar <[email protected]>
  • Loading branch information
adityaomar3 committed Sep 19, 2023
1 parent e88527f commit b4175a6
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions scripts/avogadro-remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import struct
import tempfile


class Connection:
'''Process a JSON-RPC request'''

Expand All @@ -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):
"""
Expand Down Expand Up @@ -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()

0 comments on commit b4175a6

Please sign in to comment.