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
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
import re
|
2011-06-16 19:00:25 -05:00
|
|
|
from xml.sax.saxutils import quoteattr
|
2012-10-26 06:09:44 -05:00
|
|
|
from appy.shared.xml_parser import xmlPrologue, escapeXml
|
|
|
|
from appy.pod import PodError
|
2013-08-21 05:35:30 -05:00
|
|
|
from appy.shared.utils import Traceback
|
2009-06-29 07:06:01 -05:00
|
|
|
from appy.pod.elements import *
|
2013-03-19 11:07:11 -05:00
|
|
|
from appy.pod.actions import IfAction, ElseAction, ForAction, VariablesAction, \
|
2009-07-10 08:01:50 -05:00
|
|
|
NullAction
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class ParsingError(Exception): pass
|
2015-02-26 02:53:53 -06:00
|
|
|
class EvaluationError(Exception): pass
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
# ParsingError-related constants -----------------------------------------------
|
|
|
|
ELEMENT = 'identifies the part of the document that will be impacted ' \
|
|
|
|
'by the command. It must be one of %s.' % str(PodElement.POD_ELEMS)
|
|
|
|
FOR_EXPRESSION = 'must be of the form: {name} in {expression}. {name} must be '\
|
|
|
|
'a Python variable name. It is the name of the iteration ' \
|
|
|
|
'variable. {expression} is a Python expression that, when ' \
|
|
|
|
'evaluated, produces a Python sequence (tuple, string, list, '\
|
|
|
|
'etc).'
|
|
|
|
POD_STATEMENT = 'A Pod statement has the ' \
|
|
|
|
'form: do {element} [{command} {expression}]. {element} ' + \
|
|
|
|
ELEMENT + ' Optional {command} can be "if" ' \
|
|
|
|
'(conditional inclusion of the element) or "for" (multiple ' \
|
|
|
|
'inclusion of the element). For an "if" command, {expression} '\
|
|
|
|
'is any Python expression. For a "for" command, {expression} '+\
|
|
|
|
FOR_EXPRESSION
|
|
|
|
FROM_CLAUSE = 'A "from" clause has the form: from {expression}, where ' \
|
|
|
|
'{expression} is a Python expression that, when evaluated, ' \
|
|
|
|
'produces a valid chunk of odt content that will be inserted ' \
|
|
|
|
'instead of the element that is the target of the note.'
|
|
|
|
BAD_STATEMENT_GROUP = 'Syntax error while parsing a note whose content is ' \
|
|
|
|
'"%s". In a note, you may specify at most 2 lines: a ' \
|
|
|
|
'pod statement and a "from" clause. ' + POD_STATEMENT + \
|
|
|
|
' ' + FROM_CLAUSE
|
|
|
|
BAD_STATEMENT = 'Syntax error for statement "%s". ' + POD_STATEMENT
|
|
|
|
BAD_ELEMENT = 'Bad element "%s". An element ' + ELEMENT
|
|
|
|
BAD_MINUS = "The '-' operator can't be used with element '%s'. It can only be "\
|
|
|
|
"specified for elements among %s."
|
|
|
|
ELEMENT_NOT_FOUND = 'Action specified element "%s" but available elements ' \
|
|
|
|
'in this part of the document are %s.'
|
|
|
|
BAD_FROM_CLAUSE = 'Syntax error in "from" clause "%s". ' + FROM_CLAUSE
|
|
|
|
DUPLICATE_NAMED_IF = 'An "if" statement with the same name already exists.'
|
|
|
|
ELSE_WITHOUT_IF = 'No previous "if" statement could be found for this "else" ' \
|
|
|
|
'statement.'
|
|
|
|
ELSE_WITHOUT_NAMED_IF = 'I could not find an "if" statement named "%s".'
|
|
|
|
BAD_FOR_EXPRESSION = 'Bad "for" expression "%s". A "for" expression ' + \
|
|
|
|
FOR_EXPRESSION
|
2009-07-10 08:01:50 -05:00
|
|
|
BAD_VAR_EXPRESSION = 'Bad variable definition "%s". A variable definition ' \
|
|
|
|
'must have the form {name} = {expression}. {name} must be a Python-' \
|
|
|
|
'compliant variable name. {expression} is a Python expression. When ' \
|
|
|
|
'encountering such a statement, pod will define, in the specified part ' \
|
|
|
|
'of the document, a variable {name} whose value will be the evaluated ' \
|
|
|
|
'{expression}.'
|
2009-06-29 07:06:01 -05:00
|
|
|
EVAL_EXPR_ERROR = 'Error while evaluating expression "%s". %s'
|
|
|
|
NULL_ACTION_ERROR = 'There was a problem with this action. Possible causes: ' \
|
|
|
|
'(1) you specified no action (ie "do text") while not ' \
|
|
|
|
'specifying any from clause; (2) you specified the from ' \
|
|
|
|
'clause on the same line as the action, which is not ' \
|
|
|
|
'allowed (ie "do text from ...").'
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class BufferIterator:
|
|
|
|
def __init__(self, buffer):
|
|
|
|
self.buffer = buffer
|
2015-10-27 15:10:24 -05:00
|
|
|
self.remainingSubBufferIndexes = list(self.buffer.subBuffers.keys())
|
|
|
|
self.remainingElemIndexes = list(self.buffer.elements.keys())
|
2009-06-29 07:06:01 -05:00
|
|
|
self.remainingSubBufferIndexes.sort()
|
|
|
|
self.remainingElemIndexes.sort()
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def hasNext(self):
|
|
|
|
return self.remainingSubBufferIndexes or self.remainingElemIndexes
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2015-10-27 15:10:24 -05:00
|
|
|
def __next__(self):
|
2009-06-29 07:06:01 -05:00
|
|
|
nextSubBufferIndex = None
|
|
|
|
if self.remainingSubBufferIndexes:
|
|
|
|
nextSubBufferIndex = self.remainingSubBufferIndexes[0]
|
|
|
|
nextExprIndex = None
|
|
|
|
if self.remainingElemIndexes:
|
|
|
|
nextExprIndex = self.remainingElemIndexes[0]
|
|
|
|
# Compute min between nextSubBufferIndex and nextExprIndex
|
|
|
|
if (nextSubBufferIndex != None) and (nextExprIndex != None):
|
|
|
|
res = min(nextSubBufferIndex, nextExprIndex)
|
|
|
|
elif (nextSubBufferIndex == None) and (nextExprIndex != None):
|
|
|
|
res = nextExprIndex
|
|
|
|
elif (nextSubBufferIndex != None) and (nextExprIndex == None):
|
|
|
|
res = nextSubBufferIndex
|
|
|
|
# Update "remaining" lists
|
|
|
|
if res == nextSubBufferIndex:
|
|
|
|
self.remainingSubBufferIndexes = self.remainingSubBufferIndexes[1:]
|
|
|
|
resDict = self.buffer.subBuffers
|
|
|
|
elif res == nextExprIndex:
|
|
|
|
self.remainingElemIndexes = self.remainingElemIndexes[1:]
|
|
|
|
resDict = self.buffer.elements
|
|
|
|
return res, resDict[res]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Buffer:
|
|
|
|
'''Abstract class representing any buffer used during rendering.'''
|
|
|
|
elementRex = re.compile('([\w-]+:[\w-]+)\s*(.*?)>', re.S)
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def __init__(self, env, parent):
|
|
|
|
self.parent = parent
|
|
|
|
self.subBuffers = {} # ~{i_bufferIndex: Buffer}~
|
|
|
|
self.env = env
|
2013-06-25 05:04:23 -05:00
|
|
|
# Are we computing for pod (True) or px (False)
|
|
|
|
self.pod = env.__class__.__name__ != 'PxEnvironment'
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def addSubBuffer(self, subBuffer=None):
|
|
|
|
if not subBuffer:
|
|
|
|
subBuffer = MemoryBuffer(self.env, self)
|
|
|
|
self.subBuffers[self.getLength()] = subBuffer
|
|
|
|
subBuffer.parent = self
|
|
|
|
return subBuffer
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def removeLastSubBuffer(self):
|
2015-10-27 15:10:24 -05:00
|
|
|
subBufferIndexes = list(self.subBuffers.keys())
|
2009-06-29 07:06:01 -05:00
|
|
|
subBufferIndexes.sort()
|
|
|
|
lastIndex = subBufferIndexes.pop()
|
|
|
|
del self.subBuffers[lastIndex]
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def write(self, something): pass # To be overridden
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def getLength(self): pass # To be overridden
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2015-01-14 04:46:25 -06:00
|
|
|
def patchTableElement(self, elem, attrs):
|
|
|
|
'''Convert the name of a table to an expression allowing the user to
|
|
|
|
define himself this name via variable "tableName".
|
|
|
|
|
|
|
|
Convert attribute "number-columns-repeated" of every table column
|
|
|
|
(or add it if it does not exist) to let the user define how he will
|
|
|
|
repeat table columns via variable "columnsRepeated".'''
|
|
|
|
if elem == self.env.tags['table']:
|
|
|
|
attrs = attrs._attrs
|
|
|
|
name = self.env.tags['table-name']
|
|
|
|
attrs[name] = ':tableName|"%s"' % attrs[name]
|
|
|
|
elif elem == self.env.tags['table-column']:
|
|
|
|
attrs = attrs._attrs
|
|
|
|
key = self.env.tags['number-columns-repeated']
|
|
|
|
columnNumber = self.env.getTable().nbOfColumns -1
|
|
|
|
nb = (key in attrs) and attrs[key] or '1'
|
|
|
|
attrs[key] = ':columnsRepeated[%d]|%s' % (columnNumber, nb)
|
2015-01-13 10:02:59 -06:00
|
|
|
|
2013-06-26 06:44:31 -05:00
|
|
|
def dumpStartElement(self, elem, attrs={}, ignoreAttrs=(), hook=False,
|
2013-07-08 16:39:16 -05:00
|
|
|
noEndTag=False, renamedAttrs=None):
|
2013-02-06 10:15:01 -06:00
|
|
|
'''Inserts into this buffer the start tag p_elem, with its p_attrs,
|
2013-07-08 16:39:16 -05:00
|
|
|
excepted those listed in p_ignoreAttrs. Attrs can be dumped with an
|
|
|
|
alternative name if specified in dict p_renamedAttrs. If p_hook is
|
|
|
|
not None (works only for MemoryBuffers), we will insert, at the end
|
|
|
|
of the list of dumped attributes:
|
2013-06-26 06:44:31 -05:00
|
|
|
* [pod] an Attributes instance, in order to be able, when evaluating
|
|
|
|
the buffer, to dump additional attributes, not known at this
|
|
|
|
dump time;
|
|
|
|
* [px] an Attribute instance, representing a special HTML attribute
|
|
|
|
like "checked" or "selected", that, if the tied expression
|
|
|
|
returns False, must not be dumped at all. In this case,
|
|
|
|
p_hook must be a tuple (s_attrName, s_expr).
|
|
|
|
'''
|
2009-06-29 07:06:01 -05:00
|
|
|
self.write('<%s' % elem)
|
2015-01-14 04:46:25 -06:00
|
|
|
# Some table elements must be patched (pod only)
|
|
|
|
if self.pod: self.patchTableElement(elem, attrs)
|
2015-10-27 15:10:24 -05:00
|
|
|
for name, value in list(attrs.items()):
|
2013-01-30 17:11:24 -06:00
|
|
|
if ignoreAttrs and (name in ignoreAttrs): continue
|
2013-07-08 16:39:16 -05:00
|
|
|
if renamedAttrs and (name in renamedAttrs): name=renamedAttrs[name]
|
2013-03-15 18:02:16 -05:00
|
|
|
# If the value begins with ':', it is a Python expression. Else,
|
|
|
|
# it is a static value.
|
|
|
|
if not value.startswith(':'):
|
|
|
|
self.write(' %s=%s' % (name, quoteattr(value)))
|
|
|
|
else:
|
|
|
|
self.write(' %s="' % name)
|
|
|
|
self.addExpression(value[1:])
|
|
|
|
self.write('"')
|
2013-06-26 06:44:31 -05:00
|
|
|
res = None
|
|
|
|
if hook:
|
|
|
|
if self.pod:
|
|
|
|
res = self.addAttributes()
|
|
|
|
else:
|
|
|
|
self.addAttribute(*hook)
|
2013-03-22 06:52:24 -05:00
|
|
|
# Close the tag
|
2013-06-26 06:44:31 -05:00
|
|
|
self.write(noEndTag and '/>' or '>')
|
2013-02-06 10:15:01 -06:00
|
|
|
return res
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def dumpEndElement(self, elem):
|
|
|
|
self.write('</%s>' % elem)
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def dumpElement(self, elem, content=None, attrs={}):
|
|
|
|
'''For dumping a whole element at once.'''
|
|
|
|
self.dumpStartElement(elem, attrs)
|
|
|
|
if content:
|
|
|
|
self.dumpContent(content)
|
|
|
|
self.dumpEndElement(elem)
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def dumpContent(self, content):
|
|
|
|
'''Dumps string p_content into the buffer.'''
|
2013-06-25 05:04:23 -05:00
|
|
|
if self.pod:
|
2015-01-13 10:02:59 -06:00
|
|
|
# Take care of converting line breaks and tabs
|
2013-06-12 03:30:20 -05:00
|
|
|
content = escapeXml(content, format='odf',
|
|
|
|
nsText=self.env.namespaces[self.env.NS_TEXT])
|
|
|
|
else:
|
|
|
|
content = escapeXml(content)
|
|
|
|
self.write(content)
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class FileBuffer(Buffer):
|
|
|
|
def __init__(self, env, result):
|
|
|
|
Buffer.__init__(self, env, None)
|
|
|
|
self.result = result
|
|
|
|
self.content = file(result, 'w')
|
2010-08-05 11:23:17 -05:00
|
|
|
self.content.write(xmlPrologue)
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
# getLength is used to manage insertions into sub-buffers. But in the case
|
|
|
|
# of a FileBuffer, we will only have 1 sub-buffer at a time, and we don't
|
|
|
|
# care about where it will be inserted into the FileBuffer.
|
2011-01-28 07:36:30 -06:00
|
|
|
def getLength(self): return 0
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def write(self, something):
|
2011-06-28 02:12:20 -05:00
|
|
|
try:
|
|
|
|
self.content.write(something.encode('utf-8'))
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
self.content.write(something)
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2013-02-06 10:15:01 -06:00
|
|
|
def addExpression(self, expression, tiedHook=None):
|
|
|
|
# At 2013-02-06, this method was not called within the whole test suite.
|
2009-06-29 07:06:01 -05:00
|
|
|
try:
|
2013-06-25 05:04:23 -05:00
|
|
|
expr = Expression(expression, self.pod)
|
2013-06-26 06:44:31 -05:00
|
|
|
if tiedHook: tiedHook.tiedExpression = expr
|
2013-06-25 05:04:23 -05:00
|
|
|
res, escape = expr.evaluate(self.env.context)
|
2013-03-22 06:52:24 -05:00
|
|
|
if escape: self.dumpContent(res)
|
|
|
|
else: self.write(res)
|
2015-10-27 15:10:24 -05:00
|
|
|
except Exception as e:
|
2014-06-23 05:54:32 -05:00
|
|
|
if not self.env.raiseOnError:
|
|
|
|
PodError.dump(self, EVAL_EXPR_ERROR % (expression, e),
|
|
|
|
dumpTb=False)
|
|
|
|
else:
|
|
|
|
raise Exception(EVAL_EXPR_ERROR % (expression, e))
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2013-02-06 10:15:01 -06:00
|
|
|
def addAttributes(self):
|
|
|
|
# Into a FileBuffer, it is not possible to insert Attributes. Every
|
|
|
|
# Attributes instance is tied to an Expression; because dumping
|
|
|
|
# expressions directly into FileBuffer instances seems to be a rather
|
|
|
|
# theorical case (see comment inside the previous method), it does not
|
|
|
|
# seem to be a real problem.
|
|
|
|
pass
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def pushSubBuffer(self, subBuffer): pass
|
2013-03-15 10:50:28 -05:00
|
|
|
def getRootBuffer(self): return self
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class MemoryBuffer(Buffer):
|
|
|
|
actionRex = re.compile('(?:(\w+)\s*\:\s*)?do\s+(\w+)(-)?' \
|
2009-07-10 08:01:50 -05:00
|
|
|
'(?:\s+(for|if|else|with)\s*(.*))?')
|
2009-06-29 07:06:01 -05:00
|
|
|
forRex = re.compile('\s*([\w\-_]+)\s+in\s+(.*)')
|
2013-09-23 15:36:09 -05:00
|
|
|
varRex = re.compile('\s*(@?[\w\-_]+)\s*=\s*(.*)')
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def __init__(self, env, parent):
|
|
|
|
Buffer.__init__(self, env, parent)
|
2015-10-27 15:10:24 -05:00
|
|
|
self.content = ''
|
2009-06-29 07:06:01 -05:00
|
|
|
self.elements = {}
|
|
|
|
self.action = None
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2014-10-16 10:35:35 -05:00
|
|
|
def clone(self):
|
|
|
|
'''Produces an empty buffer that is a clone of this one.'''
|
|
|
|
return MemoryBuffer(self.env, self.parent)
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def addSubBuffer(self, subBuffer=None):
|
|
|
|
sb = Buffer.addSubBuffer(self, subBuffer)
|
|
|
|
self.content += ' ' # To avoid having several subbuffers referenced at
|
|
|
|
# the same place within this buffer.
|
|
|
|
return sb
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2013-03-15 10:50:28 -05:00
|
|
|
def getRootBuffer(self):
|
|
|
|
'''Returns the root buffer. For POD it is always a FileBuffer. For PX,
|
|
|
|
it is a MemoryBuffer.'''
|
|
|
|
if self.parent: return self.parent.getRootBuffer()
|
|
|
|
return self
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def getLength(self): return len(self.content)
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def write(self, thing): self.content += thing
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def getIndex(self, podElemName):
|
|
|
|
res = -1
|
2015-10-27 15:10:24 -05:00
|
|
|
for index, podElem in self.elements.items():
|
2009-06-29 07:06:01 -05:00
|
|
|
if podElem.__class__.__name__.lower() == podElemName:
|
|
|
|
if index > res:
|
|
|
|
res = index
|
|
|
|
return res
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def getMainElement(self):
|
|
|
|
res = None
|
2015-10-27 15:10:24 -05:00
|
|
|
if 0 in self.elements:
|
2009-06-29 07:06:01 -05:00
|
|
|
res = self.elements[0]
|
|
|
|
return res
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def isMainElement(self, elem):
|
2014-06-23 09:48:32 -05:00
|
|
|
'''Is p_elem the main element within this buffer?'''
|
2009-06-29 07:06:01 -05:00
|
|
|
mainElem = self.getMainElement()
|
2013-03-15 10:50:28 -05:00
|
|
|
if not mainElem: return
|
|
|
|
if hasattr(mainElem, 'OD'): mainElem = mainElem.OD.elem
|
|
|
|
if elem != mainElem: return
|
|
|
|
# elem is the same as the main elem. But is it really the main elem, or
|
|
|
|
# the same elem, found deeper in the buffer?
|
2015-10-27 15:10:24 -05:00
|
|
|
for index, iElem in self.elements.items():
|
2013-03-15 10:50:28 -05:00
|
|
|
foundElem = None
|
|
|
|
if hasattr(iElem, 'OD'):
|
|
|
|
if iElem.OD:
|
|
|
|
foundElem = iElem.OD.elem
|
|
|
|
else:
|
|
|
|
foundElem = iElem
|
|
|
|
if (foundElem == mainElem) and (index != 0):
|
|
|
|
return
|
|
|
|
return True
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def unreferenceElement(self, elem):
|
|
|
|
# Find last occurrence of this element
|
|
|
|
elemIndex = -1
|
2015-10-27 15:10:24 -05:00
|
|
|
for index, iElem in self.elements.items():
|
2013-03-15 10:50:28 -05:00
|
|
|
foundElem = None
|
|
|
|
if hasattr(iElem, 'OD'):
|
|
|
|
# A POD element
|
|
|
|
if iElem.OD:
|
|
|
|
foundElem = iElem.OD.elem
|
|
|
|
else:
|
|
|
|
# A PX elem
|
|
|
|
foundElem = iElem
|
|
|
|
if (foundElem == elem) and (index > elemIndex):
|
|
|
|
elemIndex = index
|
2009-06-29 07:06:01 -05:00
|
|
|
del self.elements[elemIndex]
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def pushSubBuffer(self, subBuffer):
|
|
|
|
'''Sets p_subBuffer at the very end of the buffer.'''
|
|
|
|
subIndex = None
|
2015-10-27 15:10:24 -05:00
|
|
|
for index, aSubBuffer in self.subBuffers.items():
|
2009-06-29 07:06:01 -05:00
|
|
|
if aSubBuffer == subBuffer:
|
|
|
|
subIndex = index
|
|
|
|
break
|
|
|
|
if subIndex != None:
|
|
|
|
# Indeed, it is possible that this buffer is not referenced
|
|
|
|
# in the parent (if it is a temp buffer generated from a cut)
|
|
|
|
del self.subBuffers[subIndex]
|
|
|
|
self.subBuffers[self.getLength()] = subBuffer
|
2015-10-27 15:10:24 -05:00
|
|
|
self.content += ' '
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def transferAllContent(self):
|
|
|
|
'''Transfer all content to parent.'''
|
|
|
|
if isinstance(self.parent, FileBuffer):
|
|
|
|
# First unreference all elements
|
|
|
|
for index in self.getElementIndexes(expressions=False):
|
|
|
|
del self.elements[index]
|
2013-06-25 05:04:23 -05:00
|
|
|
self.evaluate(self.parent, self.env.context)
|
2009-06-29 07:06:01 -05:00
|
|
|
else:
|
|
|
|
# Transfer content in itself
|
|
|
|
oldParentLength = self.parent.getLength()
|
|
|
|
self.parent.write(self.content)
|
|
|
|
# Transfer elements
|
2015-10-27 15:10:24 -05:00
|
|
|
for index, podElem in self.elements.items():
|
2009-06-29 07:06:01 -05:00
|
|
|
self.parent.elements[oldParentLength + index] = podElem
|
2013-06-25 05:04:23 -05:00
|
|
|
# Transfer sub-buffers
|
2015-10-27 15:10:24 -05:00
|
|
|
for index, buf in self.subBuffers.items():
|
2009-06-29 07:06:01 -05:00
|
|
|
self.parent.subBuffers[oldParentLength + index] = buf
|
|
|
|
# Empty the buffer
|
|
|
|
MemoryBuffer.__init__(self, self.env, self.parent)
|
|
|
|
# Change buffer position wrt parent
|
|
|
|
self.parent.pushSubBuffer(self)
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2013-03-15 10:50:28 -05:00
|
|
|
def addElement(self, elem, elemType='pod'):
|
|
|
|
if elemType == 'pod':
|
|
|
|
elem = PodElement.create(elem)
|
|
|
|
self.elements[self.getLength()] = elem
|
|
|
|
if isinstance(elem, Cell) or isinstance(elem, Table):
|
|
|
|
elem.tableInfo = self.env.getTable()
|
|
|
|
if isinstance(elem, Cell):
|
2011-02-18 08:58:59 -06:00
|
|
|
# Remember where this cell is in the table
|
2013-03-15 10:50:28 -05:00
|
|
|
elem.colIndex = elem.tableInfo.curColIndex
|
|
|
|
if elem == 'x':
|
2013-06-27 10:40:01 -05:00
|
|
|
# See comment on similar statement in the method below.
|
2015-10-27 15:10:24 -05:00
|
|
|
self.content += ' '
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2013-02-06 10:15:01 -06:00
|
|
|
def addExpression(self, expression, tiedHook=None):
|
2009-06-29 07:06:01 -05:00
|
|
|
# Create the POD expression
|
2013-06-25 05:04:23 -05:00
|
|
|
expr = Expression(expression, self.pod)
|
2013-02-06 10:15:01 -06:00
|
|
|
if tiedHook: tiedHook.tiedExpression = expr
|
2009-06-29 07:06:01 -05:00
|
|
|
self.elements[self.getLength()] = expr
|
2013-03-15 10:50:28 -05:00
|
|
|
# To be sure that an expr and an elem can't be found at the same index
|
|
|
|
# in the buffer.
|
2015-10-27 15:10:24 -05:00
|
|
|
self.content += ' '
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2013-02-06 10:15:01 -06:00
|
|
|
def addAttributes(self):
|
2013-06-26 06:44:31 -05:00
|
|
|
'''pod-only: adds an Attributes instance into this buffer.'''
|
2013-02-06 10:15:01 -06:00
|
|
|
attrs = Attributes(self.env)
|
|
|
|
self.elements[self.getLength()] = attrs
|
2015-10-27 15:10:24 -05:00
|
|
|
self.content += ' '
|
2013-02-06 10:15:01 -06:00
|
|
|
return attrs
|
|
|
|
|
2013-06-26 06:44:31 -05:00
|
|
|
def addAttribute(self, name, expr):
|
|
|
|
'''px-only: adds an Attribute instance into this buffer.'''
|
|
|
|
attr = Attribute(name, expr)
|
|
|
|
self.elements[self.getLength()] = attr
|
2015-10-27 15:10:24 -05:00
|
|
|
self.content += ' '
|
2013-06-26 06:44:31 -05:00
|
|
|
return attr
|
|
|
|
|
2013-03-19 11:07:11 -05:00
|
|
|
def _getVariables(self, expr):
|
2013-03-22 06:52:24 -05:00
|
|
|
'''Returns variable definitions in p_expr as a list
|
|
|
|
~[(s_varName, s_expr)]~.'''
|
2013-03-19 11:07:11 -05:00
|
|
|
exprs = expr.strip().split(';')
|
2013-03-22 06:52:24 -05:00
|
|
|
res = []
|
2013-03-19 11:07:11 -05:00
|
|
|
for sub in exprs:
|
|
|
|
varRes = MemoryBuffer.varRex.match(sub)
|
|
|
|
if not varRes:
|
|
|
|
raise ParsingError(BAD_VAR_EXPRESSION % sub)
|
2013-03-22 06:52:24 -05:00
|
|
|
res.append(varRes.groups())
|
2013-03-19 11:07:11 -05:00
|
|
|
return res
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def createAction(self, statementGroup):
|
|
|
|
'''Tries to create an action based on p_statementGroup. If the statement
|
|
|
|
is not correct, r_ is -1. Else, r_ is the index of the element within
|
|
|
|
the buffer that is the object of the action.'''
|
|
|
|
res = -1
|
|
|
|
try:
|
|
|
|
# Check the whole statement group
|
|
|
|
if not statementGroup or (len(statementGroup) > 2):
|
|
|
|
raise ParsingError(BAD_STATEMENT_GROUP % str(statementGroup))
|
|
|
|
# Check the statement
|
|
|
|
statement = statementGroup[0]
|
|
|
|
aRes = self.actionRex.match(statement)
|
|
|
|
if not aRes:
|
|
|
|
raise ParsingError(BAD_STATEMENT % statement)
|
|
|
|
statementName, podElem, minus, actionType, subExpr = aRes.groups()
|
|
|
|
if not (podElem in PodElement.POD_ELEMS):
|
|
|
|
raise ParsingError(BAD_ELEMENT % podElem)
|
|
|
|
if minus and (not podElem in PodElement.MINUS_ELEMS):
|
|
|
|
raise ParsingError(
|
|
|
|
BAD_MINUS % (podElem, PodElement.MINUS_ELEMS))
|
|
|
|
indexPodElem = self.getIndex(podElem)
|
|
|
|
if indexPodElem == -1:
|
|
|
|
raise ParsingError(
|
|
|
|
ELEMENT_NOT_FOUND % (podElem, str([
|
|
|
|
e.__class__.__name__.lower() \
|
2015-10-27 15:10:24 -05:00
|
|
|
for e in list(self.elements.values())])))
|
2009-06-29 07:06:01 -05:00
|
|
|
podElem = self.elements[indexPodElem]
|
|
|
|
# Check the 'from' clause
|
|
|
|
fromClause = None
|
|
|
|
source = 'buffer'
|
|
|
|
if len(statementGroup) > 1:
|
|
|
|
fromClause = statementGroup[1]
|
|
|
|
source = 'from'
|
|
|
|
if not fromClause.startswith('from '):
|
|
|
|
raise ParsingError(BAD_FROM_CLAUSE % fromClause)
|
|
|
|
fromClause = fromClause[5:]
|
|
|
|
# Create the action
|
|
|
|
if actionType == 'if':
|
|
|
|
self.action = IfAction(statementName, self, subExpr, podElem,
|
|
|
|
minus, source, fromClause)
|
|
|
|
self.env.ifActions.append(self.action)
|
|
|
|
if self.action.name:
|
|
|
|
# We must register this action as a named action
|
2015-10-27 15:10:24 -05:00
|
|
|
if self.action.name in self.env.namedIfActions:
|
2009-06-29 07:06:01 -05:00
|
|
|
raise ParsingError(DUPLICATE_NAMED_IF)
|
|
|
|
self.env.namedIfActions[self.action.name] = self.action
|
|
|
|
elif actionType == 'else':
|
|
|
|
if not self.env.ifActions:
|
|
|
|
raise ParsingError(ELSE_WITHOUT_IF)
|
|
|
|
# Does the "else" action reference a named "if" action?
|
|
|
|
ifReference = subExpr.strip()
|
|
|
|
if ifReference:
|
2015-10-27 15:10:24 -05:00
|
|
|
if ifReference not in self.env.namedIfActions:
|
2009-06-29 07:06:01 -05:00
|
|
|
raise ParsingError(ELSE_WITHOUT_NAMED_IF % ifReference)
|
|
|
|
linkedIfAction = self.env.namedIfActions[ifReference]
|
|
|
|
# This "else" action "consumes" the "if" action: this way,
|
|
|
|
# it is not possible to define two "else" actions related to
|
|
|
|
# the same "if".
|
|
|
|
del self.env.namedIfActions[ifReference]
|
|
|
|
self.env.ifActions.remove(linkedIfAction)
|
|
|
|
else:
|
|
|
|
linkedIfAction = self.env.ifActions.pop()
|
|
|
|
self.action = ElseAction(statementName, self, None, podElem,
|
|
|
|
minus, source, fromClause,
|
|
|
|
linkedIfAction)
|
|
|
|
elif actionType == 'for':
|
|
|
|
forRes = MemoryBuffer.forRex.match(subExpr.strip())
|
|
|
|
if not forRes:
|
|
|
|
raise ParsingError(BAD_FOR_EXPRESSION % subExpr)
|
|
|
|
iter, subExpr = forRes.groups()
|
|
|
|
self.action = ForAction(statementName, self, subExpr, podElem,
|
|
|
|
minus, iter, source, fromClause)
|
2009-07-10 08:01:50 -05:00
|
|
|
elif actionType == 'with':
|
2013-03-19 11:07:11 -05:00
|
|
|
variables = self._getVariables(subExpr)
|
|
|
|
self.action = VariablesAction(statementName, self, podElem,
|
|
|
|
minus, variables, source, fromClause)
|
2009-06-29 07:06:01 -05:00
|
|
|
else: # null action
|
|
|
|
if not fromClause:
|
|
|
|
raise ParsingError(NULL_ACTION_ERROR)
|
|
|
|
self.action = NullAction(statementName, self, None, podElem,
|
|
|
|
None, source, fromClause)
|
|
|
|
res = indexPodElem
|
2015-10-27 15:10:24 -05:00
|
|
|
except ParsingError as ppe:
|
2009-06-29 07:06:01 -05:00
|
|
|
PodError.dump(self, ppe, removeFirstLine=True)
|
|
|
|
return res
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2013-03-15 10:50:28 -05:00
|
|
|
def createPxAction(self, elem, actionType, statement):
|
2013-06-25 16:22:33 -05:00
|
|
|
'''Creates a PX action and link it to this buffer. If an action is
|
|
|
|
already linked to this buffer (in self.action), this action is
|
|
|
|
chained behind the last action via self.action.subAction.'''
|
2013-03-15 10:50:28 -05:00
|
|
|
res = 0
|
2013-03-19 16:06:47 -05:00
|
|
|
statement = statement.strip()
|
2013-03-15 10:50:28 -05:00
|
|
|
if actionType == 'for':
|
2013-03-19 16:06:47 -05:00
|
|
|
forRes = MemoryBuffer.forRex.match(statement)
|
2013-03-15 10:50:28 -05:00
|
|
|
if not forRes:
|
|
|
|
raise ParsingError(BAD_FOR_EXPRESSION % statement)
|
|
|
|
iter, subExpr = forRes.groups()
|
2013-06-25 16:22:33 -05:00
|
|
|
action = ForAction('for', self, subExpr, elem, False, iter,
|
|
|
|
'buffer', None)
|
2013-03-15 10:50:28 -05:00
|
|
|
elif actionType == 'if':
|
2013-06-25 16:22:33 -05:00
|
|
|
action= IfAction('if', self, statement, elem, False, 'buffer', None)
|
2013-07-15 04:23:29 -05:00
|
|
|
elif actionType in ('var', 'var2'):
|
2013-03-19 11:07:11 -05:00
|
|
|
variables = self._getVariables(statement)
|
2013-06-25 16:22:33 -05:00
|
|
|
action = VariablesAction('var', self, elem, False, variables,
|
|
|
|
'buffer', None)
|
|
|
|
# Is it the first action for this buffer or not?
|
|
|
|
if not self.action:
|
|
|
|
self.action = action
|
|
|
|
else:
|
|
|
|
self.action.addSubAction(action)
|
2013-03-15 10:50:28 -05:00
|
|
|
return res
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def cut(self, index, keepFirstPart):
|
|
|
|
'''Cuts this buffer into 2 parts. Depending on p_keepFirstPart, the 1st
|
|
|
|
(from 0 to index-1) or the second (from index to the end) part of the
|
|
|
|
buffer is returned as a MemoryBuffer instance without parent; the other
|
|
|
|
part is self.'''
|
|
|
|
res = MemoryBuffer(self.env, None)
|
|
|
|
# Manage buffer meta-info (elements, expressions, subbuffers)
|
|
|
|
iter = BufferIterator(self)
|
|
|
|
subBuffersToDelete = []
|
|
|
|
elementsToDelete = []
|
|
|
|
mustShift = False
|
|
|
|
while iter.hasNext():
|
2015-10-27 15:10:24 -05:00
|
|
|
itemIndex, item = next(iter)
|
2009-06-29 07:06:01 -05:00
|
|
|
if keepFirstPart:
|
|
|
|
if itemIndex >= index:
|
|
|
|
newIndex = itemIndex-index
|
|
|
|
if isinstance(item, MemoryBuffer):
|
|
|
|
res.subBuffers[newIndex] = item
|
|
|
|
subBuffersToDelete.append(itemIndex)
|
|
|
|
else:
|
|
|
|
res.elements[newIndex] = item
|
|
|
|
elementsToDelete.append(itemIndex)
|
|
|
|
else:
|
|
|
|
if itemIndex < index:
|
|
|
|
if isinstance(item, MemoryBuffer):
|
|
|
|
res.subBuffers[itemIndex] = item
|
|
|
|
subBuffersToDelete.append(itemIndex)
|
|
|
|
else:
|
|
|
|
res.elements[itemIndex] = item
|
|
|
|
elementsToDelete.append(itemIndex)
|
|
|
|
else:
|
|
|
|
mustShift = True
|
|
|
|
if elementsToDelete:
|
|
|
|
for elemIndex in elementsToDelete:
|
|
|
|
del self.elements[elemIndex]
|
|
|
|
if subBuffersToDelete:
|
|
|
|
for subIndex in subBuffersToDelete:
|
|
|
|
del self.subBuffers[subIndex]
|
|
|
|
if mustShift:
|
|
|
|
elements = {}
|
2015-10-27 15:10:24 -05:00
|
|
|
for elemIndex, elem in self.elements.items():
|
2009-06-29 07:06:01 -05:00
|
|
|
elements[elemIndex-index] = elem
|
|
|
|
self.elements = elements
|
|
|
|
subBuffers = {}
|
2015-10-27 15:10:24 -05:00
|
|
|
for subIndex, buf in self.subBuffers.items():
|
2009-06-29 07:06:01 -05:00
|
|
|
subBuffers[subIndex-index] = buf
|
|
|
|
self.subBuffers = subBuffers
|
|
|
|
# Manage content
|
|
|
|
if keepFirstPart:
|
|
|
|
res.write(self.content[index:])
|
|
|
|
self.content = self.content[:index]
|
|
|
|
else:
|
|
|
|
res.write(self.content[:index])
|
|
|
|
self.content = self.content[index:]
|
|
|
|
return res
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def getElementIndexes(self, expressions=True):
|
|
|
|
res = []
|
2015-10-27 15:10:24 -05:00
|
|
|
for index, elem in self.elements.items():
|
2014-06-23 12:18:05 -05:00
|
|
|
condition = isinstance(elem, Expression) or \
|
|
|
|
isinstance(elem, Attributes)
|
2009-06-29 07:06:01 -05:00
|
|
|
if not expressions:
|
|
|
|
condition = not condition
|
|
|
|
if condition:
|
|
|
|
res.append(index)
|
|
|
|
return res
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def transferActionIndependentContent(self, actionElemIndex):
|
|
|
|
# Manage content to transfer to parent buffer
|
|
|
|
if actionElemIndex != 0:
|
|
|
|
actionIndependentBuffer = self.cut(actionElemIndex,
|
|
|
|
keepFirstPart=False)
|
|
|
|
actionIndependentBuffer.parent = self.parent
|
|
|
|
actionIndependentBuffer.transferAllContent()
|
|
|
|
self.parent.pushSubBuffer(self)
|
|
|
|
# Manage content to transfer to a child buffer
|
|
|
|
actionElemIndex = self.getIndex(
|
|
|
|
self.action.elem.__class__.__name__.lower())
|
|
|
|
# We recompute actionElemIndex because after cut it may have changed
|
|
|
|
elemIndexes = self.getElementIndexes(expressions=False)
|
|
|
|
elemIndexes.sort()
|
|
|
|
if elemIndexes.index(actionElemIndex) != (len(elemIndexes)-1):
|
|
|
|
# I must create a sub-buffer with the impactable elements after
|
|
|
|
# the action-related element
|
|
|
|
childBuffer = self.cut(elemIndexes[elemIndexes.index(
|
|
|
|
actionElemIndex)+1], keepFirstPart=True)
|
|
|
|
self.addSubBuffer(childBuffer)
|
|
|
|
res = childBuffer
|
|
|
|
else:
|
|
|
|
res = self
|
|
|
|
return res
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def getStartIndex(self, removeMainElems):
|
|
|
|
'''When I must dump the buffer, sometimes (if p_removeMainElems is
|
|
|
|
True), I must dump only a subset of it. This method returns the start
|
|
|
|
index of the buffer part I must dump.'''
|
2015-01-13 10:02:59 -06:00
|
|
|
if not removeMainElems: return 0
|
|
|
|
# Find the start position of the deepest element to remove
|
|
|
|
deepestElem = self.action.elem.DEEPEST_TO_REMOVE
|
|
|
|
pos = self.content.find('<%s' % deepestElem.elem)
|
|
|
|
pos = pos + len(deepestElem.elem)
|
|
|
|
# Now we must find the position of the end of this start tag,
|
|
|
|
# skipping potential attributes.
|
|
|
|
inAttrValue = False # Are we parsing an attribute value ?
|
|
|
|
endTagFound = False # Have we found the end of this tag ?
|
|
|
|
while not endTagFound:
|
|
|
|
pos += 1
|
|
|
|
nextChar = self.content[pos]
|
|
|
|
if (nextChar == '>') and not inAttrValue:
|
|
|
|
# Yes we have it
|
|
|
|
endTagFound = True
|
|
|
|
elif nextChar == '"':
|
|
|
|
inAttrValue = not inAttrValue
|
|
|
|
return pos + 1
|
2011-01-28 07:36:30 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
def getStopIndex(self, removeMainElems):
|
|
|
|
'''This method returns the stop index of the buffer part I must dump.'''
|
|
|
|
if removeMainElems:
|
|
|
|
ns = self.env.namespaces
|
|
|
|
deepestElem = self.action.elem.DEEPEST_TO_REMOVE
|
|
|
|
pos = self.content.rfind('</%s>' % deepestElem.getFullName(ns))
|
|
|
|
res = pos
|
|
|
|
else:
|
|
|
|
res = self.getLength()
|
|
|
|
return res
|
2010-11-26 10:30:46 -06:00
|
|
|
|
2015-01-13 10:02:59 -06:00
|
|
|
def removeAutomaticExpressions(self):
|
|
|
|
'''When a buffer has an action with minus=True, we must remove the
|
|
|
|
"columnsRepeat" expressions automatically inserted by pod. Else, we
|
|
|
|
will have problems when computing the index of the part to keep
|
|
|
|
(m_getStartIndex).'''
|
|
|
|
# Find the start position of the deepest element to remove
|
|
|
|
deepestElem = self.action.elem.DEEPEST_TO_REMOVE
|
|
|
|
pos = self.content.find('<%s' % deepestElem.elem)
|
|
|
|
for index in self.elements.keys():
|
|
|
|
if index < pos: del self.elements[index]
|
|
|
|
|
2010-11-26 10:30:46 -06:00
|
|
|
reTagContent = re.compile('<(?P<p>[\w-]+):(?P<f>[\w-]+)(.*?)>.*</(?P=p):' \
|
|
|
|
'(?P=f)>', re.S)
|
2013-06-25 05:04:23 -05:00
|
|
|
def evaluate(self, result, context, subElements=True,
|
|
|
|
removeMainElems=False):
|
|
|
|
'''Evaluates this buffer given the current p_context and add the result
|
|
|
|
into p_result. With pod, p_result is the root file buffer; with px
|
|
|
|
it is a memory buffer.'''
|
2009-06-29 07:06:01 -05:00
|
|
|
if not subElements:
|
2015-01-13 10:02:59 -06:00
|
|
|
# Dump the root tag in this buffer, but not its content
|
2010-11-26 10:30:46 -06:00
|
|
|
res = self.reTagContent.match(self.content.strip())
|
|
|
|
if not res: result.write(self.content)
|
|
|
|
else:
|
|
|
|
g = res.group
|
|
|
|
result.write('<%s:%s%s></%s:%s>' % (g(1),g(2),g(3),g(1),g(2)))
|
2009-06-29 07:06:01 -05:00
|
|
|
else:
|
2015-01-13 10:02:59 -06:00
|
|
|
if removeMainElems: self.removeAutomaticExpressions()
|
2009-06-29 07:06:01 -05:00
|
|
|
iter = BufferIterator(self)
|
|
|
|
currentIndex = self.getStartIndex(removeMainElems)
|
|
|
|
while iter.hasNext():
|
2015-10-27 15:10:24 -05:00
|
|
|
index, evalEntry = next(iter)
|
2009-06-29 07:06:01 -05:00
|
|
|
result.write(self.content[currentIndex:index])
|
|
|
|
currentIndex = index + 1
|
|
|
|
if isinstance(evalEntry, Expression):
|
|
|
|
try:
|
2013-06-25 05:04:23 -05:00
|
|
|
res, escape = evalEntry.evaluate(context)
|
2013-03-22 06:52:24 -05:00
|
|
|
if escape: result.dumpContent(res)
|
|
|
|
else: result.write(res)
|
2015-10-28 15:20:16 -05:00
|
|
|
except EvaluationError as e:
|
2015-02-26 02:53:53 -06:00
|
|
|
# This exception has already been treated (see the
|
|
|
|
# "except" block below). Simply re-raise it when needed.
|
|
|
|
if self.env.raiseOnError: raise e
|
2015-10-27 15:10:24 -05:00
|
|
|
except Exception as e:
|
2014-06-23 05:54:32 -05:00
|
|
|
if not self.env.raiseOnError:
|
2013-03-15 18:02:16 -05:00
|
|
|
PodError.dump(result, EVAL_EXPR_ERROR % (
|
2015-02-26 02:53:53 -06:00
|
|
|
evalEntry.expr, e))
|
2014-06-23 05:54:32 -05:00
|
|
|
else:
|
2015-02-26 02:53:53 -06:00
|
|
|
raise EvaluationError(EVAL_EXPR_ERROR % \
|
2015-03-06 08:54:14 -06:00
|
|
|
(evalEntry.expr, '\n'+Traceback.get(5)))
|
2013-06-26 06:44:31 -05:00
|
|
|
elif isinstance(evalEntry, Attributes) or \
|
|
|
|
isinstance(evalEntry, Attribute):
|
2013-06-25 05:04:23 -05:00
|
|
|
result.write(evalEntry.evaluate(context))
|
2009-06-29 07:06:01 -05:00
|
|
|
else: # It is a subBuffer
|
|
|
|
if evalEntry.action:
|
2013-06-25 05:04:23 -05:00
|
|
|
evalEntry.action.execute(result, context)
|
2009-06-29 07:06:01 -05:00
|
|
|
else:
|
|
|
|
result.write(evalEntry.content)
|
|
|
|
stopIndex = self.getStopIndex(removeMainElems)
|
|
|
|
if currentIndex < (stopIndex-1):
|
|
|
|
result.write(self.content[currentIndex:stopIndex])
|
2013-03-15 10:50:28 -05:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
'''Cleans the buffer content.'''
|
2015-10-27 15:10:24 -05:00
|
|
|
self.content = ''
|
2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|