TestCenter Reference
ChangeSet.py
Go to the documentation of this file.
2# Copyright 2009, MeVis Medical Solutions AG
3#
4# The user may use this file in accordance with the license agreement provided with
5# the Software or, alternatively, in accordance with the terms contained in a
6# written agreement between the user and MeVis Medical Solutions AG.
7#
8# For further information use the contact form at https://www.mevislab.de/contact
9#
10
13
14# -- system imports ----------------------------------------------------------------------------{{{-
15import os
16import sys
17import traceback
18# ----------------------------------------------------------------------------------------------}}}-
19
20# -- local imports -----------------------------------------------------------------------------{{{-
21import mevis
22# ----------------------------------------------------------------------------------------------}}}-
23
24# -- class ChangeSet ---------------------------------------------------------------------------{{{-
25
33class ChangeSet :
34 # -- member variables ------------------------------------------------------------------------{{{-
35
36 __changeDict = None
37
38 __changeStack = None
39
40 __ctx = None
41 # --------------------------------------------------------------------------------------------}}}-
42
43 # -- def __init__ ----------------------------------------------------------------------------{{{-
44
46 def __init__ (self, context):
47 self.__changeDict = {}
48 self.__changeStack = []
49 self.__ctx = context
50 self.__enableAutoRevert = True
51 # --------------------------------------------------------------------------------------------}}}-
52
53 # -- def __del__ -----------------------------------------------------------------------------{{{-
54
56 def __del__ (self):
57 self.autoRevert()
58 # --------------------------------------------------------------------------------------------}}}-
59
60 # -- def _logInfo ----------------------------------------------------------------------------{{{-
61 def _logInfo (self, message):
62 mevis.MLAB.log(message)
63 # --------------------------------------------------------------------------------------------}}}-
64
65 # -- def _logError ---------------------------------------------------------------------------{{{-
66 def _logError (self, message):
67 mevis.MLAB.logError(message)
68 # --------------------------------------------------------------------------------------------}}}-
69
70 # -- def setFieldValue -----------------------------------------------------------------------{{{-
71
78 def setFieldValue (self, fieldName, fieldValue, verbose=False):
79 retVal = True
80 field = self.__ctx.field(fieldName)
81 if not field:
82 if verbose:
83 self._logError("Field (%s) not found in the used context!" % (fieldName))
84 retVal = False
85 else:
86 fieldType = field.type
87 if (fieldType in ('Trigger')) or fieldValue is None:
88 if verbose:
89 self._logInfo("Touched field %s" % (fieldName))
90 field.touch()
91 else:
92 # Store old field value.
93 oldFieldValue = getFieldValue(field)
94 if fieldName not in self.__changeDict:
95 self.__changeDict[fieldName] = oldFieldValue
96 self.__changeStack.append(fieldName)
97 self.__changeDict.setdefault(fieldName, oldFieldValue)
98 setFieldValue(field, fieldValue)
99 if verbose:
100 self._logInfo("Set field %s to value %s" % (fieldName, fieldValue))
101 return retVal
102 # --------------------------------------------------------------------------------------------}}}-
103
104 # -- def addField ----------------------------------------------------------------------------{{{-
105
110 def addField (self, fieldName, verbose=False):
111 field = self.__ctx.field(fieldName)
112 if not field:
113 if verbose:
114 self._logError("Field (%s) not found in the used context!" % (fieldName))
115 return False
116 self.__changeDict.setdefault(fieldName, getFieldValue(field))
117 return True
118 # --------------------------------------------------------------------------------------------}}}-
119
120 # -- def autoRevert --------------------------------------------------------------------------{{{-
121
122 def autoRevert (self):
123 if self.__enableAutoRevert and self.__ctx: # the module might not exist anymore, so check
124 self.revert()
125 # --------------------------------------------------------------------------------------------}}}-
126
127 # -- def revert ------------------------------------------------------------------------------{{{-
128
129 def revert (self):
130 # For each changed field restore the original value.
131 while len(self.__changeStack) > 0:
132 fieldName = self.__changeStack.pop(-1)
133 field = self.__ctx.field(fieldName)
134 setFieldValue(field, self.__changeDict[fieldName])
135 self.__changeDict = {}
136 # --------------------------------------------------------------------------------------------}}}-
137
138 def enableAutoRevert (self, enable ):
139 # Enable/disable revert on destruction. Can be disabled for debugging purposes (to leave the
140 # network state as it was on error occurrence).
141 self.__enableAutoRevert = enable
142 # --------------------------------------------------------------------------------------------}}}-
143# ----------------------------------------------------------------------------------------------}}}-
144
145def getFieldValue(field):
146 if 'Vector6' in field.type:
147 return field.stringValue()
148 elif 'MLBase' in field.type:
149 return field.object()
150 else:
151 return field.value
152
153def setFieldValue(field, fieldValue):
154 if 'Vector6' in field.type:
155 if isinstance(fieldValue, (str, bytes)):
156 field.setStringValue(fieldValue)
157 else:
158 field.setVectorValue(fieldValue)
159 elif 'MLBase' in field.type:
160 field.setObject(fieldValue)
161 else:
162 field.value = fieldValue
Class to handle field changes and make them revertable.
Definition ChangeSet.py:33
setFieldValue(self, fieldName, fieldValue, verbose=False)
Set the value of a field.
Definition ChangeSet.py:78
__del__(self)
The default destructor.
Definition ChangeSet.py:56
addField(self, fieldName, verbose=False)
Add a field and its value without setting it.
Definition ChangeSet.py:110
revert(self)
Revert all changes made.
Definition ChangeSet.py:129
__init__(self, context)
The default constructor.
Definition ChangeSet.py:46
autoRevert(self)
Revert all changes made if auto-revert is enabled.
Definition ChangeSet.py:122
setFieldValue(field, fieldValue)
Definition ChangeSet.py:153