Added some exception handling.
This commit is contained in:
commit
87c5064fd4
@ -1,6 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
|
from string import printable
|
||||||
|
|
||||||
from PyQt4 import QtCore, QtGui
|
from PyQt4 import QtCore, QtGui
|
||||||
from serial import Serial, SerialException
|
from serial import Serial, SerialException
|
||||||
@ -10,6 +11,16 @@ from colourterm import SettingsDialog, ConnectDialog, SComboBox, Highlight, from
|
|||||||
from colourterm.cwebview import CWebView
|
from colourterm.cwebview import CWebView
|
||||||
|
|
||||||
|
|
||||||
|
class MessageType(object):
|
||||||
|
"""
|
||||||
|
An enumeration for message types
|
||||||
|
"""
|
||||||
|
Info = 1
|
||||||
|
Question = 2
|
||||||
|
Warning = 3
|
||||||
|
Critical = 4
|
||||||
|
|
||||||
|
|
||||||
class UiMainWindow(object):
|
class UiMainWindow(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
@ -147,6 +158,7 @@ class UiMainWindow(object):
|
|||||||
|
|
||||||
class MainWindow(QtGui.QMainWindow, UiMainWindow):
|
class MainWindow(QtGui.QMainWindow, UiMainWindow):
|
||||||
updateOutput = QtCore.pyqtSignal(str)
|
updateOutput = QtCore.pyqtSignal(str)
|
||||||
|
showMessage = QtCore.pyqtSignal(str, str, int)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(MainWindow, self).__init__()
|
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.page().mainFrame().contentsSizeChanged.connect(self.on_contents_size_changed)
|
||||||
self.output_browser.onScroll.connect(self.on_output_browser_scrolled)
|
self.output_browser.onScroll.connect(self.on_output_browser_scrolled)
|
||||||
self.updateOutput.connect(self.on_update_output)
|
self.updateOutput.connect(self.on_update_output)
|
||||||
|
self.showMessage.connect(self.on_show_message)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if not self.device_closed:
|
if not self.device_closed:
|
||||||
@ -184,12 +197,17 @@ class MainWindow(QtGui.QMainWindow, UiMainWindow):
|
|||||||
return QtGui.QMainWindow.close(self)
|
return QtGui.QMainWindow.close(self)
|
||||||
|
|
||||||
def document_body(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):
|
def receive_text(self):
|
||||||
output = ''
|
output = ''
|
||||||
while not self.device_closed:
|
while not self.device_closed:
|
||||||
|
try:
|
||||||
output += self.device.read(1)
|
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'):
|
if output.endswith('\r\n'):
|
||||||
#self.terminal_lines.append(output.strip('\r\n'))
|
#self.terminal_lines.append(output.strip('\r\n'))
|
||||||
#if len(self.terminal_lines) > self.max_lines:
|
#if len(self.terminal_lines) > self.max_lines:
|
||||||
@ -306,6 +324,16 @@ class MainWindow(QtGui.QMainWindow, UiMainWindow):
|
|||||||
self.on_follow_action_toggled(True)
|
self.on_follow_action_toggled(True)
|
||||||
self.follow_action.setChecked(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):
|
def refresh_output(self):
|
||||||
elements = self.output_browser.page().mainFrame().findAllElements('div')
|
elements = self.output_browser.page().mainFrame().findAllElements('div')
|
||||||
lines = [unicode(element.toPlainText()) for element in elements]
|
lines = [unicode(element.toPlainText()) for element in elements]
|
||||||
@ -330,13 +358,22 @@ class MainWindow(QtGui.QMainWindow, UiMainWindow):
|
|||||||
break
|
break
|
||||||
if style:
|
if style:
|
||||||
try:
|
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:
|
except TypeError:
|
||||||
output = u'<div style="%s">%s</div>' % (style, output)
|
output = u'<div style="%s">%s</div>' % (style, output)
|
||||||
else:
|
else:
|
||||||
output = u'<div>%s</div>' % output
|
output = u'<div>%s</div>' % output
|
||||||
return 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):
|
def save_highlights(self, highlights):
|
||||||
settings = QtCore.QSettings()
|
settings = QtCore.QSettings()
|
||||||
settings.setValue(u'highlights/count', len(highlights))
|
settings.setValue(u'highlights/count', len(highlights))
|
||||||
|
Loading…
Reference in New Issue
Block a user