Plugin parameters

In the previous example, the home URL and number of feed items to generate were hard-coded in the script. We'll make these user-configurable by adding some parameters to the plugin.

Setting up parameters for a channel plugin

Start the Channel Wizard and select the script SamplePythonChannel2.py. This time there will be an extra step in the wizard that lets you set the home URL and the number of feed items to generate.

To define plugin parameters, create a file in the same directory that has the same name as the script but with a .plugin extension. It should look something like this:

[ChannelParameterDefinition-1]
Name = HomeUrl
Type = string
DefaultValue = http://www.test.com
Description = Our dummy home URL.
IsRequired = 1

[ChannelParameterDefinition-2]
Name = nItems
Type = int
DefaultValue = 15
Description = The number of dummy feed items to generate.
IsRequired = 1

Each section defines a new parameter and can be repeated as many times as required. The exact format of this file is given in the appendix.

Getting parameter values at runtime

When the script is run, it receives a single command-line parameter, the path to an INI file that contains the values for these parameters for the channel requesting the feed. For this example, if the user had changed the number of items to generate but left the home URL with the default value, the plugin will be given an INI file that might look something like this:

[ChannelParameters]
HomeUrl = http://www.test.com
nItems = 8

We now modify the start of the script to read these values from the INI file, instead of using hard-coded values:

# get the name of the INI file
configFilename = sys.argv[1]

# get our channel parameters
HOME_URL = win32api.GetProfileVal( "ChannelParameters" , "HomeUrl" , "" , configFilename )
NFEEDITEMS = win32api.GetProfileVal( "ChannelParameters" , "nItems" , 0 , configFilename )

# the rest of the script is the same as before...

Now you can create as many channels as you like using the same script, but with different parameters.