awesome-widgets/sources/ptm/contents/code/dataengine.py
2014-07-09 13:12:52 +04:00

360 lines
22 KiB
Python

# -*- coding: utf-8 -*-
############################################################################
# This file is part of pytextmonitor #
# #
# pytextmonitor is free software: you can redistribute it and/or #
# modify it under the terms of the GNU General Public License as #
# published by the Free Software Foundation, either version 3 of the #
# License, or (at your option) any later version. #
# #
# pytextmonitor is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with pytextmonitor. If not, see http://www.gnu.org/licenses/ #
############################################################################
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.plasma import Plasma
import datetime
timeLetters = ['dddd', 'ddd', 'dd', 'd', \
'MMMM', 'MMM', 'MM', 'M', 'yyyy', 'yy', \
'hh', 'h', 'mm', 'm', 'ss', 's']
class DataEngine:
def __init__(self, parent):
"""class definition"""
# debug
environment = QProcessEnvironment.systemEnvironment()
debugEnv = environment.value(QString("PTM_DEBUG"), QString("no"));
if (debugEnv == QString("yes")):
self.debug = True
else:
self.debug = False
# main
self.parent = parent
def connectToEngine(self, bools=None, dataEngines=None, interval=1000, names=None):
"""function to initializate engine"""
if self.debug: qDebug("[PTM] [dataengine.py] [connectToEngine]")
if self.debug: qDebug("[PTM] [dataengine.py] [connectToEngine] : Run function with bools '%s'" %(bools))
if self.debug: qDebug("[PTM] [dataengine.py] [connectToEngine] : Run function with dataengines '%s'" %(dataEngines))
if self.debug: qDebug("[PTM] [dataengine.py] [connectToEngine] : Run function with names '%s'" %(names))
if (bools['cpu'] > 0):
dataEngines['system'].connectSource("cpu/system/TotalLoad", self.parent, interval)
for core in range(8):
dataEngines['system'].connectSource("cpu/cpu" + str(core) + "/TotalLoad", self.parent, interval)
if (bools['cpuclock'] > 0):
dataEngines['system'].connectSource("cpu/system/AverageClock", self.parent, interval)
for core in range(8):
dataEngines['system'].connectSource("cpu/cpu" + str(core) + "/clock", self.parent, interval)
if (bools['custom'] > 0):
dataEngines['ext'].connectSource("custom", self.parent, interval)
if (bools['desktop'] > 0):
dataEngines['ext'].connectSource("desktop", self.parent, interval)
if (bools['disk'] > 0):
for item in names['disk']:
dataEngines['system'].connectSource(item + "/Rate/rblk", self.parent, interval)
dataEngines['system'].connectSource(item + "/Rate/wblk", self.parent, interval)
if (bools['gpu'] > 0):
dataEngines['ext'].connectSource("gpu", self.parent, interval)
if (bools['gputemp'] > 0):
dataEngines['ext'].connectSource("gputemp", self.parent, interval)
if (bools['hdd'] > 0):
for item in names['hdd']:
dataEngines['system'].connectSource("partitions" + item + "/filllevel", self.parent, interval)
dataEngines['system'].connectSource("partitions" + item + "/freespace", self.parent, interval)
dataEngines['system'].connectSource("partitions" + item + "/usedspace", self.parent, interval)
if (bools['hddtemp'] > 0):
dataEngines['ext'].connectSource("hddtemp", self.parent, interval)
if (bools['mem'] > 0):
dataEngines['system'].connectSource("mem/physical/free", self.parent, interval)
dataEngines['system'].connectSource("mem/physical/used", self.parent, interval)
dataEngines['system'].connectSource("mem/physical/application", self.parent, interval)
if (bools['net'] > 0):
self.updateNetdev = 0
dataEngines['system'].connectSource("network/interfaces/" + names['net'] + "/transmitter/data", self.parent, interval)
dataEngines['system'].connectSource("network/interfaces/" + names['net'] + "/receiver/data", self.parent, interval)
if (bools['pkg'] > 0):
dataEngines['ext'].connectSource("pkg", self.parent, 60*60*1000, Plasma.AlignToHour)
if (bools['player'] > 0):
dataEngines['ext'].connectSource("player", self.parent, interval)
if (bools['ps'] > 0):
dataEngines['ext'].connectSource("ps", self.parent, interval)
if (bools['swap'] > 0):
dataEngines['system'].connectSource("mem/swap/free", self.parent, interval)
dataEngines['system'].connectSource("mem/swap/used", self.parent, interval)
if (bools['temp'] > 0):
for item in names['temp']:
dataEngines['system'].connectSource(item, self.parent, interval)
if (bools['time'] > 0):
dataEngines['time'].connectSource("Local", self.parent, 1000)
if (bools['uptime'] > 0):
dataEngines['system'].connectSource("system/uptime", self.parent, interval)
def dataUpdated(self, sourceName, data, ptm):
"""function to update data"""
if self.debug: qDebug("[PTM] [dataengine.py] [dataUpdated]")
if self.debug: qDebug("[PTM] [dataengine.py] [dataUpdated] : Run function with source '%s'" %(sourceName))
if self.debug: qDebug("[PTM] [dataengine.py] [dataUpdated] : Run function with data '%s'" %(data))
if self.debug: qDebug("[PTM] [dataengine.py] [dataUpdated] : Run function with settings '%s'" %(ptm))
adv = ptm['vars']['adv']
systemDataEngine = ptm['dataengine']['system']
formats = ptm['vars']['formats']
interval = ptm['vars']['app']['interval']
names = ptm['names']
updatedData = {'name':None, 'type':None, 'value':None}
try:
if (sourceName == "cpu/system/TotalLoad"):
updatedData['name'] = "cpu"
updatedData['type'] = -1
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = value
elif ((sourceName[:7] == "cpu/cpu") and (sourceName.split('/')[2] == "TotalLoad")):
updatedData['name'] = "cpu"
updatedData['type'] = int(sourceName[7])
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = value
elif (sourceName == "cpu/system/AverageClock"):
updatedData['name'] = "cpuclock"
updatedData['type'] = -1
value = round(data[QString(u'value')].toFloat()[0], 0)
updatedData['value'] = value
elif ((sourceName[:7] == "cpu/cpu") and (sourceName.split('/')[2] == "clock")):
updatedData['name'] = "cpuclock"
updatedData['type'] = int(sourceName[7])
value = round(data[QString(u'value')].toFloat()[0], 0)
updatedData['value'] = value
elif (sourceName == "custom"):
updatedData['name'] = "custom"
updatedData['value'] = {}
for singleData in data.keys():
updatedData['value'][str(singleData.toLower())] = str(data[singleData].toUtf8()).decode("utf-8")
elif (sourceName == "desktop"):
updatedData['name'] = "desktop"
updatedData['value'] = {}
updatedData['value']['current'] = data[QString(u'currentNumber')]
updatedData['value']['names'] = [str(desktop.toUtf8()).decode("utf-8") for desktop in data[QString(u'list')].split(';;')]
elif ((sourceName[:4] == "disk") and (sourceName[-4:] == "rblk")):
updatedData['name'] = "disk-r"
updatedData['type'] = '/'.join(str(sourceName).split('/')[0:2])
value = round(data[QString(u'value')].toFloat()[0], 0)
updatedData['value'] = value
elif ((sourceName[:4] == "disk") and (sourceName[-4:] == "wblk")):
updatedData['name'] = "disk-w"
updatedData['type'] = '/'.join(str(sourceName).split('/')[0:2])
value = round(data[QString(u'value')].toFloat()[0], 0)
updatedData['value'] = value
elif (sourceName == "gpu"):
updatedData['name'] = "gpu"
value = round(data[QString(u'GPU')], 1)
updatedData['value'] = "%4.1f" % (value)
elif (sourceName == "gputemp"):
updatedData['name'] = "gputemp"
value = round(data[QString(u'GPUTemp')], 1)
updatedData['value'] = "%4.1f" % (value)
elif (sourceName.split('/')[0] == "partitions") and (sourceName.split('/')[-1] == "filllevel"):
updatedData['name'] = "hdd"
updatedData['type'] = '/' + '/'.join(sourceName.split('/')[1:-1])
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = value
elif (sourceName.split('/')[0] == "partitions") and (sourceName.split('/')[-1] == "freespace"):
updatedData['name'] = "hddfreemb"
updatedData['type'] = '/' + '/'.join(sourceName.split('/')[1:-1])
value = data[QString(u'value')].toFloat()[0]
updatedData['value'] = value
elif (sourceName.split('/')[0] == "partitions") and (sourceName.split('/')[-1] == "usedspace"):
updatedData['name'] = "hddmb"
updatedData['type'] = '/' + '/'.join(sourceName.split('/')[1:-1])
value = data[QString(u'value')].toFloat()[0]
updatedData['value'] = value
elif (sourceName == "hddtemp"):
updatedData['name'] = "hddtemp"
updatedData['value'] = {}
for item in names['hddtemp']:
value = round(data[QString(item)], 1)
updatedData['value'][item] = "%4.1f" % (value)
elif (sourceName == "mem/physical/application"):
updatedData['name'] = "mem"
updatedData['type'] = "app"
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = value
elif (sourceName == "mem/physical/free"):
updatedData['name'] = "mem"
updatedData['type'] = "free"
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = value
elif (sourceName == "mem/physical/used"):
updatedData['name'] = "mem"
updatedData['type'] = "used"
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = value
elif ((sourceName.split('/')[0] == "network") and (sourceName.split('/')[3] == "receiver")):
updatedData['name'] = "net"
updatedData['type'] = "down"
value = round(data[QString(u'value')].toFloat()[0], 0)
updatedData['value'] = value
# update network device
self.updateNetdev = self.updateNetdev + 1
if (self.updateNetdev == 30):
self.updateNetdev = 0
if (adv['netdevBool'] == 0):
systemDataEngine.disconnectSource("network/interfaces/" + names['net'] + "/transmitter/data", self.parent)
systemDataEngine.disconnectSource("network/interfaces/" + names['net'] + "/receiver/data", self.parent)
names['net'] = self.parent.setNetdev()
systemDataEngine.connectSource("network/interfaces/" + names['net'] + "/transmitter/data", self.parent, interval)
systemDataEngine.connectSource("network/interfaces/" + names['net'] + "/receiver/data", self.parent, interval)
elif ((sourceName.split('/')[0] == "network") and (sourceName.split('/')[3] == "transmitter")):
updatedData['name'] = "net"
updatedData['type'] = "up"
value = round(data[QString(u'value')].toFloat()[0], 0)
updatedData['value'] = value
elif (sourceName == "pkg"):
updatedData['name'] = "pkg"
updatedData['value'] = {}
for singleData in data.keys():
updatedData['value'][str(singleData.toLower())] = "%i" % (data[singleData])
elif (sourceName == "player"):
updatedData['name'] = "player"
updatedData['value'] = {}
updatedData['value']['album'] = str(data[QString(u'album')].toUtf8()).decode("utf-8")
updatedData['value']['artist'] = str(data[QString(u'artist')].toUtf8()).decode("utf-8")
updatedData['value']['progress'] = str(data[QString(u'progress')].toUtf8()).decode("utf-8")
updatedData['value']['time'] = str(data[QString(u'duration')].toUtf8()).decode("utf-8")
updatedData['value']['title'] = str(data[QString(u'title')].toUtf8()).decode("utf-8")
elif (sourceName == "ps"):
updatedData['name'] = "ps"
updatedData['value'] = {}
updatedData['value']['num'] = "%i" % (data[QString(u'psCount')])
updatedData['value']['total'] = "%i" % (data[QString(u'psTotal')])
updatedData['value']['list'] = data[QString(u'ps')]
elif (sourceName == "mem/swap/free"):
updatedData['name'] = "swap"
updatedData['type'] = "free"
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = value
elif (sourceName == "mem/swap/used"):
updatedData['name'] = "swap"
updatedData['type'] = "used"
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = value
elif (sourceName.split('/')[0] == "lmsensors"):
updatedData['name'] = "temp"
updatedData['type'] = sourceName
value = round(data[QString(u'value')].toFloat()[0], 1)
updatedData['value'] = "%4.1f" % (value)
elif (sourceName == "Local"):
updatedData['name'] = "time"
if (formats['time'].split('$time')[0] != formats['time']):
value = str(data[QString(u'DateTime')].toString(Qt.TextDate).toUtf8()).decode("utf-8")
elif (formats['time'].split('$isotime')[0] != formats['time']):
value = str(data[QString(u'DateTime')].toString(Qt.ISODate).toUtf8()).decode("utf-8")
elif (formats['time'].split('$shorttime')[0] != formats['time']):
value = str(data[QString(u'DateTime')].toString(Qt.SystemLocaleShortDate).toUtf8()).decode("utf-8")
elif (formats['time'].split('$longtime')[0] != formats['time']):
value = str(data[QString(u'DateTime')].toString(Qt.SystemLocaleLongDate).toUtf8()).decode("utf-8")
elif (formats['time'].split('$custom')[0] != formats['time']):
rawDate = data[QString(u'DateTime')]
value = adv['customTime']
for letters in timeLetters:
if (value.split('$' + letters)[0] != value):
value = value.split('$' + letters)[0] + str(rawDate.toString(letters).toUtf8()).decode("utf-8") + value.split('$' + letters)[1]
updatedData['value'] = value
elif (sourceName == "system/uptime"):
updatedData['name'] = "uptime"
value = datetime.timedelta(0, int(round(float(data[QString(u'value')]), 1)))
days = value.days
hours = int(value.seconds / 60 / 60)
minutes = int(value.seconds / 60 % 60)
if (formats['uptime'].split('$uptime')[0] != formats['uptime']):
updatedData['value'] = "%3id%2ih%2im" % (days, hours, minutes)
elif (formats['uptime'].split('$custom')[0] != formats['uptime']):
updatedData['value'] = adv['customUptime']
if (updatedData['value'].split('$dd')[0] != updatedData['value']):
updatedData['value'] = "%s%03i%s" % (updatedData['value'].split('$dd')[0], days, updatedData['value'].split('$dd')[1])
if (updatedData['value'].split('$d')[0] != updatedData['value']):
updatedData['value'] = "%s%i%s" % (updatedData['value'].split('$d')[0], days, updatedData['value'].split('$d')[1])
if (updatedData['value'].split('$hh')[0] != updatedData['value']):
updatedData['value'] = "%s%02i%s" % (updatedData['value'].split('$hh')[0], hours, updatedData['value'].split('$hh')[1])
if (updatedData['value'].split('$h')[0] != updatedData['value']):
updatedData['value'] = "%s%i%s" % (updatedData['value'].split('$h')[0], hours, updatedData['value'].split('$h')[1])
if (updatedData['value'].split('$mm')[0] != updatedData['value']):
updatedData['value'] = "%s%02i%s" % (updatedData['value'].split('$mm')[0], minutes, updatedData['value'].split('$mm')[1])
if (updatedData['value'].split('$m')[0] != updatedData['value']):
updatedData['value'] = "%s%i%s" % (updatedData['value'].split('$m')[0], minutes, updatedData['value'].split('$m')[1])
except:
pass
if self.debug: qDebug("[PTM] [dataengine.py] [dataUpdated] : Returns '%s'" %(updatedData))
return updatedData
def disconnectFromSource(self, dataEngines=None, keys=None, name=None):
"""function to disconnect from sources"""
if self.debug: qDebug("[PTM] [dataengine.py] [disconnectFromSource]")
if self.debug: qDebug("[PTM] [dataengine.py] [disconnectFromSource] : Run function with dataengines '%s'" %(dataEngines))
if self.debug: qDebug("[PTM] [dataengine.py] [disconnectFromSource] : Run function with keys '%s'" %(keys))
if self.debug: qDebug("[PTM] [dataengine.py] [disconnectFromSource] : Run function with name '%s'" %(name))
if (name == "bat"):
pass
elif (name == "cpu"):
dataEngines['system'].disconnectSource("cpu/system/TotalLoad", self.parent)
for item in keys['cpu']:
if (item != -1):
dataEngines['system'].disconnectSource("cpu/cpu" + str(item) + "/TotalLoad", self.parent)
elif (name == "cpuclock"):
dataEngines['system'].disconnectSource("cpu/system/AverageClock", self.parent)
for item in keys['cpuclock']:
if (item != -1):
dataEngines['system'].disconnectSource("cpu/cpu" + str(item) + "/clock", self.parent)
elif (name == "custom"):
dataEngines['ext'].disconnectSource("custom", self.parent)
elif (name == "desktop"):
dataEngines['ext'].disconnectSource("desktop", self.parent)
elif (name == "disk"):
for item in keys['disk']:
dataEngines['system'].disconnectSource(item + "/Rate/rblk", self.parent)
dataEngines['system'].disconnectSource(item + "/Rate/rblk", self.parent)
elif (name == "gpu"):
dataEngines['ext'].disconnectSource("gpu", self.parent)
elif (name == "gputemp"):
dataEngines['ext'].disconnectSource("gputemp", self.parent)
elif (name == "hdd"):
for item in keys['hdd']:
dataEngines['system'].disconnectSource("partitions" + item + "/filllevel", self.parent)
dataEngines['system'].disconnectSource("partitions" + item + "/usedspace", self.parent)
elif (name == "hddtemp"):
dataEngines['ext'].disconnectSource("hddtemp", self.parent)
elif (name == "mem"):
dataEngines['system'].disconnectSource("mem/physical/application", self.parent)
dataEngines['system'].disconnectSource("mem/physical/free", self.parent)
dataEngines['system'].disconnectSource("mem/physical/used", self.parent)
elif (name == "net"):
dataEngines['system'].disconnectSource("network/interfaces/" + keys['net'] + "/transmitter/data", self.parent)
dataEngines['system'].disconnectSource("network/interfaces/" + keys['net'] + "/receiver/data", self.parent)
elif (name == "pkg"):
dataEngines['ext'].disconnectSource("pkg", self.parent)
elif (name == "player"):
dataEngines['ext'].disconnectSource("player", self.parent)
elif (name == "ps"):
dataEngines['ext'].disconnectSource("ps", self.parent)
elif (name == "swap"):
dataEngines['system'].disconnectSource("mem/swap/used", self.parent)
dataEngines['system'].disconnectSource("mem/swap/free", self.parent)
elif (name == "temp"):
for item in keys['temp']:
dataEngines['system'].disconnectSource(item, self.parent)
elif (name == "time"):
dataEngines['time'].disconnectSource("Local", self.parent)
elif (name == "uptime"):
dataEngines['system'].disconnectSource("system/uptime", self.parent)