Skip to content

Commit

Permalink
FIX: modules can be refreshed and imported now
Browse files Browse the repository at this point in the history
  • Loading branch information
9and3 committed Feb 13, 2024
1 parent 4fa9d32 commit 4adf78f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions GH/PyGH/components/scriptsynccpy/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import queue
import json

import importlib
import sys


class GHThread(threading.Thread, metaclass=abc.ABCMeta):
"""
Expand Down Expand Up @@ -275,6 +278,13 @@ def init_script_path(self, btn : bool = False):
if not os.path.exists(self.path):
raise Exception("script-sync::File does not exist")

def reload_all_modules(directory):
for filename in os.listdir(directory):
if filename.endswith('.py') and filename != '__init__.py':
module_name = filename[:-3] # remove '.py' from filename
if module_name in sys.modules:
importlib.reload(sys.modules[module_name])

def safe_exec(self, path, globals, locals):
"""
Execute Python3 code safely. It redirects the output of the code
Expand All @@ -290,6 +300,9 @@ def safe_exec(self, path, globals, locals):
# add the path of the file to use the modules
path_dir = os.path.dirname(path)
sys.path.insert(0, path_dir)
reload_all_modules(path_dir)

self.reload_all_modules(path_dir)

# parse the code
code = compile(f.read(), path, 'exec')
Expand Down

0 comments on commit 4adf78f

Please sign in to comment.