如何在Yapsy的.yapsy-plugin配置文件中添加自定义配置项?
Awesome question! I’ve used Yapsy for several plugin-based projects, and integrating custom configs directly into the .yapsy-plugin file is totally straightforward—since Yapsy’s PluginInfo class is built on top of Python’s standard ConfigParser, you can extend the config file with your own fields without extra setup. Let’s break this down step by step:
Step 1: Add Custom Configs to Your .yapsy-plugin File
You can add custom attributes either directly in the existing [Core] section (like you wanted) or create dedicated sections for plugin-specific settings. Here’s an example of both approaches:
Example .yapsy-plugin File
[Core] Name = bt Module = bt # Custom attributes in the Core section AttributeX = 1 EnableAutoSync = True ApiToken = my-plugin-token-123 # Optional: Dedicated section for plugin-specific settings [BtPluginSettings] MaxRetryCount = 3 LogOutputPath = /var/log/bt_plugin.log DefaultTimeout = 10
Step 2: Access the Custom Config in Your Plugin Code
Every Yapsy plugin instance comes with a plugin_info attribute (an instance of yapsy.PluginInfo) that acts like a ConfigParser object. You can use standard methods like get(), getint(), getboolean() to fetch your custom values directly in the plugin.
Example Plugin Implementation
from yapsy.IPlugin import IPlugin class BtPlugin(IPlugin): def activate(self): # Fetch values from the [Core] section self.attribute_x = self.plugin_info.getint("Core", "AttributeX") self.auto_sync_enabled = self.plugin_info.getboolean("Core", "EnableAutoSync") self.api_token = self.plugin_info.get("Core", "ApiToken") # Fetch values from the custom [BtPluginSettings] section self.max_retries = self.plugin_info.getint("BtPluginSettings", "MaxRetryCount") self.log_path = self.plugin_info.get("BtPluginSettings", "LogOutputPath") print(f"Bt Plugin activated with AttributeX: {self.attribute_x}") def deactivate(self): print("Bt Plugin deactivated")
Step 3: Handle Missing Config Values (Optional)
If you want to set default values for cases where a config field might be missing, use the fallback parameter (supported in Python 3.8+ for ConfigParser):
# Use fallback defaults if the config field doesn't exist self.default_timeout = self.plugin_info.getint("BtPluginSettings", "DefaultTimeout", fallback=15) self.log_level = self.plugin_info.get("BtPluginSettings", "LogLevel", fallback="INFO")
Step 4: Verify Configs in Your Main Application
When loading plugins in your main app, you can also access the custom config values directly from the PluginInfo object if needed:
from yapsy.PluginManager import PluginManager # Initialize plugin manager plugin_manager = PluginManager() plugin_manager.setPluginPlaces(["./plugins"]) # Path to your plugin directory plugin_manager.collectPlugins() # Iterate over loaded plugins and access configs for plugin_info in plugin_manager.getAllPlugins(): print(f"\nLoaded Plugin: {plugin_info.name}") print(f"AttributeX Value: {plugin_info.getint('Core', 'AttributeX')}") # Activate the plugin to trigger its config loading plugin_info.plugin_object.activate()
This approach lets you fully integrate all plugin configuration into the .yapsy-plugin file, eliminating the need for separate config files. The PluginInfo class handles all parsing and storage, so you don’t need to write any extra config-loading code.
内容的提问来源于stack exchange,提问作者svenema

