Added some exception handling.

This commit is contained in:
Raoul Snyman 2014-01-16 14:48:29 +02:00
commit 87c5064fd4

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import os
import threading
from string import printable
from PyQt4 import QtCore, QtGui
from serial import Serial, SerialException
@ -10,6 +11,16 @@ from colourterm import SettingsDialog, ConnectDialog, SComboBox, Highlight, from
from colourterm.cwebview import CWebView
class MessageType(object):
"""
An enumeration for message types
"""
Info = 1
Question = 2
Warning = 3
Critical = 4
class UiMainWindow(object):
def __init__(self):
"""
@ -147,6 +158,7 @@ class UiMainWindow(object):
class MainWindow(QtGui.QMainWindow, UiMainWindow):
updateOutput = QtCore.pyqtSignal(str)
showMessage = QtCore.pyqtSignal(str, str, int)
def __init__(self):
super(MainWindow, self).__init__()
@ -174,6 +186,7 @@ class MainWindow(QtGui.QMainWindow, UiMainWindow):
self.output_browser.page().mainFrame().contentsSizeChanged.connect(self.on_contents_size_changed)
self.output_browser.onScroll.connect(self.on_output_browser_scrolled)
self.updateOutput.connect(self.on_update_output)
self.showMessage.connect(self.on_show_message)
def close(self):
if not self.device_closed:
@ -184,12 +197,17 @@ class MainWindow(QtGui.QMainWindow, UiMainWindow):
return QtGui.QMainWindow.close(self)
def document_body(self):
return self.output_browser.page().mainFrame().documentElement().findFirst('pre')
return self.output_browser.page().mainFrame().documentElement().findFirst(u'pre')
def receive_text(self):
output = ''
while not self.device_closed:
try:
output += self.device.read(1)
except SerialException as e:
self.showMessage.emit(u'Port Error', u'Error reading from serial port: %s' % e, MessageType.Critical)
self.on_close_action_triggered()
continue
if output.endswith('\r\n'):
#self.terminal_lines.append(output.strip('\r\n'))
#if len(self.terminal_lines) > self.max_lines:
@ -306,6 +324,16 @@ class MainWindow(QtGui.QMainWindow, UiMainWindow):
self.on_follow_action_toggled(True)
self.follow_action.setChecked(True)
def on_show_message(self, title, message, type_=MessageType.Info):
if type_ == MessageType.Info:
QtGui.QMessageBox.information(self, title, message)
elif type_ == MessageType.Question:
QtGui.QMessageBox.question(self, title, message)
elif type_ == MessageType.Warning:
QtGui.QMessageBox.warning(self, title, message)
elif type_ == MessageType.Critical:
QtGui.QMessageBox.critical(self, title, message)
def refresh_output(self):
elements = self.output_browser.page().mainFrame().findAllElements('div')
lines = [unicode(element.toPlainText()) for element in elements]
@ -330,13 +358,22 @@ class MainWindow(QtGui.QMainWindow, UiMainWindow):
break
if style:
try:
output = u'<div style="%s">%s</div>' % (style, unicode(output, u'utf-8'))
output = u'<div style="%s">%s</div>' % (style, self.filter_printable(output))
#output = u'<div style="%s">%s</div>' % (style, unicode(output, u'utf-8'))
except TypeError:
output = u'<div style="%s">%s</div>' % (style, output)
else:
output = u'<div>%s</div>' % output
return output
def filter_printable(self, output):
printable_output = u''
for char in output:
if char not in printable:
printable_output += u'\\x{:02x}'.format(ord(char))
else:
printable_output += unicode(char, u'utf8')
def save_highlights(self, highlights):
settings = QtCore.QSettings()
settings.setValue(u'highlights/count', len(highlights))