More cleanup, logs.py ok
[cadence-nykeej.git] / src / logs.py
blob98c157f547c9d35824f69a1f3344999b235e8a6c
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Imports (Global)
5 import os
6 from PyQt4.QtCore import Qt, QFile, QIODevice, QTextStream, QTimer, SIGNAL
7 from PyQt4.QtGui import QDialog, QIcon, QPalette, QSyntaxHighlighter
9 # Imports (Custom Stuff)
10 import icons_rc, ui_logs
12 # Get Icon from user theme, using our own as backup (Oxygen)
13 def getIcon(icon, size=16):
14 return QIcon.fromTheme(icon, QIcon(":/%ix%i/.png" % (size,size)))
16 # Jack Syntax Highlighter
17 class JackSyntaxH(QSyntaxHighlighter):
18 def __init__(self, parent=None):
19 super(JackSyntaxH, self).__init__(parent)
21 self.palette = self.parent().palette()
23 def highlightBlock(self, text):
24 if ("ERROR: " in text):
25 self.setFormat(text.indexOf("ERROR: "), text.count(), Qt.red)
26 elif ("WARNING: " in text):
27 self.setFormat(text.indexOf("WARNING: "), text.count(), Qt.darkRed)
28 elif ("------------------" in text):
29 self.setFormat(text.indexOf("------------------"), text.count(), self.palette.color(QPalette.Active, QPalette.Mid))
30 elif ("Connecting " in text):
31 self.setFormat(text.indexOf("Connecting "), text.count(), self.palette.color(QPalette.Active, QPalette.Link))
32 elif ("Disconnecting " in text):
33 self.setFormat(text.indexOf("Disconnecting "), text.count(), self.palette.color(QPalette.Active, QPalette.LinkVisited))
35 # LADISH Syntax Highlighter
36 class LadishSyntaxH(QSyntaxHighlighter):
37 def __init__(self, parent=None):
38 super(LadishSyntaxH, self).__init__(parent)
40 self.palette = self.parent().palette()
42 def highlightBlock(self, text):
43 if ("ERROR: " in text):
44 self.setFormat(text.indexOf("ERROR: "), text.count(), Qt.red)
45 elif ("WARNING: " in text):
46 self.setFormat(text.indexOf("WARNING: "), text.count(), Qt.darkRed)
47 elif ("------------------" in text):
48 self.setFormat(text.indexOf("------------------"), text.count(), self.palette.color(QPalette.Active, QPalette.Mid))
49 #elif ("Connecting " in text):
50 #self.setFormat(text.indexOf("Connecting "), text.count(), self.palette.color(QPalette.Active, QPalette.Link))
51 #elif ("Disconnecting " in text):
52 #self.setFormat(text.indexOf("Disconnecting "), text.count(), self.palette.color(QPalette.Active, QPalette.LinkVisited))
54 # A2J Syntax Highlighter
55 class A2JSyntaxH(QSyntaxHighlighter):
56 def __init__(self, parent=None):
57 super(A2JSyntaxH, self).__init__(parent)
59 self.palette = self.parent().palette()
61 def highlightBlock(self, text):
62 if ("error: " in text):
63 self.setFormat(text.indexOf("error: "), text.count(), Qt.red)
64 elif ("WARNING: " in text):
65 self.setFormat(text.indexOf("WARNING: "), text.count(), Qt.darkRed)
66 elif ("----------------------------" in text):
67 self.setFormat(text.indexOf("----------------------------"), text.count(), self.palette.color(QPalette.Active, QPalette.Mid))
68 elif ("port created: " in text):
69 self.setFormat(text.indexOf("port created: "), text.count(), self.palette.color(QPalette.Active, QPalette.Link))
70 elif ("port deleted: " in text):
71 self.setFormat(text.indexOf("port deleted: "), text.count(), self.palette.color(QPalette.Active, QPalette.LinkVisited))
74 # Render Window
75 class LogsW(QDialog, ui_logs.Ui_LogsW):
76 def __init__(self, parent=None):
77 super(LogsW, self).__init__(parent)
78 self.setupUi(self)
80 self.b_close.setIcon(getIcon("dialog-close"))
81 self.b_purge.setIcon(getIcon("edit-delete"))
83 self.timer = QTimer()
84 self.timer.setInterval(1000)
85 self.timer.start()
87 self.jack_syh = JackSyntaxH(self.pte_jack)
88 self.jack_syh.setDocument(self.pte_jack.document())
90 self.ladish_syh = LadishSyntaxH(self.pte_ladish)
91 self.ladish_syh.setDocument(self.pte_ladish.document())
93 self.a2j_syh = A2JSyntaxH(self.pte_a2j)
94 self.a2j_syh.setDocument(self.pte_a2j.document())
96 self.connect(self.timer, SIGNAL("timeout()"), self.updateLogs)
97 self.connect(self.b_purge, SIGNAL("clicked()"), self.purgeLogs)
99 self.initLogs()
100 self.updateLogs()
102 def initLogs(self):
103 HOME = os.getenv("HOME")
105 self.log_jack = {
106 'valid': False,
107 'file': QFile(os.path.join(HOME, ".log", "jack", "jackdbus.log")),
108 'stream': None,
109 'last_line': 0
112 self.log_ladish = {
113 'valid': False,
114 'file': QFile(os.path.join(HOME, ".log", "ladish", "ladish.log")),
115 'stream': None,
116 'last_line': 0
119 self.log_a2j = {
120 'valid': False,
121 'file': QFile(os.path.join(HOME, ".log", "a2j", "a2j.log")),
122 'stream': None,
123 'last_line': 0
126 if (self.log_jack['file'].exists()):
127 self.log_jack['valid'] = True
128 self.log_jack['file'].open(QIODevice.ReadOnly)
129 self.log_jack['stream'] = QTextStream(self.log_jack['file'])
130 self.log_jack['stream'].setCodec("UTF-8")
132 if (self.log_ladish['file'].exists()):
133 self.log_ladish['valid'] = True
134 self.log_ladish['file'].open(QIODevice.ReadOnly)
135 self.log_ladish['stream'] = QTextStream(self.log_ladish['file'])
136 self.log_ladish['stream'].setCodec("UTF-8")
138 if (self.log_a2j['file'].exists()):
139 self.log_a2j['valid'] = True
140 self.log_a2j['file'].open(QIODevice.ReadOnly)
141 self.log_a2j['stream'] = QTextStream(self.log_a2j['file'])
142 self.log_a2j['stream'].setCodec("UTF-8")
144 def purgeLogs(self):
145 self.timer.stop()
147 if (self.log_jack['valid']):
148 self.log_jack['stream'].flush()
149 self.pte_jack.clear()
150 self.log_jack['file'].close()
151 self.log_jack['file'].open(QIODevice.WriteOnly)
152 self.log_jack['file'].close()
154 if (self.log_ladish['valid']):
155 self.log_ladish['stream'].flush()
156 self.pte_ladish.clear()
157 self.log_ladish['file'].close()
158 self.log_ladish['file'].open(QIODevice.WriteOnly)
159 self.log_ladish['file'].close()
161 if (self.log_a2j['valid']):
162 self.log_a2j['stream'].flush()
163 self.pte_a2j.clear()
164 self.log_a2j['file'].close()
165 self.log_a2j['file'].open(QIODevice.WriteOnly)
166 self.log_a2j['file'].close()
168 self.initLogs()
169 self.timer.start()
171 def updateLogs(self):
172 if (self.log_jack['valid']):
173 text = self.log_jack['stream'].readAll().replace("\e[1m\e[31m","").replace("\e[1m\e[33m","").replace("\e[0m","")
174 text.remove(text.count()-1, text.count())
175 if not text.isEmpty():
176 self.pte_jack.appendPlainText(text)
178 if (self.log_ladish['valid']):
179 text = self.log_ladish['stream'].readAll().replace("\e[31m","").replace("\e[33m","").replace("\e[0m","")
180 text.remove(text.count()-1, text.count())
181 if not text.isEmpty():
182 self.pte_ladish.appendPlainText(text)
184 if (self.log_a2j['valid']):
185 text = self.log_a2j['stream'].readAll().replace("\e[31m","").replace("\e[33m","").replace("\e[0m","")
186 text.remove(text.count()-1, text.count())
187 if not text.isEmpty():
188 self.pte_a2j.appendPlainText(text)
190 def closeEvent(self, event):
191 self.timer.stop()
193 if (self.log_jack['valid']):
194 self.log_jack['file'].close()
196 if (self.log_ladish['valid']):
197 self.log_ladish['file'].close()
199 if (self.log_a2j['valid']):
200 self.log_a2j['file'].close()
202 return QDialog.closeEvent(self, event)
205 # Allow to use this as a standalone app
206 if __name__ == '__main__':
208 # Additional imports
209 import sys
210 from PyQt4.QtGui import QApplication
212 # App initialization
213 app = QApplication(sys.argv)
215 # Show GUI
216 gui = LogsW()
217 gui.show()
219 # App-Loop
220 sys.exit(app.exec_())