You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Yapsy的.yapsy-plugin配置文件中添加自定义配置项?

How to Integrate Custom Configs Directly into Yapsy's .yapsy-plugin Files

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:25:26