Skip to content

Commit

Permalink
Exceptionhandling for GUI applications
Browse files Browse the repository at this point in the history
  • Loading branch information
bklebel committed Jul 4, 2019
1 parent 9ea7f06 commit 2faa516
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
11 changes: 11 additions & 0 deletions measureSequences/Sequence_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import threading

from .util import Window_ui
from .util import ExceptionHandling
from .Sequence_parsing import Sequence_parser

from .qlistmodel import SequenceListModel
Expand Down Expand Up @@ -279,12 +280,14 @@ class Sequence_builder(Window_ui, Sequence_parser):

sig_runSequence = pyqtSignal(list)
sig_abortSequence = pyqtSignal()
sig_assertion = pyqtSignal(str)

def __init__(self, parent=None, **kwargs):
super().__init__(
ui_file=pkg_resources.resource_filename(__name__, "configurations\\sequence.ui"), **kwargs)

# self.listSequence.sig_dropped.connect(lambda value: self.dropreact(value))
self.__name__ = 'Sequence_builder'

QTimer.singleShot(0, self.initialize_all_windows)
# QTimer.singleShot(
Expand Down Expand Up @@ -317,10 +320,12 @@ def __init__(self, parent=None, **kwargs):
def init_data(self):
self.data = []

@ExceptionHandling
def running_sequence(self):
self.data = self.model.pass_data()
self.sig_runSequence.emit(deepcopy(self.data))

@ExceptionHandling
def addItem_toSequence(self, text):
"""
depending on the Item clicked, add the correct Item to the model,
Expand Down Expand Up @@ -354,6 +359,7 @@ def addItem_toSequence(self, text):
if text.text(0) == 'Shutdown Temperature Control':
raise NotImplementedError

@ExceptionHandling
def addWaiting(self, data):
string = self.displaytext_waiting(data)
data.update(dict(DisplayText=string))
Expand All @@ -362,6 +368,7 @@ def addWaiting(self, data):
QTimer.singleShot(1, lambda: self.listSequence.repaint())
# QTimer.singleShot(10, self.model.)

@ExceptionHandling
def addTscan(self, data):
string = self.displaytext_scan_T(data)
data.update(dict(DisplayText=string))
Expand All @@ -381,6 +388,7 @@ def printing(self, data):
# with open(self.sequence_file_json, 'w') as output:
# output.write(json.dumps(self.data))

@ExceptionHandling
def initialize_all_windows(self):
self.initialise_window_waiting()
self.initialise_window_Tscan()
Expand All @@ -401,6 +409,7 @@ def initialise_window_ChangeDataFile(self):
self.Window_ChangeDataFile.sig_accept.connect(
lambda value: self.addChangeDataFile(value))

@ExceptionHandling
def window_FileDialogSave(self):
self.sequence_file_json, __ = QtWidgets.QFileDialog.getSaveFileName(self, 'Save As',
'c:\\', "Serialised (*.json)")
Expand All @@ -409,12 +418,14 @@ def window_FileDialogSave(self):
self.sequence_file_p = self.sequence_file_json[:-4] + 'pkl'
# self.sequence_file_json = self.sequence_file[:-3] + 'json'

# @ExceptionHandling
def window_FileDialogOpen(self):
self.sequence_file, __ = QtWidgets.QFileDialog.getOpenFileName(self, 'Save As',
'c:\\', "Sequence files (*.seq)")
self.lineFileLocation.setText(self.sequence_file)
self.initialize_sequence(self.sequence_file)

@ExceptionHandling
def initialize_sequence(self, sequence_file):
"""build & run the sequence parsing, add items to the display model"""
super().initialize_sequence(sequence_file)
Expand Down
46 changes: 46 additions & 0 deletions measureSequences/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,52 @@
from PyQt5 import QtWidgets
from PyQt5.uic import loadUi

import functools
import inspect


def ExceptionSignal(thread, func, e_type, err):
"""Emit assertion-signal with relevant information"""
thread.sig_assertion.emit('{}: {}: {}: {}'.format(
thread.__name__,
func.__name__,
e_type,
err.args[0]))


def ExceptionHandling(func):
@functools.wraps(func)
def wrapper_ExceptionHandling(*args, **kwargs):
if inspect.isclass(type(args[0])):
try:
return func(*args, **kwargs)
except AssertionError as e_ass:
ExceptionSignal(args[0], func, 'Assertion', e_ass)

except TypeError as e_type:
ExceptionSignal(args[0], func, 'Type', e_type)

except KeyError as e_key:
ExceptionSignal(args[0], func, 'Key', e_key)

except ValueError as e_val:
ExceptionSignal(args[0], func, 'Value', e_val)

except AttributeError as e_attr:
ExceptionSignal(args[0], func, 'Attribute', e_attr)

except NotImplementedError as e_implement:
ExceptionSignal(args[0], func, 'NotImplemented', e_implement)

except OSError as e:
ExceptionSignal(args[0], func, 'OSError', e)

except IndexError as e:
ExceptionSignal(args[0], func, 'IndexError', e)
else:
print('There is a bug!! ' + func.__name__)
return wrapper_ExceptionHandling


def ScanningN(start, end, N):
"""utility function for building linspaced number-sequences"""
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy
PyQt5
PyQt5
functools
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
download_url='https://github.com/bklebel/measureSequences/archive/0.1.6.tar.gz',
# packages=setuptools.find_packages(),
packages=['measureSequences'],
install_requires=['numpy', 'PyQt5'],
install_requires=['numpy', 'PyQt5', 'functools'],
include_package_data=True,
classifiers=[
"Programming Language :: Python :: 3",
Expand Down

0 comments on commit 2faa516

Please sign in to comment.