Skip to content

Commit

Permalink
CAP-WIP: working filepath set and persistence across sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
9and3 committed Feb 5, 2024
1 parent a36d71c commit 6670c15
Showing 1 changed file with 51 additions and 72 deletions.
123 changes: 51 additions & 72 deletions GH/PyGH/scriptsyncGH_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

# TODO: modify the error messages for client with vscode-related info
#TODO: add tooltip description to parameters in metadata
# TODO: in the tcp send to vscode, we need to include first the name of the selected file

class GHThread(threading.Thread, metaclass=abc.ABCMeta):
"""
Expand Down Expand Up @@ -238,7 +239,7 @@ class ScriptSyncCPy(component):
def __init__(self):
super(ScriptSyncCPy, self).__init__()
self._var_output = []
ghenv.Component.Message = "ScriptSyncCPy"
ghenv.Component.Message = "no-script-selected"

self.is_success = False

Expand All @@ -251,12 +252,9 @@ def __init__(self):
self.event_fire_msg = threading.Event()

self.filechanged_thread_name = None
self.path = ""
self.__path_name_table_value = "script-sync::" + "path::" + str(ghenv.Component.InstanceGuid)
self.path_lock = threading.Lock()

# TODO: test
self.path_name_table_value = None

def RemovedFromDocument(self, doc):
""" Remove the component from the document. """
if self.client_thread_name in [t.name for t in threading.enumerate()]:
Expand All @@ -272,8 +270,8 @@ def RemovedFromDocument(self, doc):
if self.event_fire_msg is not None:
self.event_fire_msg.clear()

ghenv.Component.OnPingDocument().ValueTable.DeleteValue(self.path_name_table_value) # <<<<<<<<<<<<< path usage

# clear the path from the table view
del self.path

def _add_button(self):
"""Add a button to the canvas and wire it to the "script" param."""
Expand Down Expand Up @@ -304,9 +302,6 @@ def _add_button(self):

return True

# def _on_script_selected(self):
# """ The function deals with the button to set the script path. """

def _safe_exec(self, path, globals, locals):
"""
Execute Python3 code safely. It redirects the output of the code
Expand All @@ -319,9 +314,16 @@ def _safe_exec(self, path, globals, locals):
"""
try:
with open(path, 'r') as f:
# add the path of the file to use the modules
path_dir = self.path.split("\\")
path_dir = "\\".join(path_dir[:-1])
sys.path.insert(0, path_dir)

# parse the code
code = compile(f.read(), path, 'exec')
output = io.StringIO()

# execute the code
with contextlib.redirect_stdout(output):
exec(code, globals, locals)
locals["stdout"] = output.getvalue()
Expand Down Expand Up @@ -366,81 +368,40 @@ def RunScript(self,
""" This method is called whenever the component has to be recalculated it's the solve main instance. """
self.is_success = False


# set up the tcp client to connect to the vscode server
self.client_thread_name : str = f"script-sync-client-thread::{ghenv.Component.InstanceGuid}"
_ = [print(t.name) for t in threading.enumerate()]
if self.client_thread_name not in [t.name for t in threading.enumerate()]:
ClientThread(self.vscode_server_ip,
self.vscode_server_port,
self.client_thread_name,
self.queue_msg,
self.queue_msg_lock,
self.event_fire_msg
).start()


# ======================================================================================
# path saving/setting

print(self.path)
path_name_table_value = "script-sync::" + "path::" + str(ghenv.Component.InstanceGuid) + "::" + "self.path"
ghenv.Component.OnPingDocument().ValueTable.SetValue(self.path_name_table_value, self.path_name_table_value)
# value = ghenv.Component.OnPingDocument().ValueTable.GetValue(self.path_name_table_value, "not_found")
print(value)

# remove the path from the value table
# ghenv.Component.OnPingDocument().ValueTable.DeleteValue(self.path_name_table_value)
value =ghenv.Component.OnPingDocument().ValueTable.GetValue(self.path_name_table_value, "not_found")
print(f"after delete: {value}")


# set the path
if script is True:
# use windows form to open the file
dialog = System.Windows.Forms.OpenFileDialog()
dialog.Filter = "Python files (*.py)|*.py"
dialog.Title = "Select a Python file"
dialog.InitialDirectory = os.path.dirname(self.path)
dialog.FileName = os.path.basename(self.path)
dialog.InitialDirectory = os.path.dirname("")
dialog.FileName = ""
dialog.Multiselect = False
dialog.CheckFileExists = True
dialog.CheckPathExists = True
dialog.RestoreDirectory = True
if dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK:
self.path = dialog.FileName # <<<<<<<<<<<<< path usage
else:
raise Exception("script-sync::No file selected")
# else:
# if not os.path.exists(self.path):
# raise Exception("script-sync::File not selected")

if self.path == "": # <<<<<<<<<<<<< path usage
self.path = dialog.FileName
if self.path is None:
raise Exception("script-sync::File not selected")
if not os.path.exists(self.path): # <<<<<<<<<<<<< path usage
if not os.path.exists(self.path):
raise Exception("script-sync::File does not exist")


# get the name of the file
script_name = os.path.basename(self.path) # <<<<<<<<<<<<< path usage
ghenv.Component.Message = f"{script_name}"





# ======================================================================================


# file change listener thread
self.filechanged_thread_name : str = f"script-sync-fileChanged-thread::{ghenv.Component.InstanceGuid}"
if self.filechanged_thread_name not in [t.name for t in threading.enumerate()]:
FileChangedThread(self.path, self.path_lock, self.filechanged_thread_name).start()


# add the path of the file to use the modules
path_dir = self.path.split("\\")
path_dir = "\\".join(path_dir[:-1])
sys.path.insert(0, path_dir)
# set up the tcp client to connect to the vscode server
self.client_thread_name : str = f"script-sync-client-thread::{ghenv.Component.InstanceGuid}"
_ = [print(t.name) for t in threading.enumerate()]
if self.client_thread_name not in [t.name for t in threading.enumerate()]:
ClientThread(self.vscode_server_ip,
self.vscode_server_port,
self.client_thread_name,
self.queue_msg,
self.queue_msg_lock,
self.event_fire_msg
).start()

# run the script
res = self._safe_exec(self.path, globals(), locals())
Expand All @@ -463,8 +424,26 @@ def AfterRunScript(self):
ghenv.Component.Params.Output[idx].AddVolatileData(gh.Kernel.Data.GH_Path(0), 0, self._var_output[idx])
self._var_output.clear()

@property
def path(self):
""" Get the path of the file from the table view to be sticking between the sessions. """
table_value = ghenv.Component.OnPingDocument().ValueTable.GetValue(
self.__path_name_table_value, "not_found"
)
if table_value != "not_found":
return table_value
else:
return None

@path.setter
def path(self, path : str):
""" Set the path of the file to the table view to be sticking between the sessions. """
ghenv.Component.OnPingDocument().ValueTable.SetValue(self.__path_name_table_value, path)

script_name = os.path.basename(path)
ghenv.Component.Message = f"{script_name}"

# @property
# def path(self):
# self._path = ghenv.Component.OnPingDocument().GetValue("path")
# return self._path
@path.deleter
def path(self):
""" Delete the path of the file from the table view if the object is erased. """
ghenv.Component.OnPingDocument().ValueTable.DeleteValue(self.__path_name_table_value)

0 comments on commit 6670c15

Please sign in to comment.