diff --git a/python/scriptsmenu/action.py b/python/scriptsmenu/action.py index 2de744b..9ee5734 100644 --- a/python/scriptsmenu/action.py +++ b/python/scriptsmenu/action.py @@ -16,6 +16,14 @@ def __init__(self, parent=None): self._iconfile = None self._label = None + self._COMMAND = """ +import imp + +f, filepath, descr = imp.find_module("{module_name}", ["{dirname}"]) +module = imp.load_module("{module_name}", f, filepath, descr) +module.main() +""" + @property def root(self): return self._root @@ -143,11 +151,13 @@ def process_command(self): if self._sourcetype == "file": if os.path.isabs(self._command): - string = os.path.normpath(self._command) + filepath = self._command else: - string = os.path.normpath(os.path.expandvars(self._command)) + filepath = os.path.normpath(os.path.expandvars(self._command)) - return 'execfile("{}")'.format(string) + command = self._wrap_filepath(filepath) + + return command def has_tag(self, tag): """Check whether the tag matches with the action's tags. @@ -169,3 +179,15 @@ def has_tag(self, tag): return True return False + + def _wrap_filepath(self, filepath): + """ + Create a wrapped string for the python command + :param filepath: + :return: + """ + + dirname = os.path.dirname(r"{}".format(filepath)) + module_name = os.path.splitext(os.path.basename(filepath))[0] + + return self._COMMAND.format(module_name=module_name, dirname=dirname)