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# -- local imports -----------------------------------------------------------------------------{{{-
15import mevis
16
17# ----------------------------------------------------------------------------------------------}}}-
18
19
20# -- class ChangeSet ---------------------------------------------------------------------------{{{-
21
30 # -- member variables ------------------------------------------------------------------------{{{-
31
32 __changeDict = None
33
34 __changeStack = None
35
36 __ctx = None
37 # --------------------------------------------------------------------------------------------}}}-
38
39 # -- def __init__ ----------------------------------------------------------------------------{{{-
40
42 def __init__(self, context):
43 self.__changeDict = {}
44 self.__changeStack = []
45 self.__ctx = context
46 self.__enableAutoRevert = True
47
48 # --------------------------------------------------------------------------------------------}}}-
49
50 # -- def __del__ -----------------------------------------------------------------------------{{{-
51
53 def __del__(self):
54 self.autoRevert()
55
56 # --------------------------------------------------------------------------------------------}}}-
57
58 # -- def _logInfo ----------------------------------------------------------------------------{{{-
59 def _logInfo(self, message):
60 mevis.MLAB.log(message)
61
62 # --------------------------------------------------------------------------------------------}}}-
63
64 # -- def _logError ---------------------------------------------------------------------------{{{-
65 def _logError(self, message):
66 mevis.MLAB.logError(message)
67
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
105 # -- def addField ----------------------------------------------------------------------------{{{-
106
111 def addField(self, fieldName, verbose=False):
112 field = self.__ctx.field(fieldName)
113 if not field:
114 if verbose:
115 self._logError("Field (%s) not found in the used context!" % (fieldName))
116 return False
117 self.__changeDict.setdefault(fieldName, getFieldValue(field))
118 return True
119
120 # --------------------------------------------------------------------------------------------}}}-
121
122 # -- def autoRevert --------------------------------------------------------------------------{{{-
123
124 def autoRevert(self):
125 if self.__enableAutoRevert and self.__ctx: # the module might not exist anymore, so check
126 self.revert()
127
128 # --------------------------------------------------------------------------------------------}}}-
129
130 # -- def revert ------------------------------------------------------------------------------{{{-
131
132 def revert(self):
133 # For each changed field restore the original value.
134 while len(self.__changeStack) > 0:
135 fieldName = self.__changeStack.pop(-1)
136 field = self.__ctx.field(fieldName)
137 setFieldValue(field, self.__changeDict[fieldName])
138 self.__changeDict = {}
139
140 # --------------------------------------------------------------------------------------------}}}-
141
142 def enableAutoRevert(self, enable):
143 # Enable/disable revert on destruction. Can be disabled for debugging purposes (to leave the
144 # network state as it was on error occurrence).
145 self.__enableAutoRevert = enable
146
147 # --------------------------------------------------------------------------------------------}}}-
148
149
150# ----------------------------------------------------------------------------------------------}}}-
151
152
153def getFieldValue(field):
154 if "Vector6" in field.type:
155 return field.stringValue()
156 elif "MLBase" in field.type:
157 return field.object()
158 else:
159 return field.value
160
161
162def setFieldValue(field, fieldValue):
163 if "Vector6" in field.type:
164 if isinstance(fieldValue, (str, bytes)):
165 field.setStringValue(fieldValue)
166 else:
167 field.setVectorValue(fieldValue)
168 elif "MLBase" in field.type:
169 field.setObject(fieldValue)
170 else:
171 field.value = fieldValue
Class to handle field changes and make them revertable.
Definition ChangeSet.py:29
setFieldValue(self, fieldName, fieldValue, verbose=False)
Set the value of a field.
Definition ChangeSet.py:78
__del__(self)
The default destructor.
Definition ChangeSet.py:53
addField(self, fieldName, verbose=False)
Add a field and its value without setting it.
Definition ChangeSet.py:111
revert(self)
Revert all changes made.
Definition ChangeSet.py:132
__init__(self, context)
The default constructor.
Definition ChangeSet.py:42
autoRevert(self)
Revert all changes made if auto-revert is enabled.
Definition ChangeSet.py:124
setFieldValue(field, fieldValue)
Definition ChangeSet.py:162