forked from pyvista/pyvista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.py
93 lines (79 loc) · 2.88 KB
/
errors.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
import logging
import os
import re
import vtk
def set_error_output_file(filename):
"""Sets a file to write out the VTK errors"""
filename = os.path.abspath(os.path.expanduser(filename))
fileOutputWindow = vtk.vtkFileOutputWindow()
fileOutputWindow.SetFileName(filename)
outputWindow = vtk.vtkOutputWindow()
outputWindow.SetInstance(fileOutputWindow)
return fileOutputWindow, outputWindow
class Observer:
"""A standerd class for observing VTK objects.
"""
def __init__(self, event_type='ErrorEvent', log=True):
self.__event_occurred = False
self.__message = None
self.__message_etc = None
self.CallDataType = 'string0'
self.__observing = False
self.event_type = event_type
self.__log = log
@staticmethod
def parse_message(message):
# Message format
regex = re.compile(r'([A-Z]+):\sIn\s(.+),\sline\s.+\n\w+\s(.+):\s(.+)')
try:
kind, path, address, alert = regex.findall(message)[0]
return kind, path, address, alert
except:
return '', '', '', message
def log_message(self, kind, alert):
"""Parses different event types and passes them to logging"""
if kind == 'ERROR':
logging.error(alert)
else:
logging.warning(alert)
return
def __call__(self, obj, event, message):
"""On an event occurence, this function executes"""
self.__event_occurred = True
self.__message_etc = message
kind, path, address, alert = self.parse_message(message)
self.__message = alert
if self.__log:
self.log_message(kind, alert)
def has_event_occurred(self):
"""Ask self if an error has occured since last querried.
This resets the observer's status.
"""
occ = self.__event_occurred
self.__event_occurred = False
return occ
def get_message(self, etc=False):
"""Get the last set error message
Return:
str: the last set error message
"""
if etc:
return self.__message_etc
return self.__message
def observe(self, algorithm):
"""Make this an observer of an algorithm
"""
if self.__observing:
raise RuntimeError('This error observer is already observing an algorithm.')
if hasattr(algorithm, 'GetExecutive') and algorithm.GetExecutive() is not None:
algorithm.GetExecutive().AddObserver(self.event_type, self)
algorithm.AddObserver(self.event_type, self)
self.__observing = True
return
def send_errors_to_logging():
"""Send all VTK error/warning messages to Python's logging module"""
error_output = vtk.vtkStringOutputWindow()
error_win = vtk.vtkOutputWindow()
error_win.SetInstance(error_output)
obs = Observer()
return obs.observe(error_output)