Python
refactor many plugin injection points into 3
Chris Malonea94af0253d22
diff --git a/shared/utils/plugins.py b/shared/utils/plugins.pyindex 985b9f8..03206f3 100644--- a/shared/utils/plugins.py+++ b/shared/utils/plugins.py@@ -381,29 +381,36 @@ class WAN2GPApplication:self.plugin_manager = PluginManager()self.tab_to_plugin_map: Dict[str, WAN2GPPlugin] = {}self.all_rendered_tabs: List[gr.Tab] = []+ self.enabled_plugins: List[str] = []- def run_component_insertion(self, components_dict: Dict[str, Any]):- if self.plugin_manager:- self.plugin_manager.run_component_insertion_and_setup(components_dict)+ def initialize_plugins(self, wgp_globals: dict):+ if not hasattr(self, 'plugin_manager'):+ return++ server_config = wgp_globals.get("server_config")+ if not server_config:+ print("[PluginManager] ERROR: server_config not found in globals.")+ return++ self.enabled_plugins = server_config.get("enabled_plugins", [])+ self.plugin_manager.load_plugins_from_directory(self.enabled_plugins)+ self.plugin_manager.inject_globals(wgp_globals)- def _create_plugin_tabs(self, enabled_plugins):- if not self.plugin_manager:+ def setup_ui_tabs(self, main_tabs_component: gr.Tabs, state_component: gr.State):+ self._create_plugin_tabs()+ self._setup_tab_events(main_tabs_component, state_component)++ def _create_plugin_tabs(self):+ if not hasattr(self, 'plugin_manager'):returnloaded_plugins = self.plugin_manager.get_all_plugins()-- system_tabs = []- user_tabs = []+ system_tabs, user_tabs = [], []for plugin_id, plugin in loaded_plugins.items():for tab_id, tab in plugin.tabs.items():self.tab_to_plugin_map[tab.label] = plugin- tab_info = {- 'id': tab_id,- 'label': tab.label,- 'component_constructor': tab.component_constructor,- 'position': tab.position- }+ tab_info = {'id': tab_id, 'label': tab.label, 'component_constructor': tab.component_constructor, 'position': tab.position}if plugin_id in SYSTEM_PLUGINS:system_tabs.append(tab_info)else:@@ -411,11 +418,7 @@ class WAN2GPApplication:system_tabs.sort(key=lambda t: (t.get('position', -1), t['label']))- sorted_user_tabs = []- for plugin_id in enabled_plugins:- for pid, tab_info in user_tabs:- if pid == plugin_id:- sorted_user_tabs.append(tab_info)+ sorted_user_tabs = [tab_info for plugin_id in self.enabled_plugins for pid, tab_info in user_tabs if pid == plugin_id]pre_user_tabs = [t for t in system_tabs if t.get('position', -1) < USER_PLUGIN_INSERT_POSITION]post_user_tabs = [t for t in system_tabs if t.get('position', -1) >= USER_PLUGIN_INSERT_POSITION]@@ -427,6 +430,15 @@ class WAN2GPApplication:self.all_rendered_tabs.append(new_tab)tab_info['component_constructor']()+ def _setup_tab_events(self, main_tabs_component: gr.Tabs, state_component: gr.State):+ if main_tabs_component and state_component:+ main_tabs_component.select(+ fn=self._handle_tab_selection,+ inputs=[state_component],+ outputs=None,+ show_progress="hidden",+ )+def _handle_tab_selection(self, state: dict, evt: gr.SelectData):if not hasattr(self, 'previous_tab_id'):self.previous_tab_id = "video_gen"@@ -452,4 +464,8 @@ class WAN2GPApplication:print(f"[PluginManager] Error in on_tab_select for plugin {plugin_to_select.name}: {e}")traceback.print_exc()- self.previous_tab_id = new_tab_id\ No newline at end of file+ self.previous_tab_id = new_tab_id++ def run_component_insertion(self, components_dict: Dict[str, Any]):+ if hasattr(self, 'plugin_manager'):+ self.plugin_manager.run_component_insertion_and_setup(components_dict)\ No newline at end of filediff --git a/wgp.py b/wgp.pyindex 0f2aa26..a58d20b 100644--- a/wgp.py+++ b/wgp.py@@ -9404,8 +9404,7 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Nongen_inputs = [state_dict if k=="state" else locals_dict[k] for k in inputs_names] + [state_dict, plugin_data] + extra_inputsreturn gen_inputselse:- if hasattr(app, 'plugin_manager'):- app.run_component_insertion(locals_dict)+ app.run_component_insertion(locals_dict)return locals_dict@@ -10205,10 +10204,7 @@ def create_ui():js += """}"""- if hasattr(app, 'plugin_manager'):- enabled_plugins = server_config.get("enabled_plugins", [])- app.plugin_manager.load_plugins_from_directory(enabled_plugins)- app.plugin_manager.inject_globals(globals())+ app.initialize_plugins(globals())if server_config.get("display_stats", 0) == 1:from shared.utils.stats import SystemStatsAppstats_app = SystemStatsApp()@@ -10336,15 +10332,7 @@ def create_ui():outputs=[generator_tab_components['queue_html'], main_tabs, edit_tab],show_progress="hidden")- if hasattr(app, 'plugin_manager'):- app._create_plugin_tabs(enabled_plugins)- if main_tabs and state:- main_tabs.select(- fn=app._handle_tab_selection,- inputs=[state],- outputs=None,- show_progress="hidden",- )+ app.setup_ui_tabs(main_tabs, state)if stats_app is not None:stats_app.setup_events(main, state)return main