awesome-widgets/source/contents/code/main.py
arcan1s 98458dfcf3 Pre-release 1.3.3b:
+ added select temperature device
2013-05-29 18:00:20 +04:00

187 lines
6.8 KiB
Python

# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.kdecore import *
from PyKDE4.kdeui import *
from PyKDE4.kio import *
from PyKDE4 import plasmascript
from PyKDE4.plasma import Plasma
import commands, os, time
import configdef
import configwindow
import dataengine
import reinit
from util import *
class pyTextWidget(plasmascript.Applet):
def __init__(self, parent, args=None):
"""widget definition"""
plasmascript.Applet.__init__(self, parent)
def init(self):
"""function to initializate widget"""
self._name = str(self.package().metadata().pluginName())
self.initTooltip()
self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)
self.dataengine = dataengine.DataEngine(self)
self.reinit = reinit.Reinit(self)
self.timer = QTimer()
QObject.connect(self.timer, SIGNAL("timeout()"), self.updateLabel)
self.setupVar()
self.reinit.reinit()
self.setHasConfigurationInterface(True)
def createConfigurationInterface(self, parent):
"""function to setup configuration window"""
self.configpage = configwindow.ConfigWindow(self)
self.configdef = configdef.ConfigDefinition(self, self.configpage)
self.configdef.createConfigurationInterface(parent)
def initTooltip(self):
"""function to create tooltip"""
self.tooltip = Plasma.ToolTipContent()
self.tooltip.setMainText("PyTetMonitor")
self.tooltip.setSubText('')
Plasma.ToolTipManager.self().registerWidget(self.applet)
# show tooltip
#Plasma.ToolTipManager.self().setContent(self.applet, self.tooltip)
def setupNetdev(self):
"""function to setup network device"""
if (self.num_dev == 0):
for line in commands.getoutput("ifconfig -a").split("\n"):
if (line != ''):
if ((line[0] != ' ') and (line[0:3] != "lo:")):
self.netdev = line.split()[0][:-1]
else:
interfaces = []
for line in commands.getoutput("ifconfig -a").split("\n"):
if (line != ''):
if ((line[0] != ' ') and (line[0:3] != "lo:")):
interfaces.append(line.split()[0][:-1])
command_line = "if ! (ifconfig "+ interfaces[1] + " | grep 'inet ' > /dev/null); then "
command_line = command_line + "if ! (ifconfig "+ interfaces[0] + " | grep 'inet ' > /dev/null); then echo lo; "
command_line = command_line + "else echo "+ interfaces[0] + "; fi; else echo "+ interfaces[1] + "; fi"
self.netdev = commands.getoutput(command_line)
def setupVar(self):
"""function to setup variables"""
self.netdev = ''
# setup number of cores
commandOut = commands.getoutput("grep -c '^processor' /proc/cpuinfo")
self.numCores = int(commandOut)
# create dictionaries
self.dict_orders = {'6':'bat', '1':'cpu', '7':'cpuclock', '9':'gpu', 'a':'gputemp',
'b':'hdd', 'c':'hddtemp', '3':'mem', '5':'net', '4':'swap', '2':'temp', '8':'uptime'}
self.dict_defFormat = {'bat':'[bat: $bat%$ac]', 'cpu':'[cpu: $cpu%]',
'cpuclock':'[mhz: $cpucl]', 'gpu':'[gpu: $gpu%]',
'gputemp':'[gpu temp: $gputemp°C]', 'hdd':'[hdd: @@/@@%]',
'hddtemp':'[hdd temp: @@/dev/sda@@°C]', 'mem':'[mem: $mem%]',
'net':'[$netdev: $netKB/s]', 'swap':'[swap: $swap%]',
'temp':'[temp: $temp°C]', 'uptime':'[uptime: $uptime]'}
def showConfigurationInterface(self):
"""function to show configuration window"""
plasmascript.Applet.showConfigurationInterface(self)
def startPolling(self):
try:
self.timer.start()
self.updateLabel()
self.tooltip.setSubText('')
except Exception as (strerror):
self.tooltip.setSubText(str(strerror))
self.label_error = Plasma.Label(self.applet)
self.label_error.setText('<font color="red">ERROR</font>')
self.layout.addItem(self.label_error)
return
def updateLabel(self):
"""function to update label"""
if ((self.memBool == 1) and (self.memInMb == False)):
self.memText()
if ((self.swapBool == 1) and (self.swapInMb == False)):
self.swapText()
if (self.batBool == 1):
self.batText()
def batText(self):
"""function to set battery text"""
line = self.batFormat
if (line.split('$bat')[0] != line):
if os.path.exists(self.battery_device):
with open (self.battery_device, 'r') as bat_file:
bat = bat_file.readline().split('\n')[0]
else:
bat = 'off'
bat = "%3s" % (bat)
line = line.split('$bat')[0] + bat + line.split('$bat')[1]
if (line.split('$ac')[0] != line):
if os.path.exists(self.ac_device):
with open (self.ac_device, 'r') as bat_file:
bat = bat_file.readline().split('\n')[0]
if (bat == '1'):
bat = '(*)'
else:
bat = '( )'
else:
bat = '(?)'
line = line.split('$ac')[0] + bat + line.split('$ac')[1]
text = self.formatLine.split('$LINE')[0] + line + self.formatLine.split('$LINE')[1]
self.label_bat.setText(text)
def memText(self):
"""function to set mem text"""
full = self.mem_uf + self.mem_free
mem = 100 * self.mem_used / full
mem = "%5s" % (str(round(mem, 1)))
if (self.memFormat.split('$mem')[0] != self.memFormat):
line = self.memFormat.split('$mem')[0] + mem + self.memFormat.split('$mem')[1]
else:
line = self.memFormat
text = self.formatLine.split('$LINE')[0] + line + self.formatLine.split('$LINE')[1]
self.label_mem.setText(text)
def swapText(self):
"""function to set swap text"""
full = self.swap_used + self.swap_free
mem = 100 * self.swap_used / full
mem = "%5s" % (str(round(mem, 1)))
if (self.swapFormat.split('$swap')[0] != self.swapFormat):
line = self.swapFormat.split('$swap')[0] + mem + self.swapFormat.split('$swap')[1]
else:
line = self.swapFormat
text = self.formatLine.split('$LINE')[0] + line + self.formatLine.split('$LINE')[1]
self.label_swap.setText(text)
@pyqtSignature("dataUpdated(const QString &, const Plasma::DataEngine::Data &)")
def dataUpdated(self, sourceName, data):
"""function to update label"""
self.dataengine.dataUpdated(sourceName, data)
def CreateApplet(parent):
return pyTextWidget(parent)