
\begin{DoxyItemize}
\item infos = Information about the python plugin is in keys below
\item infos/author = Manuel Mausz \href{mailto:manuel-elektra@mausz.at}{\tt manuel-\/elektra@mausz.\+at}
\item infos/licence = B\+S\+D
\item infos/provides =
\item infos/needs =
\item infos/placements =
\item infos/status = maintained unittest configurable memleak
\item infos/description = proxy that calls other plugins (scripts) written in python
\end{DoxyItemize}

The plugin uses Python to do magic things. It basically allows to call plugins 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.

\subsection*{Usage}

The python plugin requires the configuration parameter {\bfseries script} holding the file path to a python script. The mount command would look like \begin{DoxyVerb}kdb mount file.ini /python python script=/path/to/filter_script.py
\end{DoxyVerb}


if the {\bfseries 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 (json) storage plugin itself, one could also use \begin{DoxyVerb}kdb mount file.json /python python script=/path/to/json_plugin.py
\end{DoxyVerb}


\subsubsection*{Plugin Configuration}

The python plugin supports following optional configuration values/flags\+:


\begin{DoxyItemize}
\item {\ttfamily print} (flag)\+: Make the plugin print engine errors, triggered by the calls of this plugin, to stderr. Mainly intended for diagnostic. Please note that the Python engine itself will print script errors to stderr regardless of this flag.
\item {\ttfamily shutdown} (value, 0 or 1)\+: If enabled, the last call to {\ttfamily \hyperlink{group__kdb_gadb54dc9fda17ee07deb9444df745c96f}{kdb\+Close()}} will also shutdown Pythons engine. Default is 0.
\end{DoxyItemize}

\subsubsection*{Python Scripts}

Python scripts must implement a class called {\ttfamily Elektra\+Plugin} with one parameter. The class itself can implement the following functions


\begin{DoxyItemize}
\item open(self, config, error\+Key)
\item get(self, returned, parent\+Key)
\item set(self, returned, parent\+Key)
\item error(self, returned, parent\+Key)
\item close(self, error\+Key)
\end{DoxyItemize}

where {\itshape config} \& {\itshape returned} are Key\+Sets and {\itshape error\+Key} \& {\itshape parent\+Key} 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 {\bfseries kdb} can be retrieved using the Python import \begin{DoxyVerb}import kdb
\end{DoxyVerb}


\subsection*{Example}

An example script that prints some information for each method call would be\+: \begin{DoxyVerb}class ElektraPlugin(object):
    def open(self, config, errorKey):
        print("Python script method 'open' called")
        return 0

    def get(self, returned, parentKey):
        print("Python script method 'get' called")
        return 1

    def set(self, returned, parentKey):
        print("Python script method 'set' called")
        return 1

    def error(self, returned, parentKey):
        print("Python script method 'error' called")
        return 1

    def close(self, errorKey):
        print("Python script method 'close' called")
        return 0
\end{DoxyVerb}


Further examples can be found in the python directory.

\subsection*{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. The python plugin does also not clean up resources and is not restartable. 