Skip to content

Commit

Permalink
ADD: functionality to switch file on the same component
Browse files Browse the repository at this point in the history
  • Loading branch information
9and3 committed Feb 7, 2024
1 parent e3d433b commit 95330cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
40 changes: 22 additions & 18 deletions GH/PyGH/scriptsyncGH_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,32 +200,33 @@ class FileChangedThread(GHThread):
"""
def __init__(self,
path : str,
path_lock : threading.Lock,
name : str):
name : str
):
super().__init__(name=name)
self.path = path
self.path_lock = path_lock
self.refresh_rate = 1000 # milliseconds
self._on_file_changed = threading.Event()

def run(self):
""" Run the thread. """
self.check_file_change(self.path, self.path_lock)

def check_file_change(self, path : str, path_lock : threading.Lock) -> None:
"""
Check if the file has changed on disk.
:param path: The path of the file to check.
:param path_lock: The lock for the path.
"""
with path_lock:
last_modified = os.path.getmtime(path)
while self.component_on_canvas:
System.Threading.Thread.Sleep(self.refresh_rate)
current_modified = os.path.getmtime(path)
if current_modified != last_modified:
last_modified = current_modified
self.expire_component_solution()
last_modified = os.path.getmtime(self.path)
while self.component_on_canvas and not self._on_file_changed.is_set():
System.Threading.Thread.Sleep(self.refresh_rate)
current_modified = os.path.getmtime(self.path)
if current_modified != last_modified:
last_modified = current_modified
self.expire_component_solution()
self._on_file_changed.clear()
return

def stop(self):
""" Stop the thread. """
self._on_file_changed.set()


class ScriptSyncCPy(component):
Expand All @@ -246,7 +247,6 @@ def __init__(self):

self.filechanged_thread_name : str = f"script-sync-fileChanged-thread::{ghenv.Component.InstanceGuid}"
self.__path_name_table_value = "script-sync::" + "path::" + str(ghenv.Component.InstanceGuid)
self.path_lock = threading.Lock()

def RemovedFromDocument(self, doc):
""" Remove the component from the document. """
Expand Down Expand Up @@ -395,9 +395,10 @@ def RunScript(self,
raise Exception("script-sync::File does not exist")

# file change listener thread

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()
FileChangedThread(self.path,
self.filechanged_thread_name
).start()

# set up the tcp client to connect to the vscode server
_ = [print(t.name) for t in threading.enumerate()]
Expand Down Expand Up @@ -450,6 +451,9 @@ def path(self, path : str):
script_name = os.path.basename(path)
ghenv.Component.Message = f"{script_name}"

if self.filechanged_thread_name in [t.name for t in threading.enumerate()]:
_ = [t for t in threading.enumerate() if t.name == self.filechanged_thread_name][0].stop()

@path.deleter
def path(self):
""" Delete the path of the file from the table view if the object is erased. """
Expand Down
4 changes: 2 additions & 2 deletions GH/PyGH/test/runner_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#! python3

def main():
x = 123
y = 45
x = 12344
y = 453
a = x - y
c = x + y
b = 12345
Expand Down
2 changes: 1 addition & 1 deletion GH/PyGH/test/runner_script_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#! python3

def main():
k = 1234444
k = 12333
print(f"runner_script_2.py::k value: {k}")

return a
Expand Down

0 comments on commit 95330cf

Please sign in to comment.