|
33 | 33 | from pydantic import BaseModel |
34 | 34 |
|
35 | 35 | from nodescraper.constants import DEFAULT_LOGGER |
36 | | -from nodescraper.interfaces import ConnectionManager, DataPlugin |
| 36 | +from nodescraper.interfaces import ConnectionManager, DataPlugin, PluginInterface |
37 | 37 | from nodescraper.models import PluginConfig, SystemInfo |
38 | 38 | from nodescraper.models.pluginresult import PluginResult |
39 | 39 | from nodescraper.pluginregistry import PluginRegistry |
@@ -165,16 +165,25 @@ def run_queue(self) -> list[PluginResult]: |
165 | 165 | plugin_inst = plugin_class(**init_payload) |
166 | 166 |
|
167 | 167 | run_payload = copy.deepcopy(plugin_args) |
168 | | - |
169 | 168 | run_args = TypeUtils.get_func_arg_types(plugin_class.run, plugin_class) |
| 169 | + |
170 | 170 | for arg in run_args.keys(): |
171 | 171 | if arg == "preserve_connection" and issubclass(plugin_class, DataPlugin): |
172 | 172 | run_payload[arg] = True |
173 | | - elif arg in self.plugin_config.global_args: |
174 | | - run_payload[arg] = self.plugin_config.global_args[arg] |
175 | 173 |
|
176 | | - # TODO |
177 | | - # enable global substitution in collection and analysis args |
| 174 | + try: |
| 175 | + global_run_args = self.apply_global_args_to_plugin( |
| 176 | + plugin_inst, plugin_class, self.plugin_config.global_args |
| 177 | + ) |
| 178 | + run_payload.update(global_run_args) |
| 179 | + except ValueError as ve: |
| 180 | + self.logger.error( |
| 181 | + "Invalid global_args for plugin %s: %s. Skipping plugin.", |
| 182 | + plugin_name, |
| 183 | + str(ve), |
| 184 | + ) |
| 185 | + continue |
| 186 | + |
178 | 187 | self.logger.info("-" * 50) |
179 | 188 | plugin_results.append(plugin_inst.run(**run_payload)) |
180 | 189 | except Exception as e: |
@@ -210,3 +219,42 @@ def run_queue(self) -> list[PluginResult]: |
210 | 219 | ) |
211 | 220 |
|
212 | 221 | return plugin_results |
| 222 | + |
| 223 | + def apply_global_args_to_plugin( |
| 224 | + self, |
| 225 | + plugin_inst: PluginInterface, |
| 226 | + plugin_class: type, |
| 227 | + global_args: dict, |
| 228 | + ) -> dict: |
| 229 | + """ |
| 230 | + Applies global arguments to the plugin instance, including standard attributes |
| 231 | + and merging Pydantic model arguments (collection_args, analysis_args). |
| 232 | +
|
| 233 | + Args: |
| 234 | + plugin_inst: The plugin instance to update. |
| 235 | + plugin_class: The plugin class (needed for model instantiation). |
| 236 | + global_args: Dict of global argument overrides. |
| 237 | + """ |
| 238 | + |
| 239 | + run_args = {} |
| 240 | + for key in global_args: |
| 241 | + if key in ["collection_args", "analysis_args"] and isinstance(plugin_inst, DataPlugin): |
| 242 | + continue |
| 243 | + else: |
| 244 | + run_args[key] = global_args[key] |
| 245 | + |
| 246 | + if "collection_args" in global_args and hasattr(plugin_class, "COLLECTOR_ARGS"): |
| 247 | + plugin_fields = set(plugin_class.COLLECTOR_ARGS.__fields__.keys()) |
| 248 | + filtered = { |
| 249 | + k: v for k, v in global_args["collection_args"].items() if k in plugin_fields |
| 250 | + } |
| 251 | + if filtered: |
| 252 | + run_args["collection_args"] = filtered |
| 253 | + |
| 254 | + if "analysis_args" in global_args and hasattr(plugin_class, "ANALYZER_ARGS"): |
| 255 | + plugin_fields = set(plugin_class.ANALYZER_ARGS.__fields__.keys()) |
| 256 | + filtered = {k: v for k, v in global_args["analysis_args"].items() if k in plugin_fields} |
| 257 | + if filtered: |
| 258 | + run_args["analysis_args"] = filtered |
| 259 | + |
| 260 | + return run_args |
0 commit comments