-
Notifications
You must be signed in to change notification settings - Fork 0
/
topqt.py
executable file
·158 lines (119 loc) · 3.87 KB
/
topqt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python3
import configparser
import subprocess
import sys
import os
import time
from libtopqt import *
from threading import Thread
from PyQt4 import QtGui, uic
from PyQt4 import QtCore
from PyQt4.QtCore import Qt
import gettext
import re
window = None
columnKeyBindings = None
selectedRowIndex = None
processNameFilter = "bash"
class SettingsDialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Settings')
self.resize(300, 200)
class RefresherThread(QtCore.QThread):
refreshFrequency = None
def __init__(self, refreshFrequency):
self.refreshFrequency = refreshFrequency
QtCore.QThread.__init__(self)
def run(self):
while True:
window.tableWidget.clearContents()
for i in range(window.tableWidget.rowCount()-1, -1, -1):
try:
window.tableWidget.removeRow(i)
except Exception:
print("Här blev det fel" + i)
getAndShowProcesses()
time.sleep(self.refreshFrequency)
def setupTable(data):
global columnKeyBindings
columnKeyBindings = data
tableWidget = window.tableWidget
tableWidget.setColumnCount(len(columnKeyBindings))
for binding in columnKeyBindings:
tableWidget.setHorizontalHeaderItem(columnKeyBindings.index(binding), QtGui.QTableWidgetItem(binding["caption"]))
def insertRow(data):
global columnKeyBindings
searchIndex = -1;
matchKey = None
tableWidget = window.tableWidget
tableWidget.setSortingEnabled(False)
tableWidget.insertRow(tableWidget.rowCount())
#tableWidget.insertRow(tableWidget.rowCount())
for value in data:
searchIndex = 0
matchKey = None
for binding in columnKeyBindings:
searchIndex = searchIndex + 1
if value == binding["key"]:
matchKey = value
break
if matchKey:
tableWidget.setItem(tableWidget.rowCount()-1, searchIndex-1, QtGui.QTableWidgetItem(data[matchKey]))
#Ugly hack filter
if not re.match('.*' + processNameFilter + '.*', data["NAME"]):
print("WRONG")
tableWidget.hideRow(1)
tableWidget.setSortingEnabled(True)
def getAndShowProcesses():
processHandler = ProcessHandler()
processes = processHandler.getProcesses()
print("Found: {0} processes".format(len(processes)))
for psTuple in processes:
insertRow(psTuple)
def readConfig():
#Used for translation
#Fix translations
keyToColumnNames = {"COMMAND": "Command", "USER": "Useless"}
config = configparser.RawConfigParser()
config.read('topqt.conf')
setupTuple = []
for c in config.get('general','columns').split(","):
try:
setupTuple.append({"caption": keyToColumnNames[c], "key": c})
except:
setupTuple.append({"caption": c, "key": c})
pass
setupTable(setupTuple)
def actionQuitTriggered():
QtGui.QApplication.quit()
def actionSettingsTriggered():
settingsDialog = SettingsDialog(window)
settingsDialog.exec()
def itemSelectionChanged():
pass
#print(window.tableWidget.selectedIndexes()[0].row())
#print(window.tableWidget.selectedItems())
def main():
gettext.install('topqt')
gettext.translation('topqt', './locale', languages=['sv']).install(True)
global window
app = QtGui.QApplication(sys.argv)
if os.path.isfile("/usr/share/topqt/topqt.ui"):
window = uic.loadUi("/usr/share/topqt/topqt.ui")
elif os.path.isfile("topqt.ui"):
window = uic.loadUi("topqt.ui")
else:
print(_("Error! Couldn't find user interface file topqt.ui! Aborting!"))
sys.exit(1)
readConfig()
#setupTable([{"caption": "PID", "key": "PID"},{"caption":"Name","key":"NAME"}, {"caption":"%CPU","key":"%CPU"},{"caption":"path","key":"COMMAND"}])
t = RefresherThread(5)
t.start()
window.connect(window.actionQuit, QtCore.SIGNAL("triggered()"), actionQuitTriggered);
window.connect(window.actionSettings, QtCore.SIGNAL("triggered()"), actionSettingsTriggered);
#window.tableWidget.connect(window.tableWidget, QtCore.SIGNAL('itemSelectionChanged()'), itemSelectionChanged)
window.show();
sys.exit(app.exec_())
if __name__ == '__main__':
main()