2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
2013-06-25 05:04:23 -05:00
|
|
|
# This file is part of Appy, a framework for building applications in the Python
|
|
|
|
# language. Copyright (C) 2007 Gaetan Delannay
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
# Appy is free software; you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
# Appy is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Appy. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2012-03-16 08:59:59 -05:00
|
|
|
from appy import Object
|
2009-06-29 07:06:01 -05:00
|
|
|
from appy.pod import PodError
|
|
|
|
from appy.pod.elements import *
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2013-03-19 16:06:47 -05:00
|
|
|
EVAL_ERROR = 'Error while evaluating expression "%s". %s'
|
2009-06-29 07:06:01 -05:00
|
|
|
FROM_EVAL_ERROR = 'Error while evaluating the expression "%s" defined in the ' \
|
2013-03-19 16:06:47 -05:00
|
|
|
'"from" part of a statement. %s'
|
2009-06-29 07:06:01 -05:00
|
|
|
WRONG_SEQ_TYPE = 'Expression "%s" is not iterable.'
|
|
|
|
TABLE_NOT_ONE_CELL = "The table you wanted to populate with '%s' " \
|
|
|
|
"can\'t be dumped with the '-' option because it has " \
|
|
|
|
"more than one cell in it."
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class BufferAction:
|
|
|
|
'''Abstract class representing a action (=statement) that must be performed
|
|
|
|
on the content of a buffer (if, for...).'''
|
|
|
|
def __init__(self, name, buffer, expr, elem, minus, source, fromExpr):
|
|
|
|
self.name = name # Actions may be named. Currently, the name of an
|
|
|
|
# action is only used for giving a name to "if" actions; thanks to this
|
|
|
|
# name, "else" actions that are far away may reference their "if".
|
|
|
|
self.buffer = buffer # The object of the action
|
|
|
|
self.expr = expr # Python expression to evaluate (may be None in the
|
|
|
|
# case of a NullAction or ElseAction, for example)
|
|
|
|
self.elem = elem # The element within the buffer that is the object
|
|
|
|
# of the action.
|
2013-06-25 05:04:23 -05:00
|
|
|
self.minus = minus # If True, the main elem(s) must not be dumped.
|
|
|
|
self.source = source # If 'buffer', we must dump the (evaluated) buffer
|
2009-06-29 07:06:01 -05:00
|
|
|
# content. If 'from', we must dump what comes from the 'from' part of
|
|
|
|
# the action (='fromExpr')
|
|
|
|
self.fromExpr = fromExpr
|
2013-06-25 16:22:33 -05:00
|
|
|
# Several actions may co-exist for the same buffer, as a chain of
|
|
|
|
# BufferAction instances, defined via the following attribute.
|
|
|
|
self.subAction = None
|
2013-03-19 16:06:47 -05:00
|
|
|
|
|
|
|
def getExceptionLine(self, e):
|
2014-06-24 01:31:39 -05:00
|
|
|
'''Gets the line describing exception p_e, containing the exception
|
|
|
|
class, message and line number.'''
|
|
|
|
return '%s: %s' % (e.__class__.__name__, str(e))
|
2013-03-19 11:07:11 -05:00
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
def manageError(self, result, context, errorMessage, dumpTb=True):
|
|
|
|
'''Manage the encountered error: dump it into the buffer or raise an
|
2014-06-23 05:54:32 -05:00
|
|
|
exception.'''
|
|
|
|
if self.buffer.env.raiseOnError:
|
2013-06-25 05:04:23 -05:00
|
|
|
if not self.buffer.pod:
|
2013-03-19 16:06:47 -05:00
|
|
|
# Add in the error message the line nb where the errors occurs
|
|
|
|
# within the PX.
|
2013-03-22 06:52:24 -05:00
|
|
|
locator = self.buffer.env.parser.locator
|
|
|
|
# The column number may not be given.
|
|
|
|
col = locator.getColumnNumber()
|
|
|
|
if col == None: col = ''
|
|
|
|
else: col = ', column %d' % col
|
|
|
|
errorMessage += ' (line %s%s)' % (locator.getLineNumber(), col)
|
2013-03-19 16:06:47 -05:00
|
|
|
raise Exception(errorMessage)
|
2013-06-25 05:04:23 -05:00
|
|
|
# Empty the buffer (pod-only)
|
2009-06-29 07:06:01 -05:00
|
|
|
self.buffer.__init__(self.buffer.env, self.buffer.parent)
|
|
|
|
PodError.dump(self.buffer, errorMessage, withinElement=self.elem,
|
|
|
|
dumpTb=dumpTb)
|
2013-06-25 05:04:23 -05:00
|
|
|
self.buffer.evaluate(result, context)
|
2013-03-19 11:07:11 -05:00
|
|
|
|
2013-08-21 05:35:30 -05:00
|
|
|
def _evalExpr(self, expr, context):
|
|
|
|
'''Evaluates p_expr with p_context. p_expr can contain an error expr,
|
|
|
|
in the form "someExpr|errorExpr". If it is the case, if the "normal"
|
|
|
|
expr raises an error, the "error" expr is evaluated instead.'''
|
|
|
|
if '|' not in expr:
|
|
|
|
res = eval(expr, context)
|
|
|
|
else:
|
|
|
|
expr, errorExpr = expr.rsplit('|', 1)
|
|
|
|
try:
|
|
|
|
res = eval(expr, context)
|
|
|
|
except Exception:
|
|
|
|
res = eval(errorExpr, context)
|
|
|
|
return res
|
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
def evaluateExpression(self, result, context, expr):
|
|
|
|
'''Evaluates expression p_expr with the current p_context. Returns a
|
|
|
|
tuple (result, errorOccurred).'''
|
2013-03-19 11:07:11 -05:00
|
|
|
try:
|
2013-08-21 05:35:30 -05:00
|
|
|
res = self._evalExpr(expr, context)
|
2013-03-19 11:07:11 -05:00
|
|
|
error = False
|
2013-03-19 16:06:47 -05:00
|
|
|
except Exception, e:
|
2013-03-19 11:07:11 -05:00
|
|
|
res = None
|
2013-06-25 05:04:23 -05:00
|
|
|
errorMessage = EVAL_ERROR % (expr, self.getExceptionLine(e))
|
|
|
|
self.manageError(result, context, errorMessage)
|
2013-03-19 11:07:11 -05:00
|
|
|
error = True
|
|
|
|
return res, error
|
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
def execute(self, result, context):
|
|
|
|
'''Executes this action given some p_context and add the result to
|
|
|
|
p_result.'''
|
2009-06-29 07:06:01 -05:00
|
|
|
# Check that if minus is set, we have an element which can accept it
|
|
|
|
if self.minus and isinstance(self.elem, Table) and \
|
|
|
|
(not self.elem.tableInfo.isOneCell()):
|
2013-06-25 05:04:23 -05:00
|
|
|
self.manageError(result, context, TABLE_NOT_ONE_CELL % self.expr)
|
2009-06-29 07:06:01 -05:00
|
|
|
else:
|
2013-03-19 11:07:11 -05:00
|
|
|
error = False
|
2013-06-25 05:04:23 -05:00
|
|
|
# Evaluate self.expr in eRes.
|
|
|
|
eRes = None
|
2009-06-29 07:06:01 -05:00
|
|
|
if self.expr:
|
2013-06-25 05:04:23 -05:00
|
|
|
eRes,error = self.evaluateExpression(result, context, self.expr)
|
2013-03-19 11:07:11 -05:00
|
|
|
if not error:
|
2013-06-25 05:04:23 -05:00
|
|
|
# Trigger action-specific behaviour.
|
|
|
|
self.do(result, context, eRes)
|
2013-03-19 11:07:11 -05:00
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
def evaluateBuffer(self, result, context):
|
2009-06-29 07:06:01 -05:00
|
|
|
if self.source == 'buffer':
|
2013-06-25 05:04:23 -05:00
|
|
|
self.buffer.evaluate(result, context, removeMainElems=self.minus)
|
2009-06-29 07:06:01 -05:00
|
|
|
else:
|
2013-06-25 05:04:23 -05:00
|
|
|
# Evaluate self.fromExpr in feRes.
|
|
|
|
feRes = None
|
2013-03-19 16:06:47 -05:00
|
|
|
error = False
|
2009-06-29 07:06:01 -05:00
|
|
|
try:
|
2013-06-25 05:04:23 -05:00
|
|
|
feRes = eval(self.fromExpr, context)
|
2013-03-19 16:06:47 -05:00
|
|
|
except Exception, e:
|
2013-06-25 05:04:23 -05:00
|
|
|
msg = FROM_EVAL_ERROR% (self.fromExpr, self.getExceptionLine(e))
|
|
|
|
self.manageError(result, context, msg, dumpTb=False)
|
2013-03-19 16:06:47 -05:00
|
|
|
error = True
|
|
|
|
if not error:
|
2013-06-25 05:04:23 -05:00
|
|
|
result.write(feRes)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2013-06-25 16:22:33 -05:00
|
|
|
def addSubAction(self, action):
|
|
|
|
'''Adds p_action as a sub-action of this action.'''
|
|
|
|
if not self.subAction:
|
|
|
|
self.subAction = action
|
|
|
|
else:
|
|
|
|
self.subAction.addSubAction(action)
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
class IfAction(BufferAction):
|
|
|
|
'''Action that determines if we must include the content of the buffer in
|
2009-07-10 08:01:50 -05:00
|
|
|
the result or not.'''
|
2013-06-25 05:04:23 -05:00
|
|
|
def do(self, result, context, exprRes):
|
|
|
|
if exprRes:
|
2013-07-15 04:23:29 -05:00
|
|
|
if self.subAction:
|
|
|
|
self.subAction.execute(result, context)
|
|
|
|
else:
|
|
|
|
self.evaluateBuffer(result, context)
|
2009-06-29 07:06:01 -05:00
|
|
|
else:
|
|
|
|
if self.buffer.isMainElement(Cell.OD):
|
|
|
|
# Don't leave the current row with a wrong number of cells
|
2013-06-25 05:04:23 -05:00
|
|
|
result.dumpElement(Cell.OD.elem)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
class ElseAction(IfAction):
|
|
|
|
'''Action that is linked to a previous "if" action. In fact, an "else"
|
|
|
|
action works exactly like an "if" action, excepted that instead of
|
|
|
|
defining a conditional expression, it is based on the negation of the
|
|
|
|
conditional expression of the last defined "if" action.'''
|
2013-06-25 05:04:23 -05:00
|
|
|
|
|
|
|
def __init__(self, name, buff, expr, elem, minus, src, fromExpr, ifAction):
|
|
|
|
IfAction.__init__(self, name, buff, None, elem, minus, src, fromExpr)
|
2009-06-29 07:06:01 -05:00
|
|
|
self.ifAction = ifAction
|
2013-06-25 05:04:23 -05:00
|
|
|
|
|
|
|
def do(self, result, context, exprRes):
|
|
|
|
# This action is executed if the tied "if" action is not executed.
|
|
|
|
ifAction = self.ifAction
|
|
|
|
iRes,error = ifAction.evaluateExpression(result, context, ifAction.expr)
|
|
|
|
IfAction.do(self, result, context, not iRes)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
class ForAction(BufferAction):
|
|
|
|
'''Actions that will include the content of the buffer as many times as
|
2009-07-10 08:01:50 -05:00
|
|
|
specified by the action parameters.'''
|
2013-06-25 05:04:23 -05:00
|
|
|
|
|
|
|
def __init__(self, name, buff, expr, elem, minus, iter, src, fromExpr):
|
|
|
|
BufferAction.__init__(self, name, buff, expr, elem, minus, src,fromExpr)
|
2009-06-29 07:06:01 -05:00
|
|
|
self.iter = iter # Name of the iterator variable used in the each loop
|
2012-01-24 10:04:40 -06:00
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
def initialiseLoop(self, context, elems):
|
2013-08-21 05:35:30 -05:00
|
|
|
'''Initialises information about the loop, before entering into it. It
|
|
|
|
is possible that this loop overrides an outer loop whose iterator
|
|
|
|
has the same name. This method returns a tuple
|
|
|
|
(loop, outerOverriddenLoop).'''
|
2012-03-16 08:59:59 -05:00
|
|
|
# The "loop" object, made available in the POD context, contains info
|
|
|
|
# about all currently walked loops. For every walked loop, a specific
|
|
|
|
# object, le'ts name it curLoop, accessible at getattr(loop, self.iter),
|
|
|
|
# stores info about its status:
|
|
|
|
# * curLoop.length gives the total number of walked elements withhin
|
|
|
|
# the loop
|
|
|
|
# * curLoop.nb gives the index (starting at 0) if the currently
|
|
|
|
# walked element.
|
2013-06-27 04:57:39 -05:00
|
|
|
# * curLoop.first is True if the currently walked element is the
|
2013-05-27 15:32:18 -05:00
|
|
|
# first one.
|
2013-06-27 04:57:39 -05:00
|
|
|
# * curLoop.last is True if the currently walked element is the
|
2013-05-27 15:32:18 -05:00
|
|
|
# last one.
|
2013-06-27 04:57:39 -05:00
|
|
|
# * curLoop.odd is True if the currently walked element is odd
|
|
|
|
# * curLoop.even is True if the currently walked element is even
|
2012-03-16 08:59:59 -05:00
|
|
|
# For example, if you have a "for" statement like this:
|
|
|
|
# for elem in myListOfElements
|
|
|
|
# Within the part of the ODT document impacted by this statement, you
|
|
|
|
# may access to:
|
|
|
|
# * loop.elem.length to know the total length of myListOfElements
|
|
|
|
# * loop.elem.nb to know the index of the current elem within
|
|
|
|
# myListOfElements.
|
|
|
|
if 'loop' not in context:
|
|
|
|
context['loop'] = Object()
|
|
|
|
try:
|
2013-06-25 05:04:23 -05:00
|
|
|
total = len(elems)
|
2013-08-21 05:35:30 -05:00
|
|
|
except Exception:
|
2012-03-16 08:59:59 -05:00
|
|
|
total = 0
|
|
|
|
curLoop = Object(length=total)
|
2013-08-21 05:35:30 -05:00
|
|
|
# Does this loop overrides an outer loop whose iterator has the same
|
|
|
|
# name ?
|
|
|
|
outerLoop = None
|
|
|
|
if hasattr(context['loop'], self.iter):
|
|
|
|
outerLoop = getattr(context['loop'], self.iter)
|
|
|
|
# Put this loop in the global object "loop".
|
2012-03-16 08:59:59 -05:00
|
|
|
setattr(context['loop'], self.iter, curLoop)
|
2013-08-21 05:35:30 -05:00
|
|
|
return curLoop, outerLoop
|
2012-03-16 08:59:59 -05:00
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
def do(self, result, context, elems):
|
|
|
|
'''Performs the "for" action. p_elems is the list of elements to
|
|
|
|
walk, evaluated from self.expr.'''
|
|
|
|
# Check p_exprRes type.
|
2009-06-29 07:06:01 -05:00
|
|
|
try:
|
2013-03-19 16:06:47 -05:00
|
|
|
# All "iterable" objects are OK.
|
2013-06-25 05:04:23 -05:00
|
|
|
iter(elems)
|
2009-06-29 07:06:01 -05:00
|
|
|
except TypeError:
|
2013-06-25 05:04:23 -05:00
|
|
|
self.manageError(result, context, WRONG_SEQ_TYPE % self.expr)
|
2009-06-29 07:06:01 -05:00
|
|
|
return
|
|
|
|
# Remember variable hidden by iter if any
|
|
|
|
hasHiddenVariable = False
|
|
|
|
if context.has_key(self.iter):
|
|
|
|
hiddenVariable = context[self.iter]
|
|
|
|
hasHiddenVariable = True
|
|
|
|
# In the case of cells, initialize some values
|
|
|
|
isCell = False
|
|
|
|
if isinstance(self.elem, Cell):
|
|
|
|
isCell = True
|
|
|
|
nbOfColumns = self.elem.tableInfo.nbOfColumns
|
2011-02-18 08:58:59 -06:00
|
|
|
initialColIndex = self.elem.colIndex
|
2009-06-29 07:06:01 -05:00
|
|
|
currentColIndex = initialColIndex
|
|
|
|
rowAttributes = self.elem.tableInfo.curRowAttrs
|
2013-06-25 05:04:23 -05:00
|
|
|
# If p_elems is empty, dump an empty cell to avoid having the wrong
|
|
|
|
# number of cells for the current row.
|
|
|
|
if not elems:
|
|
|
|
result.dumpElement(Cell.OD.elem)
|
|
|
|
# Enter the "for" loop.
|
2013-08-21 05:35:30 -05:00
|
|
|
loop, outerLoop = self.initialiseLoop(context, elems)
|
2012-03-16 08:59:59 -05:00
|
|
|
i = -1
|
2013-06-25 05:04:23 -05:00
|
|
|
for item in elems:
|
2012-03-16 08:59:59 -05:00
|
|
|
i += 1
|
|
|
|
loop.nb = i
|
2013-05-27 15:32:18 -05:00
|
|
|
loop.first = i == 0
|
|
|
|
loop.last = i == (loop.length-1)
|
2013-06-27 04:57:39 -05:00
|
|
|
loop.even = (i%2)==0
|
|
|
|
loop.odd = not loop.even
|
2009-06-29 07:06:01 -05:00
|
|
|
context[self.iter] = item
|
|
|
|
# Cell: add a new row if we are at the end of a row
|
|
|
|
if isCell and (currentColIndex == nbOfColumns):
|
2013-06-25 05:04:23 -05:00
|
|
|
result.dumpEndElement(Row.OD.elem)
|
|
|
|
result.dumpStartElement(Row.OD.elem, rowAttributes)
|
2009-06-29 07:06:01 -05:00
|
|
|
currentColIndex = 0
|
2013-06-25 16:22:33 -05:00
|
|
|
# If a sub-action is defined, execute it.
|
|
|
|
if self.subAction:
|
|
|
|
self.subAction.execute(result, context)
|
|
|
|
else:
|
|
|
|
# Evaluate the buffer directly.
|
|
|
|
self.evaluateBuffer(result, context)
|
2009-06-29 07:06:01 -05:00
|
|
|
# Cell: increment the current column index
|
|
|
|
if isCell:
|
|
|
|
currentColIndex += 1
|
|
|
|
# Cell: leave the last row with the correct number of cells
|
2013-06-25 05:04:23 -05:00
|
|
|
if isCell and elems:
|
2009-06-29 07:06:01 -05:00
|
|
|
wrongNbOfCells = (currentColIndex-1) - initialColIndex
|
|
|
|
if wrongNbOfCells < 0: # Too few cells for last row
|
|
|
|
for i in range(abs(wrongNbOfCells)):
|
|
|
|
context[self.iter] = ''
|
2013-06-25 05:04:23 -05:00
|
|
|
self.buffer.evaluate(result, context, subElements=False)
|
2009-06-29 07:06:01 -05:00
|
|
|
# This way, the cell is dumped with the correct styles
|
|
|
|
elif wrongNbOfCells > 0: # Too many cells for last row
|
|
|
|
# Finish current row
|
|
|
|
nbOfMissingCells = 0
|
|
|
|
if currentColIndex < nbOfColumns:
|
|
|
|
nbOfMissingCells = nbOfColumns - currentColIndex
|
|
|
|
context[self.iter] = ''
|
|
|
|
for i in range(nbOfMissingCells):
|
2013-06-25 05:04:23 -05:00
|
|
|
self.buffer.evaluate(result, context, subElements=False)
|
|
|
|
result.dumpEndElement(Row.OD.elem)
|
2009-06-29 07:06:01 -05:00
|
|
|
# Create additional row with remaining cells
|
2013-06-25 05:04:23 -05:00
|
|
|
result.dumpStartElement(Row.OD.elem, rowAttributes)
|
2009-06-29 07:06:01 -05:00
|
|
|
nbOfRemainingCells = wrongNbOfCells + nbOfMissingCells
|
|
|
|
nbOfMissingCellsLastLine = nbOfColumns - nbOfRemainingCells
|
|
|
|
context[self.iter] = ''
|
|
|
|
for i in range(nbOfMissingCellsLastLine):
|
2013-06-25 05:04:23 -05:00
|
|
|
self.buffer.evaluate(result, context, subElements=False)
|
2013-08-21 05:35:30 -05:00
|
|
|
# Delete the current loop object and restore the overridden one if any.
|
2012-03-16 08:59:59 -05:00
|
|
|
try:
|
|
|
|
delattr(context['loop'], self.iter)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2013-08-21 05:35:30 -05:00
|
|
|
if outerLoop:
|
|
|
|
setattr(context['loop'], self.iter, outerLoop)
|
2009-06-29 07:06:01 -05:00
|
|
|
# Restore hidden variable if any
|
|
|
|
if hasHiddenVariable:
|
|
|
|
context[self.iter] = hiddenVariable
|
|
|
|
else:
|
2013-06-25 05:04:23 -05:00
|
|
|
if elems:
|
2012-01-24 10:04:40 -06:00
|
|
|
if self.iter in context: # May not be the case on error.
|
|
|
|
del context[self.iter]
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
class NullAction(BufferAction):
|
|
|
|
'''Action that does nothing. Used in conjunction with a "from" clause, it
|
|
|
|
allows to insert in a buffer arbitrary odt content.'''
|
2013-06-25 05:04:23 -05:00
|
|
|
def do(self, result, context, exprRes):
|
|
|
|
self.evaluateBuffer(result, context)
|
2009-07-10 08:01:50 -05:00
|
|
|
|
2013-03-19 11:07:11 -05:00
|
|
|
class VariablesAction(BufferAction):
|
|
|
|
'''Action that allows to define a set of variables somewhere in the
|
|
|
|
template.'''
|
2013-06-25 05:04:23 -05:00
|
|
|
def __init__(self, name, buff, elem, minus, variables, src, fromExpr):
|
2013-03-19 11:07:11 -05:00
|
|
|
# We do not use the default Buffer.expr attribute for storing the Python
|
|
|
|
# expression, because here we will have several expressions, one for
|
|
|
|
# every defined variable.
|
2013-06-25 05:04:23 -05:00
|
|
|
BufferAction.__init__(self,name, buff, None, elem, minus, src, fromExpr)
|
2013-03-22 06:52:24 -05:00
|
|
|
# Definitions of variables: ~[(s_name, s_expr)]~
|
2013-03-19 11:07:11 -05:00
|
|
|
self.variables = variables
|
|
|
|
|
2013-06-25 05:04:23 -05:00
|
|
|
def do(self, result, context, exprRes):
|
|
|
|
'''Evaluate the variables' expressions: because there are several
|
|
|
|
expressions, we do not use the standard, single-expression-minded
|
|
|
|
BufferAction code for evaluating our expressions.
|
|
|
|
|
|
|
|
We remember the names and values of the variables that we will hide
|
|
|
|
in the context: after execution of this buffer we will restore those
|
|
|
|
values.
|
|
|
|
'''
|
2013-03-19 11:07:11 -05:00
|
|
|
hidden = None
|
2013-03-22 06:52:24 -05:00
|
|
|
for name, expr in self.variables:
|
2013-06-25 05:04:23 -05:00
|
|
|
# Evaluate variable expression in vRes.
|
|
|
|
vRes, error = self.evaluateExpression(result, context, expr)
|
2013-03-19 11:07:11 -05:00
|
|
|
if error: return
|
2013-09-23 15:36:09 -05:00
|
|
|
# Replace the value of global variables
|
|
|
|
if name.startswith('@'):
|
|
|
|
context[name[1:]] = vRes
|
|
|
|
continue
|
2013-03-19 11:07:11 -05:00
|
|
|
# Remember the variable previous value if already in the context
|
|
|
|
if name in context:
|
|
|
|
if not hidden:
|
|
|
|
hidden = {name: context[name]}
|
|
|
|
else:
|
|
|
|
hidden[name] = context[name]
|
2013-03-19 16:06:47 -05:00
|
|
|
# Store the result into the context
|
2013-06-25 05:04:23 -05:00
|
|
|
context[name] = vRes
|
2013-06-25 16:22:33 -05:00
|
|
|
# If a sub-action is defined, execute it.
|
|
|
|
if self.subAction:
|
|
|
|
self.subAction.execute(result, context)
|
|
|
|
else:
|
|
|
|
# Evaluate the buffer directly.
|
|
|
|
self.evaluateBuffer(result, context)
|
2013-03-19 11:07:11 -05:00
|
|
|
# Restore hidden variables if any
|
|
|
|
if hidden: context.update(hidden)
|
|
|
|
# Delete not-hidden variables
|
2013-03-22 06:52:24 -05:00
|
|
|
for name, expr in self.variables:
|
2013-09-23 15:36:09 -05:00
|
|
|
if name.startswith('@'): continue
|
2013-03-19 11:07:11 -05:00
|
|
|
if hidden and (name in hidden): continue
|
|
|
|
del context[name]
|
2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|