Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty plugin folder #408

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions core/cat/mad_hatter/mad_hatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ def load_plugin(self, plugin_path, active):
# Instantiate plugin.
# If the plugin is inactive, only manifest will be loaded
# If active, also settings, tools and hooks
plugin = Plugin(plugin_path, active=active)

# if plugin is valid, keep a reference
self.plugins[plugin.id] = plugin
try:
plugin = Plugin(plugin_path, active=active)
# if plugin is valid, keep a reference
self.plugins[plugin.id] = plugin
except Exception as e:
log(e, "WARNING")

# Load hooks and tools of the active plugins into MadHatter
def sync_hooks_and_tools(self):
Expand Down
14 changes: 8 additions & 6 deletions core/cat/mad_hatter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def __init__(self, plugin_path: str, active: bool):
# where the plugin is on disk
self._path: str = plugin_path

# search for .py files in folder
py_files_path = os.path.join(self._path, "**/*.py")
self.py_files = glob.glob(py_files_path, recursive=True)

if len(self.py_files) == 0:
raise Exception(f"{plugin_path} does not contain any python files. Cannot create Plugin.")

# plugin id is just the folder name
self._id: str = os.path.basename(os.path.normpath(plugin_path))

Expand Down Expand Up @@ -150,15 +157,10 @@ def _load_manifest(self):

# lists of hooks and tools
def _load_hooks_and_tools(self):

# search for .py files in folder
py_files_path = os.path.join(self._path, "**/*.py")
py_files = glob.glob(py_files_path, recursive=True)

hooks = []
tools = []

for py_file in py_files:
for py_file in self.py_files:
py_filename = py_file.replace("/", ".").replace(".py", "") # this is UGLY I know. I'm sorry

# save a reference to decorated functions
Expand Down
1 change: 1 addition & 0 deletions core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def clean_up_mocks():
"tests/mocks/metadata-test.json",
"tests/mocks/mock_plugin.zip",
"tests/mocks/mock_plugin_folder/mock_plugin",
"tests/mocks/empty_folder"
]
for tbr in to_be_removed:
if os.path.exists(tbr):
Expand Down
11 changes: 11 additions & 0 deletions core/tests/mad_hatter/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def test_create_plugin_wrong_folder():

assert f"Cannot create" in str(e.value)

def test_create_plugin_empty_folder():

path = "tests/mocks/empty_folder"

os.mkdir(path)

with pytest.raises(Exception) as e:
Plugin(path, active=True)

assert f"Cannot create" in str(e.value)


def test_create_non_active_plugin():

Expand Down
Loading