TestCenter Reference
ChangeSet.py
Go to the documentation of this file.
1 #
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 
11 
13 
14 # -- local imports -----------------------------------------------------------------------------{{{-
15 import mevis
16 
17 # ----------------------------------------------------------------------------------------------}}}-
18 
19 
20 # -- class ChangeSet ---------------------------------------------------------------------------{{{-
21 
29 class ChangeSet:
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__changeDict = {}
44  self.__changeStack__changeStack = []
45  self.__ctx__ctx = context
46  self.__enableAutoRevert__enableAutoRevert = True
47 
48  # --------------------------------------------------------------------------------------------}}}-
49 
50  # -- def __del__ -----------------------------------------------------------------------------{{{-
51 
53  def __del__(self):
54  self.autoRevertautoRevert()
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__ctx.field(fieldName)
81  if not field:
82  if verbose:
83  self._logError_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_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__changeDict:
95  self.__changeDict__changeDict[fieldName] = oldFieldValue
96  self.__changeStack__changeStack.append(fieldName)
97  self.__changeDict__changeDict.setdefault(fieldName, oldFieldValue)
98  setFieldValue(field, fieldValue)
99  if verbose:
100  self._logInfo_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__ctx.field(fieldName)
113  if not field:
114  if verbose:
115  self._logError_logError("Field (%s) not found in the used context!" % (fieldName))
116  return False
117  self.__changeDict__changeDict.setdefault(fieldName, getFieldValue(field))
118  return True
119 
120  # --------------------------------------------------------------------------------------------}}}-
121 
122  # -- def autoRevert --------------------------------------------------------------------------{{{-
123 
124  def autoRevert(self):
125  if self.__enableAutoRevert__enableAutoRevert and self.__ctx__ctx: # the module might not exist anymore, so check
126  self.revertrevert()
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__changeStack) > 0:
135  fieldName = self.__changeStack__changeStack.pop(-1)
136  field = self.__ctx__ctx.field(fieldName)
137  setFieldValue(field, self.__changeDict__changeDict[fieldName])
138  self.__changeDict__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__enableAutoRevert = enable
146 
147  # --------------------------------------------------------------------------------------------}}}-
148 
149 
150 # ----------------------------------------------------------------------------------------------}}}-
151 
152 
153 def 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 
162 def 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
def revert(self)
Revert all changes made.
Definition: ChangeSet.py:132
def _logError(self, message)
Definition: ChangeSet.py:65
def __init__(self, context)
The default constructor.
Definition: ChangeSet.py:42
def enableAutoRevert(self, enable)
Definition: ChangeSet.py:142
def _logInfo(self, message)
Definition: ChangeSet.py:59
def __del__(self)
The default destructor.
Definition: ChangeSet.py:53
def addField(self, fieldName, verbose=False)
Add a field and its value without setting it.
Definition: ChangeSet.py:111
def autoRevert(self)
Revert all changes made if auto-revert is enabled.
Definition: ChangeSet.py:124
def setFieldValue(self, fieldName, fieldValue, verbose=False)
Set the value of a field.
Definition: ChangeSet.py:78
def getFieldValue(field)
Definition: ChangeSet.py:153
def setFieldValue(field, fieldValue)
Definition: ChangeSet.py:162