Gaetan Delannay 2009-07-10 15:01:50 +02:00
parent ec17f900a6
commit 10eea7d735
19 changed files with 1770 additions and 1644 deletions

View file

@ -95,7 +95,7 @@ class BufferAction:
class IfAction(BufferAction):
'''Action that determines if we must include the content of the buffer in
the result or not.'''
the result or not.'''
def do(self):
if self.exprResult:
self.evaluateBuffer()
@ -122,7 +122,7 @@ class ElseAction(IfAction):
class ForAction(BufferAction):
'''Actions that will include the content of the buffer as many times as
specified by the action parameters.'''
specified by the action parameters.'''
def __init__(self, name, buffer, expr, elem, minus, iter, source, fromExpr):
BufferAction.__init__(self, name, buffer, expr, elem, minus, source,
fromExpr)
@ -202,4 +202,28 @@ class NullAction(BufferAction):
allows to insert in a buffer arbitrary odt content.'''
def do(self):
self.evaluateBuffer()
class VariableAction(BufferAction):
'''Action that allows to define a variable somewhere in the template.'''
def __init__(self, name, buffer, expr, elem, minus, varName, source,
fromExpr):
BufferAction.__init__(self, name, buffer, expr, elem, minus, source,
fromExpr)
self.varName = varName # Name of the variable
def do(self):
context = self.buffer.env.context
# Remember the variable hidden by our variable definition, if any
hasHiddenVariable = False
if context.has_key(self.varName):
hiddenVariable = context[self.varName]
hasHiddenVariable = True
# Add the variable to the context
context[self.varName] = self.exprResult
# Evaluate the buffer
self.evaluateBuffer()
# Restore hidden variable if any
if hasHiddenVariable:
context[self.varName] = hiddenVariable
else:
del context[self.varName]
# ------------------------------------------------------------------------------

View file

@ -21,7 +21,8 @@ import re
from appy.pod import PodError, XML_SPECIAL_CHARS
from appy.pod.elements import *
from appy.pod.actions import IfAction, ElseAction, ForAction, NullAction
from appy.pod.actions import IfAction, ElseAction, ForAction, VariableAction, \
NullAction
# ------------------------------------------------------------------------------
class ParsingError(Exception): pass
@ -62,6 +63,12 @@ ELSE_WITHOUT_IF = 'No previous "if" statement could be found for this "else" ' \
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
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}.'
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 ' \
@ -168,8 +175,9 @@ class FileBuffer(Buffer):
# ------------------------------------------------------------------------------
class MemoryBuffer(Buffer):
actionRex = re.compile('(?:(\w+)\s*\:\s*)?do\s+(\w+)(-)?' \
'(?:\s+(for|if|else)\s*(.*))?')
'(?:\s+(for|if|else|with)\s*(.*))?')
forRex = re.compile('\s*([\w\-_]+)\s+in\s+(.*)')
varRex = re.compile('\s*([\w\-_]+)\s+=\s+(.*)')
def __init__(self, env, parent):
Buffer.__init__(self, env, parent)
self.content = u''
@ -338,6 +346,13 @@ class MemoryBuffer(Buffer):
iter, subExpr = forRes.groups()
self.action = ForAction(statementName, self, subExpr, podElem,
minus, iter, source, fromClause)
elif actionType == 'with':
varRes = MemoryBuffer.varRex.match(subExpr.strip())
if not varRes:
raise ParsingError(BAD_VAR_EXPRESSION % subExpr)
varName, subExpr = varRes.groups()
self.action = VariableAction(statementName, self, subExpr,
podElem, minus, varName, source, fromClause)
else: # null action
if not fromClause:
raise ParsingError(NULL_ACTION_ERROR)

View file

@ -48,4 +48,7 @@ class OdfEnvironment(XmlEnvironment):
class OdfParser(XmlParser):
'''XML parser that is specific for parsing ODF files.'''
def __init__(self, env=None, caller=None):
if not env: env = OdfEnvironment()
XmlParser.__init__(self, env, caller)
# ------------------------------------------------------------------------------

View file

@ -285,7 +285,7 @@ class Renderer:
except OSError, oe:
raise PodError(CANT_WRITE_RESULT % (self.result, oe))
except IOError, ie:
raise PodError(CANT_WRITE_RESULT % (self.result, oe))
raise PodError(CANT_WRITE_RESULT % (self.result, ie))
self.result = os.path.abspath(self.result)
os.remove(self.result)
# Check that temp folder does not exist

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,2 @@
var1 = 'VAR1 not overridden'
var2 = 'VAR2 not overridden'

BIN
pod/test/results/varDef.odt Normal file

Binary file not shown.

Binary file not shown.