Skip to content

Commit 3f7b942

Browse files
Refactor plugins_model
In an attempt to write unit tests for the plugins_model, I found the function load_plugins too large to effectively test. So, I tried to break it up into smaller methods, which could then be tested independently. Also found duplicated code in the plugins_controller, so I created a method of that section of code, and placed it in config.py. Both the plugins model and the plugins controller now load this function... Will test before moving on to write unit tests...
1 parent 14af024 commit 3f7b942

File tree

3 files changed

+196
-156
lines changed

3 files changed

+196
-156
lines changed

src/navigate/config/config.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright (c) 2021-2024 The University of Texas Southwestern Medical Center.
22
# All rights reserved.
3-
43
# Redistribution and use in source and binary forms, with or without
54
# modification, are permitted for academic and research use only
65
# (subject to the limitations in the disclaimer below)
@@ -39,12 +38,14 @@
3938
from pathlib import Path
4039
from os.path import isfile
4140
from multiprocessing.managers import ListProxy, DictProxy
41+
import inspect
4242

4343
# Third Party Imports
4444
import yaml
4545

4646
# Local Imports
47-
from navigate.tools.common_functions import build_ref_name
47+
from navigate.tools.common_functions import build_ref_name, load_module_from_file
48+
from navigate.model.features import feature_related_functions
4849

4950

5051
def get_navigate_path():
@@ -1101,3 +1102,23 @@ def verify_configuration(manager, configuration):
11011102
"gui",
11021103
{"channels": {"count": channel_count}},
11031104
)
1105+
1106+
1107+
def set_feature_attributes(plugin_path):
1108+
"""Load feature module and set function attributes.
1109+
1110+
Parameters
1111+
----------
1112+
plugin_path : str
1113+
The path to the plugins folder.
1114+
"""
1115+
features_dir = os.path.join(plugin_path, "model", "features")
1116+
if os.path.exists(features_dir):
1117+
features = os.listdir(features_dir)
1118+
for feature in features:
1119+
feature_file = os.path.join(features_dir, feature)
1120+
if os.path.isfile(feature_file):
1121+
temp = load_module_from_file(feature, feature_file)
1122+
for c in dir(temp):
1123+
if inspect.isclass(getattr(temp, c)):
1124+
setattr(feature_related_functions, c, getattr(temp, c))

src/navigate/controller/sub_controllers/plugins_controller.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021-2022 The University of Texas Southwestern Medical Center.
1+
# Copyright (c) 2021-2024 The University of Texas Southwestern Medical Center.
22
# All rights reserved.
33

44
# Redistribution and use in source and binary forms, with or without
@@ -33,7 +33,6 @@
3333
# Standard library imports
3434
from pathlib import Path
3535
import os
36-
import inspect
3736
import tkinter as tk
3837
from tkinter import messagebox
3938

@@ -45,9 +44,9 @@
4544
from navigate.tools.file_functions import load_yaml_file, save_yaml_file
4645
from navigate.tools.common_functions import combine_funcs, load_module_from_file
4746
from navigate.tools.decorators import AcquisitionMode
48-
from navigate.model.features import feature_related_functions
4947
from navigate.controller.sub_controllers.gui_controller import GUIController
5048
from navigate.view.popups.plugins_popup import PluginsPopup
49+
from navigate.config import set_feature_attributes
5150

5251

5352
class PluginsController:
@@ -137,8 +136,9 @@ def load_plugins(self):
137136
)
138137
if plugin_frame_module is None:
139138
print(
140-
f"Make sure that the plugin frame name {plugin_class_name} is correct! "
141-
f"Plugin {plugin_name} needs to be uninstalled from navigate or reinstalled!"
139+
f"Make sure that the plugin frame name {plugin_class_name}"
140+
f" is correct! Plugin {plugin_name} needs to be "
141+
f"uninstalled from navigate or reinstalled!"
142142
)
143143
continue
144144
plugin_frame = getattr(
@@ -149,8 +149,9 @@ def load_plugins(self):
149149
)
150150
if plugin_controller_module is None:
151151
print(
152-
f"Make sure that the plugin controller {plugin_class_name} is correct! "
153-
f"Plugin {plugin_name} needs to be uninstalled from navigate or reinstalled!"
152+
f"Make sure that the plugin controller "
153+
f"{plugin_class_name} is correct! Plugin {plugin_name} "
154+
f"needs to be uninstalled from navigate or reinstalled!"
154155
)
155156
plugin_controller = getattr(
156157
plugin_controller_module, f"{plugin_class_name}Controller"
@@ -169,18 +170,7 @@ def load_plugins(self):
169170
plugin_name, plugin_frame, plugin_controller
170171
)
171172
# feature
172-
features_dir = os.path.join(plugin_path, "model", "features")
173-
if os.path.exists(features_dir):
174-
features = os.listdir(features_dir)
175-
for feature in features:
176-
feature_file = os.path.join(features_dir, feature)
177-
if os.path.isfile(feature_file):
178-
module = load_module_from_file(feature, feature_file)
179-
for c in dir(module):
180-
if inspect.isclass(getattr(module, c)):
181-
setattr(
182-
feature_related_functions, c, getattr(module, c)
183-
)
173+
set_feature_attributes(plugin_path)
184174

185175
# acquisition mode
186176
acquisition_modes = plugin_config.get("acquisition_modes", [])
@@ -223,13 +213,13 @@ def build_tab_window(self, plugin_name, frame, controller):
223213
"__plugin" + "_".join(plugin_name.lower().split()) + "_controller"
224214
)
225215
self.plugins_dict[controller_name] = plugin_controller
226-
except:
216+
except Exception:
227217
messagebox.showwarning(
228218
title="Warning",
229219
message=(
230220
f"Plugin {plugin_name} has something went wrong."
231221
f"Please make sure the plugin works correctly or uninstall it!"
232-
)
222+
),
233223
)
234224

235225
def build_popup_window(self, plugin_name, frame, controller):
@@ -283,13 +273,14 @@ def func(*args, **kwargs):
283273
def func_with_wrapper(*args, **kwargs):
284274
try:
285275
func(*args, **kwargs)
286-
except:
276+
except Exception:
287277
messagebox.showwarning(
288278
title="Warning",
289279
message=(
290-
f"Plugin {plugin_name} has something went wrong."
291-
f"Please make sure the plugin works correctly or uninstall it from navigate!"
292-
)
280+
f"Plugin {plugin_name} has something went wrong. Please make "
281+
f"sure the plugin works correctly or uninstall it from "
282+
f"navigate!"
283+
),
293284
)
294285

295286
return func_with_wrapper

0 commit comments

Comments
 (0)