Commit 32d0382e authored by iv's avatar iv

ERP5Workflow: PERF: avoid compilation of expression being done again and again

parent 9a61b162
...@@ -147,7 +147,7 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow): ...@@ -147,7 +147,7 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow):
# Not set yet. Use a default. # Not set yet. Use a default.
elif variable_expression is not None: elif variable_expression is not None:
ec = createExprContext(StateChangeInfo(ob, self, status)) ec = createExprContext(StateChangeInfo(ob, self, status))
value = Expression(variable_expression)(ec) value = variable_expression(ec)
else: else:
value = vdef.getVariableValue() value = vdef.getVariableValue()
...@@ -289,7 +289,7 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow): ...@@ -289,7 +289,7 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow):
else: else:
variable_expression = vdef.getVariableExpression() variable_expression = vdef.getVariableExpression()
if variable_expression is not None: if variable_expression is not None:
expression = Expression(variable_expression) expression = variable_expression
else: else:
value = vdef.getVariableValue() value = vdef.getVariableValue()
if expression is not None: if expression is not None:
......
...@@ -481,7 +481,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject): ...@@ -481,7 +481,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
# Not set yet. Use a default. # Not set yet. Use a default.
elif variable_expression is not None: elif variable_expression is not None:
ec = createExprContext(StateChangeInfo(ob, self, status)) ec = createExprContext(StateChangeInfo(ob, self, status))
value = Expression(variable_expression)(ec) value = variable_expression(ec)
else: else:
value = vdef.getVariableValue() value = vdef.getVariableValue()
...@@ -715,7 +715,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject): ...@@ -715,7 +715,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
expr = variable_expression expr = variable_expression
else: else:
value = vdef.getVariableValue(object=object) value = vdef.getVariableValue(object=object)
if expr not in(None, ''): if expr not in (None, ''):
# Evaluate an expression. # Evaluate an expression.
if econtext is None: if econtext is None:
# Lazily create the expression context. # Lazily create the expression context.
...@@ -725,7 +725,6 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject): ...@@ -725,7 +725,6 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
document, self, former_status, tdef, document, self, former_status, tdef,
old_sdef, new_sdef, kwargs) old_sdef, new_sdef, kwargs)
econtext = createExprContext(sci) econtext = createExprContext(sci)
expr = Expression(expr)
value = expr(econtext) value = expr(econtext)
status_dict[variable_reference] = value status_dict[variable_reference] = value
# Do not proceed in case of failure of before script # Do not proceed in case of failure of before script
...@@ -949,7 +948,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject): ...@@ -949,7 +948,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
for tr_var in tr_var_list: for tr_var in tr_var_list:
reference = self._getOb(tr_var.getCausalityId()).getReference() reference = self._getOb(tr_var.getCausalityId()).getReference()
transition_variable = SubElement(transition_variables, property_id, attrib=dict(id=reference,type='variable')) transition_variable = SubElement(transition_variables, property_id, attrib=dict(id=reference,type='variable'))
transition_variable.text = str(tr_var.getVariableExpression()) transition_variable.text = str(tr_var.getVariableExpressionText())
else: else:
property_value = tdef.getProperty(property_id) property_value = tdef.getProperty(property_id)
property_type = tdef.getPropertyType(property_id) property_type = tdef.getPropertyType(property_id)
...@@ -1123,7 +1122,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject): ...@@ -1123,7 +1122,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
else: else:
variable_expression = vdef.getVariableExpression() variable_expression = vdef.getVariableExpression()
if variable_expression is not None: if variable_expression is not None:
expr = Expression(variable_expression) expr = variable_expression
else: else:
value = vdef.getVariableValue() value = vdef.getVariableValue()
if expr is not None: if expr is not None:
...@@ -1183,7 +1182,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject): ...@@ -1183,7 +1182,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
elif variable_expression is not None: elif variable_expression is not None:
ec = createExprContext(StateChangeInfo(ob, self, status)) ec = createExprContext(StateChangeInfo(ob, self, status))
# convert string to expression before execute it. # convert string to expression before execute it.
value = Expression(variable_expression)(ec) value = variable_expression(ec)
else: else:
value = variable.getVariableValue() value = variable.getVariableValue()
if hasattr(self, 'getSourceValue'): if hasattr(self, 'getSourceValue'):
......
...@@ -35,7 +35,7 @@ from Products.ERP5Type.XMLObject import XMLObject ...@@ -35,7 +35,7 @@ from Products.ERP5Type.XMLObject import XMLObject
class WorkflowVariable(IdAsReferenceMixin("variable_", "prefix"), XMLObject): class WorkflowVariable(IdAsReferenceMixin("variable_", "prefix"), XMLObject):
""" """
A ERP5 Variable. A ERP5 Workflow Variable.
""" """
meta_type = 'ERP5 Variable' meta_type = 'ERP5 Variable'
...@@ -65,6 +65,15 @@ class WorkflowVariable(IdAsReferenceMixin("variable_", "prefix"), XMLObject): ...@@ -65,6 +65,15 @@ class WorkflowVariable(IdAsReferenceMixin("variable_", "prefix"), XMLObject):
PropertySheet.WorkflowVariable, PropertySheet.WorkflowVariable,
) )
def _setVariableExpression(self, expression):
if isinstance(expression, Expression):
self.variable_expression = Expression(expression.text)
else:
self.variable_expression = Expression(expression)
def getVariableExpressionText(self):
return getattr(self.variable_expression, 'text', '')
def getInfoGuard(self): def getInfoGuard(self):
if self.getGuardRoleList() is None and\ if self.getGuardRoleList() is None and\
self.getGuardPermissionList() is None and\ self.getGuardPermissionList() is None and\
...@@ -73,7 +82,7 @@ class WorkflowVariable(IdAsReferenceMixin("variable_", "prefix"), XMLObject): ...@@ -73,7 +82,7 @@ class WorkflowVariable(IdAsReferenceMixin("variable_", "prefix"), XMLObject):
self.info_guard is None: self.info_guard is None:
return Guard().__of__(self) return Guard().__of__(self)
elif self.info_guard is None: elif self.info_guard is None:
self.generateGuard() self.generateInfoGuard()
return self.info_guard return self.info_guard
def generateInfoGuard(self): def generateInfoGuard(self):
......
...@@ -177,10 +177,12 @@ class Worklist(IdAsReferenceMixin("worklist_", "prefix"), XMLObject, ...@@ -177,10 +177,12 @@ class Worklist(IdAsReferenceMixin("worklist_", "prefix"), XMLObject,
if dynamic_variable.getVariableValue(): if dynamic_variable.getVariableValue():
matches = [dynamic_variable.getVariableValue()] matches = [dynamic_variable.getVariableValue()]
# Override initial value if expression set: # Override initial value if expression set:
if dynamic_variable.getVariableExpression(): dynamic_variable_expression_text = dynamic_variable\
matches = Expression(dynamic_variable.getVariableExpression()) .getVariableExpression()
if dynamic_variable_expression_text:
matches = dynamic_variable_expression_text
if matches is not [] and matches is not None: if matches not in ([], None):
if not isinstance(matches, (tuple, Expression)): if not isinstance(matches, (tuple, Expression)):
# Old version, convert it. # Old version, convert it.
matches = tuple(matches) matches = tuple(matches)
......
...@@ -461,7 +461,7 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool): ...@@ -461,7 +461,7 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool):
worklist_variable_value = worklist.newContent(portal_type='Worklist Variable', worklist_variable_value = worklist.newContent(portal_type='Worklist Variable',
reference=key) reference=key)
if isinstance(values, Expression): if isinstance(values, Expression):
worklist_variable_value.setVariableExpression(values.text) worklist_variable_value.setVariableExpression(values)
else: else:
worklist_variable_value.setVariableValue(values[0]) #XXX(WORKFLOW): to be changed worklist_variable_value.setVariableValue(values[0]) #XXX(WORKFLOW): to be changed
...@@ -537,9 +537,9 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool): ...@@ -537,9 +537,9 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool):
# for a very specific case, action return the reference of transition # for a very specific case, action return the reference of transition
# in order to generation correct workflow history. # in order to generation correct workflow history.
if variable_id == 'action': if variable_id == 'action':
variable.setVariableExpression('transition/getReference|nothing') variable.setVariableExpression(Expression('transition/getReference|nothing'))
else: else:
variable.setVariableExpression(variable_definition.default_expr.text) variable.setVariableExpression(variable_definition.default_expr)
if variable_definition.info_guard: if variable_definition.info_guard:
variable.info_guard = variable_definition.info_guard variable.info_guard = variable_definition.info_guard
variable.setGuardRoleList(variable_definition.info_guard.roles) variable.setGuardRoleList(variable_definition.info_guard.roles)
...@@ -565,7 +565,7 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool): ...@@ -565,7 +565,7 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool):
else: var_exprs = origin_tdef.var_exprs else: var_exprs = origin_tdef.var_exprs
for key in var_exprs: for key in var_exprs:
tr_var = transition.newContent(portal_type='Transition Variable', temp_object=is_temporary) tr_var = transition.newContent(portal_type='Transition Variable', temp_object=is_temporary)
tr_var.setVariableExpression(var_exprs[key].text) tr_var.setVariableExpression(var_exprs[key])
tr_var_path = getattr(workflow, 'variable_'+key).getPath() tr_var_path = getattr(workflow, 'variable_'+key).getPath()
tr_var_path = '/'.join(tr_var_path.split('/')[2:]) tr_var_path = '/'.join(tr_var_path.split('/')[2:])
new_category.append(tr_var_path) new_category.append(tr_var_path)
...@@ -582,7 +582,7 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool): ...@@ -582,7 +582,7 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool):
else: var_exprs = origin_tdef.var_exprs else: var_exprs = origin_tdef.var_exprs
for key in var_exprs: for key in var_exprs:
tr_var = interaction.newContent(portal_type='Transition Variable', temp_object=is_temporary) tr_var = interaction.newContent(portal_type='Transition Variable', temp_object=is_temporary)
tr_var.setVariableExpression(var_exprs[key].text) tr_var.setVariableExpression(var_exprs[key])
tr_var_path = getattr(workflow, 'variable_'+key).getPath() tr_var_path = getattr(workflow, 'variable_'+key).getPath()
tr_var_path = '/'.join(tr_var_path.split('/')[2:]) tr_var_path = '/'.join(tr_var_path.split('/')[2:])
new_category.append(tr_var_path) new_category.append(tr_var_path)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment