wiki:plugin_python

Python Plugin

The Python plugin is one of the most powerful tools available in LCD4Linux since it allows using all kinds of Python functions in very simple, yet efficient way. You can query the status of your machine (CPU, temperature, battery), get information about your system (dbus, mpris, systemd) or scrap news from the web (http, email, twitter) with a few lines of Python code.

Python 2

Currently the python-plugin only supports Python 2. It comes with the standard download package but needs to be enabled via configure:

  $ ./configure --with-python
  $ make

Successfully compiled you can call Python functions in expressions using the following form:

 python::exec(python_modulename, python_func_name, arg)

All args are mandatory. arg is a single string argument in Python and may have to be unpacked in your Python function.

In this example

  expression python::exec('myPythonFileName','myPythonFunction','myFirstArgument')

in the file named "myPythonFileName.py"

  def myPythonFunction(args):
  return "Hello World"

is called. Remember to always return a string to get a result for the calling expression.

If your module "myPythonFileName.py" is not in a standard location, you must set PYTHONPATH to where your module is located before executing lcd4linux.

  $ export PYTHONPATH='/home/myuserhome/bin'

Python 3

For Python 3 things are quite similar - configure LCD4Linux and make the python-enabled version. Fortunately configure comes with a python-version switch:

  $ ./configure --with-python PYTHON_VERSION="3.4"
  $ make

Unfortunately there are two problems currently:

Problem 1: Due to a bug in ax_python_devel.m4 configure initially fails with python 3.4 complaining about incorrect python library paths.

checking consistency of all components of python development environment... no
configure: error: in `/home/lcd4linux':
configure: error: 
  Could not link test program to Python. Maybe the main Python library has been
  installed in some non-standard library path. If so, pass it to configure,
  via the LDFLAGS environment variable.
  Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
  ============================================================================
   ERROR!
   You probably have to install the development version of the Python package
   for your distribution.  The exact name of this package varies among them.
  ============================================================================

Solution a: Use PYTHON_LDFLAGS="-lpython3.4m".

Solution b: Correct the problem and get the lastest version of ax_python_devel.m4 from the authors (http://www.gnu.org/software/autoconf-archive/ax_python_devel.html) and create a new config file.

  $ ./bootstrap 

Problem 2: The plugin itself tries to call Python 2 functions no longer present in Python 3.

Solution: Patch plugin_python.c

58c58
<     pName = PyString_FromString(module);
---
>     pName = PyUnicode_FromString(module);
74c74
<               pValue = PyString_FromString(argv[i]);
---
>               pValue = PyUnicode_FromString(argv[i]);
88c88
<               rv = PyString_AsString(pValue);
---
>               rv = PyUnicode_AsUTF8(pValue); /* Non ASCII characters are not encoded correctly! */
131c131
<     AddFunction("python::exec", 3, my_exec);
---
>     AddFunction("python3::exec", 3, my_exec);

To avoid confusion between different versions the respective python expression has changed to "python3::exec".

  expression python3::exec('myPythonFileName','myPythonFunction','myFirstArgument')
Last modified 3 years ago Last modified on 03/02/16 16:43:51