📜 ⬆️ ⬇️

DataEngine and Python2: Creating a new DataEngine

My first short note on DataEngine can be found here . Actually, I did not plan to continue this topic. But in the development process, it was decided to include a set of new labels, for example: loading of the GPU, temperature of the GPU, temperature of the HDD. In the process of implementation, I encountered some difficulties (“plasma drops” ©), and in the process of finding a way out of the situation, it was decided to create a new DataEngine with blackjack and harlots.
For those who missed something. DataEngine is a special class ( plasmascript.DataEngine ) in the PyKDE4 module. In fact - a list of dictionaries, each dictionary when accessing it gives some useful information. Perhaps the most popular "dictionaries" - systemmonitor and time, the purpose of both is obvious. I am plagued by vague doubts that half of KDE4 are somehow tied to the work of these DataEngines. The purpose of this topic is to create your DataEngine with the necessary dictionaries.
Anyone interested, I ask for habrakat.

Everything will be viewed from the point of view of the GPU temperature monitor, the rest is done with a slight movement of the hand (or fingers) in the image and likeness. Let's start with.

Call the required modules and declare the class:
from PyQt4.QtCore import * from PyKDE4.kdecore import * from PyKDE4 import plasmascript import commands class ExtendedSysMon(plasmascript.DataEngine): def __init__(self, parent, args=None): """dataengine definition""" plasmascript.DataEngine.__init__(self, parent) def init(self): """initialization""" self.setMinimumPollingInterval(333) # setup gpu device self.gpudev = '' commandOut = commands.getoutput("lspci") if (commandOut.lower().find('nvidia') > -1): self.gpudev = 'nvidia' elif (commandOut.lower().find('radeon') > -1): self.gpudev = 'ati' 

The first three modules are for the operation of the tool ( engine ), the last is for obtaining the temperature. In __init__ , as well as all cultural people we do nothing, except the announcement. In init everything is pretty simple. We set the minimum request interval (333) in ms, and determine which device we have (nvidia or ati). Of course, the second can be done better.
Create "sursy" ("dictionaries"):
  def sources(self): """create sources""" sources = ["gputemp"] return sources def sourceRequestEvent(self, name): return self.updateSourceEvent(name) 

We remembered (recorded) our sources , we still have to address them. The sourceRequestEvent function handles our (or not our) requests to dictionaries. Now let's write a function that handles this all ( updateSourceEvent ):
  def updateSourceEvent(self, source): """update sources and setup values""" if (source == "gputemp"): if (self.gpudev == 'nvidia'): commandOut = commands.getoutput("nvidia-smi -q -d TEMPERATURE | grep Gpu | tail -n1") try: value = "%4s" % (str(round(float(commandOut.split()[2]), 1))) except: value = " N\A" elif (self.gpudev == 'ati'): commandOut = commands.getoutput("aticonfig --od-gettemperature | grep Temperature | tail -n1") try: value = "%4s" % (str(round(float(commandOut.split()[4]), 1))) except: value = " N\A" else: value = " N\A" self.setData(source, "GPUTemp", QString(value)) return True 

Everything is simple and intuitive. We receive a string, parsim it, pull out the digits and pass to the dictionary. A little about the self.setData () function. It has three parameters: source (the name of the dictionary to which we refer, QString), the key of the dictionary (QString) and the value corresponding to this key (you can int, you can float or something, here QString). I note that, like in other normal dictionaries, there may be several keys - you can create one source for the HDD temperature, in which there are 3 keys for 3 hard disks. Oh yeah, try ... except - this is in order not to make dull software / video card checks and so on. If the source 's will be much - we make the construction if ... elif ....
All right, the script is almost ready:
 def CreateDataEngine(parent): return ExtendedSysMon(parent) 

Now we make it all beautiful:
 $ ls -R extsysmon extsysmon: contents metadata.desktop extsysmon/contents: code extsysmon/contents/code: main.py 

I am plagued by vague doubts (the source is flipping through) that if you put main.py in any other directory, nothing will start, although everything seems to be working. And create metadata.desktop :
 [Desktop Entry] Encoding=UTF-8 Name=Extended SystemMonitor DataEngine Comment=Adds gpu, gputemp and hddtemp to DataEngine ServiceTypes=Plasma/DataEngine Type=Service Icon=utilities-system-monitor X-Plasma-API=python X-Plasma-MainScript=code/main.py X-KDE-PluginInfo-Author=%username% X-KDE-PluginInfo-Email=%e-mail% X-KDE-PluginInfo-Name=ext-sysmon X-KDE-PluginInfo-Version=1.0 X-KDE-PluginInfo-Website=http://kde-look.org/ X-KDE-PluginInfo-Category=System Information X-KDE-PluginInfo-Depends= X-KDE-PluginInfo-License=GPL X-KDE-PluginInfo-EnabledByDefault=true 

It is crucial here - ServiceTypes , X-Plasma-API , X-Plasma-MainScript . The first indicates the type (DataEngine), the second indicates the shell, and the third indicates the path to the source script (excluding the contents). The rest is clear, as elsewhere.

Archiving (from working directory):
zip -qr extsysmon.zip contents metadata.desktop
Install:
plasmapkg -t dataengine -i extsysmon.zip
They say it is necessary to specify the type ( -t dataengine ) here, otherwise the widget will determine.
...
PROFIT!
Beautiful picture, everything works:

I do not determine the GPU load, but there is no GPU temperature, discrete graphics card is turned off.
Source code is available on github . Connecting the tool and working with it - in the first article .

')

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


All Articles