Skip to content

Commit

Permalink
updated action class, command is now wrapped in an imp.find_module
Browse files Browse the repository at this point in the history
  • Loading branch information
aardschok committed Aug 10, 2017
1 parent c35d749 commit 196d88e
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions python/scriptsmenu/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)

0 comments on commit 196d88e

Please sign in to comment.