Commit 35c21265 authored by JinyuanSun's avatar JinyuanSun
Browse files

update chatmol_pkg

parent 8e54d0ef
Loading
Loading
Loading
Loading
+48 −10
Original line number Original line Diff line number Diff line
import subprocess
import subprocess, threading
from xmlrpc import client
from xmlrpc import client
from .utils import ChatMol
from .utils import ChatMol


class PymolServer():
class PymolServer():
    def __init__(self, default_client:ChatMol):
    def __init__(self, default_client:ChatMol):
        self.cm = default_client
        self.cm = default_client
        self.start_pymol()
        self.pymol_console = ""


    def start_pymol(self):
    def start_pymol(self, pymol_path='pymol'):
        # os.system("nohup pymol -R /dev/null 2>&1")
        """
        subprocess.Popen(["pymol", "-R"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        Starts PyMOL using a subprocess and captures its stdout in a non-blocking way.
        
        Args:
        pymol_path (str): Path to the PyMOL executable. Default is 'pymol'.
        """
        # Start PyMOL as a subprocess
        self.pymol_process = subprocess.Popen(
            [pymol_path, "-R"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            bufsize=1,  # Line-buffered
            universal_newlines=True
        )
        self.server = client.ServerProxy(uri="http://localhost:9123/RPC2")
        self.server = client.ServerProxy(uri="http://localhost:9123/RPC2")
        return 0

        # Check if the process has started correctly
        if self.pymol_process.stdout is None:
            print("Failed to start PyMOL process.")
            return 

        # Start a thread to listen to the output
        self.stdout_thread = threading.Thread(target=self.get_stdout)
        self.stdout_thread.start()

    def get_stdout(self):
        """
        Prints the stdout of the PyMol subprocess continuously.
        """
        # This function runs in a separate thread
        if self.pymol_process is not None:
            while True:
                output = self.pymol_process.stdout.readline()
                if output == '' and self.pymol_process.poll() is not None:
                    break
                if output:
                    self.pymol_console += output.strip()
                    # print(output.strip())
            self.pymol_process.stdout.close()


    def chatlite(self, question):
    def chatlite(self, question):
        answer = self.cm.chatlite(question)
        answer = self.cm.chatlite(question)
@@ -23,7 +59,7 @@ class PymolServer():
            else:
            else:
                print(command)
                print(command)
                self.server.do(command)
                self.server.do(command)
        return commands
        return answer


    def chatgpt(self, message, execute:bool=True, lite:bool=False):
    def chatgpt(self, message, execute:bool=True, lite:bool=False):
        if lite:
        if lite:
@@ -33,7 +69,7 @@ class PymolServer():
            if len(self.cm.stashed_commands) == 0:
            if len(self.cm.stashed_commands) == 0:
                print("There is no stashed commands")
                print("There is no stashed commands")
            else:
            else:
                for command in stashed_commands:
                for command in self.cm.stashed_commands:
                    self.server.do(command)
                    self.server.do(command)
                self.cm.clear_stashed_commands()
                self.cm.clear_stashed_commands()
            return 0
            return 0
@@ -69,6 +105,7 @@ class PymolServer():
                            self.cm.stashed_commands.append(command)
                            self.cm.stashed_commands.append(command)
        except Exception as e:
        except Exception as e:
            print(f"Error during command execution: {e}")
            print(f"Error during command execution: {e}")
        return response


    def claude(self, message, execute:bool=True):
    def claude(self, message, execute:bool=True):
        message = message.strip()
        message = message.strip()
@@ -76,7 +113,7 @@ class PymolServer():
            if len(self.cm.stashed_commands) == 0:
            if len(self.cm.stashed_commands) == 0:
                print("There is no stashed commands")
                print("There is no stashed commands")
            else:
            else:
                for command in stashed_commands:
                for command in self.cm.stashed_commands:
                    self.server.do(command)
                    self.server.do(command)
                self.cm.clear_stashed_commands()
                self.cm.clear_stashed_commands()
            return 0
            return 0
@@ -112,3 +149,4 @@ class PymolServer():
                            self.cm.stashed_commands.append(command)
                            self.cm.stashed_commands.append(command)
        except Exception as e:
        except Exception as e:
            print(f"Error during command execution: {e}")
            print(f"Error during command execution: {e}")
        return response
 No newline at end of file