Project

General

Profile

metadict (or setting the parameters for the plugin)

This is a special Python object that is used to define the parameters a plugin will be requiring. It is basically a Python dictionary that has the ability to store more data (meta-data) on the stored keys.

You may use metadict just as the dict function:

$ python
>>> from evaluation_system.api.plugin import metadict
>>> dict(a=1) == metadict(a=1)
True
>>> d = metadict(a=1,b=2)
>>> d
{'a': 1, 'b': 2}

But you have a couple of other methods to set/access the metadata:

>>> d.setMetadata('a', foo='bar', bar=1)
>>> d.getMetadata('a')
{'foo': 'bar', 'bar': 1}
>>> metadict.getMetaValue(d, 'a', 'foo')
'bar'

The last method is used to get information out of any dictionary. All methods return None if there's no meta-data (even if the dictionary key is wrong).
While creating the metadict you may use a special key called compact_creation with the value of True to create the dictionary in just one line.

Here's how we would create the same d dictionary as before in this new way:

metadict(compact_creation = True,
  a=(1, dict(foo='bar, bar=1)),
  b=2)

You must have an item named compact_creation = True, if the key has no meta-data you would store it normally like in dict() (see b), if the value is a 2-item tuple, the first item is the value assigned to the key, and the second is a normal dictionary with all meta-data entries.

Usage of metadict in the Evaluation System

We use metadict to generate help, configuration files, etc, automagically for the plugin.

The metadict must be stored in a property called __config_metadict__ at the class extending the plugin abstract class. The keys are the properties being defined, their values the default values of them and the meta-data used by the system is in the following table summarized:

Name Type Explanation
help str Describes the purpose of the parameter.
mandatory bool If the parameter is mandatory or optional (if not present the parameter is assumed to be optional).
type type The type of the parameter. This is only required if the parameter has no default value from which the type can be inferred.

Take a look at the following complete plugin example:

from evaluation_system.api import plugin

class Test(plugin.PluginAbstract):
    __short_description__ = "A test" 
    __version__ = (0,0,1)
    __config_metadict__ =  metadict(compact_creation = True,
                                    iterations = (1,    dict(help='The number of iterations this tool will work')),
                                    var        = (None, dict(mandatory=True, type=str, help='The variable name')),
                                    special    = (None, dict(type=bool, help='If something special should happen')),
                                    answer     = 42
                                   )
    def runTool(self, config_dict=None):
        pass

This defines 4 parameters used by the plugin Test:
  • iterations: An integer (inferred) with a default value of 1 and some help.
  • var: A string (explicit) without any default value but required.
  • special: An optional boolean without any default value and some help.
  • answer: An integer (inferred) with a default value of 42 and no help.

Defining plugin parameters using metadict

We have already seen how metadict is used. If the default values are strings, they will be parsed looking for special tokens ($) that might refer to other parameters or to some special parameters described in the next section.

For example:

#[...]
    __config_metadict__ =  metadict(compact_creation = True,
                                    a = '$bc',
                                    b = '1',
                                    c = '${b}c',
                                    bc = '2'
                                    d = '$a$a$c$c',
                                   )

This results in:

a  == '2'
b  == '1'
c  == '1c'
bc == '2'
d  == '221c1c'

Note: The curly braces {} are used when it's not clear where the token ends, i.e. if there's a letter or a number immediately following the token name like in the case of ${b}c.