1717
1818def find_gui_modules ():
1919 """
20- Recursively finds all GUI modules in the project, excluding non-GUI files .
21- Returns them in a format suitable for importlib (e.g., 'Keithley_2400 .IV_K2400_GUI').
20+ Recursively finds all GUI modules in the `pica` directory .
21+ Returns them in a format suitable for importlib (e.g., 'pica.keithley.k2400 .IV_K2400_GUI').
2222 """
2323 gui_files = []
24- for root , _ , files in os .walk (project_root ):
25- # Exclude directories that are not part of the main source code
26- if 'tests' in root or '.git' in root or 'venv' in root or '__pycache__' in root or 'assets' in root or 'Setup' in root :
27- continue
24+ pica_dir = os .path .join (project_root , 'pica' )
25+ for root , _ , files in os .walk (pica_dir ):
2826 for file in files :
29- if file .endswith ('_GUI_v' or ' _GUI.py' ) and file != '__init__.py' :
27+ if file .endswith ('_GUI.py' ) and not file . startswith ( '__' ) :
3028 full_path = os .path .join (root , file )
3129 # Convert file path to module path
32- # e.g., F:\...\PICA\Keithley_2400\IV_K2400_GUI.py -> Keithley_2400.IV_K2400_GUI
3330 relative_path = os .path .relpath (full_path , project_root )
3431 module_path = os .path .splitext (relative_path )[0 ].replace (os .path .sep , '.' )
3532 gui_files .append (module_path )
@@ -50,20 +47,24 @@ def test_gui_module_initialization(module_path):
5047 gui_module = importlib .import_module (module_path )
5148
5249 # The main application class is assumed to have the same name as the file,
53- # but without the version suffix (e.g., 'IV_K2400_GUI' from 'IV_K2400_GUI.py')
54- # This logic needs to be robust to handle different naming conventions.
5550 base_name = module_path .split ('.' )[- 1 ]
56- if '_GUI_v' in base_name :
57- class_name = base_name .split ('_GUI_v' )[0 ] + "_GUI"
58- elif base_name .endswith ('_GUI' ):
59- class_name = base_name
60- else :
61- pytest .fail (f"Could not determine class name from module path: { module_path } " )
51+
52+ # A more robust way to find the class.
53+ # We look for a class in the module that ends with 'GUI' and is defined in that module.
54+ gui_class = None
55+ for name , obj in gui_module .__dict__ .items ():
56+ if isinstance (obj , type ) and obj .__module__ == gui_module .__name__ :
57+ if name .endswith ('GUI' ):
58+ gui_class = obj
59+ break
60+
61+ if not gui_class :
62+ pytest .fail (f"Could not find a suitable GUI class in module: { module_path } " )
63+
6264
6365 # Get the class from the module and instantiate it
64- AppClass = getattr (gui_module , class_name )
6566 mock_root = MagicMock ()
66- app_instance = AppClass (mock_root )
67+ app_instance = gui_class (mock_root )
6768
6869 assert app_instance is not None , "GUI class failed to instantiate."
6970
0 commit comments