$darkmode
Elektra 0.11.0
Plugin: python

The plugin uses Python to do magic things. It allows plugins to be written in Python.

What a Python script can do is not really limited by design, so any kind of plugin may be implemented. The python plugin is especially useful to write filter and logging scripts.

Installation

See installation. The package is called libelektra5-python.

Usage

The python plugin requires the configuration parameter script holding the file path to a python script. The mount command would look like

1 sudo kdb mount file.ini /python python script=/path/to/filter_script.py

if the ini plugin should be used for storage and the python plugin only serves to invoke the filter script.

For a Python script that serves as INI storage plugin itself, one uses

1 sudo kdb mount file.json /python python script=python_configparser.py

Plugin Configuration

The python plugin supports following optional configuration values/flags:

Python Scripts

Python scripts must implement a class called ElektraPlugin with one parameter. The class itself can implement the following functions

where config & returned are KeySets and errorKey & parentKey are Keys. For the return codes of the functions, the same rules as for normal plugins apply.

If a function is not available, it simply is not called. A script does not have to implement all functions therefore.

Access to kdb can be retrieved using the Python import

1 import kdb

Example

An example script that prints some information for each method call would be:

1 import kdb
2 
3 class ElektraPlugin(object):
4  def open(self, config, errorKey):
5  """
6  returns
7  * 0: no error
8  * -1: error during initialization
9  """
10  print("Python script method 'open' called")
11  return 0
12 
13  def get(self, returned, parentKey):
14  """
15  returns
16  * 1: on success
17  * 0: nothing was to do
18  * -1: failure
19  """
20  print("Python script method 'get' called")
21  return 1
22 
23  def set(self, returned, parentKey):
24  """
25  returns
26  * 1: on success
27  * 0: nothing was to do
28  * -1: failure
29  """
30  print("Python script method 'set' called")
31  return 1
32 
33  def error(self, returned, parentKey):
34  """
35  returns
36  * 1: on success
37  * 0: on success with no action
38  * -1: failure
39  """
40  print("Python script method 'error' called")
41  return 1
42 
43  def close(self, errorKey):
44  print("Python script method 'close' called")
45  return 0

Further examples can be found in the python directory.

Disclaimer

Be aware that a Python script will never be as performant as a native C/C++ plugin. Spinning up the interpreter takes additional time and resources.