📜 ⬆️ ⬇️

DataEngine and Python2 or How I wrote my widget

Instead of the preface


After sitting a bit awesome, I decided to return to the more familiar ones, hung with many useful and not very good buns, sneakers (KDE4). After a while, I decided to remove the ugly self-written caramba widget (SuperKaramba) and replace it with a more elegant and simple plasma widget. The requirements for it were simple: it should be a simple widget on the panel that would display all the main (or rather, demanded by me) information as text. Poryskav by kde-look.org, I found one widget that suited me sufficiently. However, I needed some additional features that were not implemented in this widget. Also, the author did not provide for the possibility of easy configuration of the widget, and to configure it, you had to go into the original script written in JavaScript.
After several iterations of setting up an existing widget, due to my lack of any ideas about JS, I decided to write my own widget. Python2 was chosen from programming languages, in which I understand more or less. No sooner said than done. During the evening I wrote the first version of the widget, quite workable. Later, during testing, it was discovered that it has an unpleasant peculiarity that hangs during the update. Firstly, this was due to the fact that all fields were updated simultaneously. And secondly, and this is perhaps the most important thing: to parse data files (among them, / proc / stat and a file that contains information about the transmitted / received traffic), some delay is needed. If you put 0.2 seconds on them, then as a result the widget hangs for almost 0.5 seconds, which is noticeable even with the naked eye.
Then I made an attempt to rewrite the code, including DataEngine, which I spied in the original widget. Below we will talk about the use of this module in Python2.

The only problem with using this module is the lack of any documentation for it in Python2. There is a cpp with a description of the required parameters in the function (there is no such thing in Python), and there is a small note in the developer tutorial with a very childish example. Because of this, we had to work blindly in the image and likeness, so something could certainly be made more elegant and simpler. Immediately, before turning to the narrative, I note that my programming skills are not so great anymore, which also cannot but affect the quality of the code. So, I ask you to excuse me, if suddenly I do something wrong.
Also, the purpose of this article does not include a guide on writing plasmoids in Python, for this there are already other articles, for example: one and two times .
So, after a long introduction, let's get started.

The basics


You can view the list of available services using the plasmaengineexplorer utility (in ArchLinux is included in the plasmate package, available in the aura). The interface is simple, like boots:

We chose the necessary tool (engine) from the drop-down list, many lines appeared that contain information (in Python it is transmitted in the form of a dictionary). It should be noted that, at least in the case of the systemmonitor tool, the variable also has the key ' value ', which contains the information of interest.
Connecting the tool is even easier (in the class of the widget itself):
from PyKDE4.plasma import Plasma def connectToEngine(self): """function to initializate engine""" self.systemmonitor = self.dataEngine("systemmonitor") 

The only option is our DataEngine. However, we have not connected to the necessary data yet, therefore we will not see anything. If we want to know the average value of the processor load, then the continuation of the function will look like this:
  self.systemmonitor.connectSource("cpu/system/TotalLoad", self, 1000) 

Here are three parameters. The first is where we go to — source (in general, it requires the format of QString, although, usually, you can also slip a simple string). This is the first column in the plasmaengineexplorer . The second parameter is the visualization (where the parameter will be sent), we specify the widget itself. The third parameter is the update interval (int), ms is the time after which we will ask if the information is updated. In general, everything is ready, it remains only to add the update function:
 @pyqtSignature("dataUpdated(const QString &, const Plasma::DataEngine::Data &)") def dataUpdated(self, sourceName, data): """function to refresh data""" if (sourceName == "cpu/system/TotalLoad"): value = str(round(float(data[QString(u'value')]), 1)) cpuText = "%5s" % (value) 

cpuText is a five-character string — for example, '100.0'. A few comments - data (here) is our dictionary, sourceName - source, which is updated (indicated above), data [QString (u'value ')] - the value of interest (after other keys - again, to plasmaengineexplorer ). Voila, the widget is ready, it remains only to display the resulting value and that's it. I note that updating the widget text makes sense right in this function.

Some particulars


Update

If you use only DataEngine, you do not need to create a timer, the data will be updated themselves. Also, it is worth noting that I could not make friends with the powermanager tool (to read the battery charge) - this was due to the fact that the charge value was not updated by itself (if the power adapter is connected). Accordingly, there was no reading of the initial value, until it changes (unless, of course, I understand correctly).
')
Simultaneous update

It may be necessary if you want, for example, to calculate the memory load in%. Honestly, I didn’t manage to do this. However, if I am not mistaken, I need to dig in the direction of such a combination:
 self.engine.addSource(self.container) self.engine.connectAllSources(self, 1000) 

We add source'y, and then we connect them at once. Problem with container definition (variable type is PyKDE4.plasma.Plasma.DataContainer). You can create your own container and fill it with data, but I could not connect it with an existing source. Therefore, in the current version of the widget, the values ​​that are further used are written into variables that are processed by a special function after the timer ends.

Advantages and disadvantages

Parallel update of widget elements, no delays (the data is read right there) and high speed of work. Also, it is worth noting that DataEngine contains all sorts of useful things that can be used when writing widgets. Among the shortcomings is the dependence on the presence of a module in the system ( kdebindings-python2 package in the case of ArchLinux).

Afterword


Widget sources are available on github or kde-look.org . In the final version, the widget (if anyone is interested) looks like this (although a fairly flexible setting is provided):

I apologize if, in your opinion, the text is very smeared, if you wish, you can shrink it twice.

Source: https://habr.com/ru/post/171155/


All Articles