def addExpression(self, expression): try: self.dumpContent(Expression(expression).evaluate(self.env.context)) except Exception, e: PodError.dump(self, EVAL_EXPR_ERROR % (expression, e), dumpTb=False)
def manageError(self, result, context, errorMessage, originalError=None): '''Manage the encountered error: dump it into the buffer or raise an exception.''' if self.buffer.env.raiseOnError: if not self.buffer.pod: # Add in the error message the line nb where the errors occurs # within the PX. 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) # Integrate the traceback (at least, its last lines) errorMessage += '\n' + Traceback.get(6).decode('utf-8') if originalError: raise EvaluationError(originalError, errorMessage) raise Exception(errorMessage) # Create a temporary buffer to dump the error. If I reuse this buffer to # dump the error (what I did before), and we are, at some depth, in a # for loop, this buffer will contain the error message and not the # content to repeat anymore. It means that this error will also show up # for every subsequent iteration. tempBuffer = self.buffer.clone() PodError.dump(tempBuffer, errorMessage, withinElement=self.elem) tempBuffer.evaluate(result, context)
def evaluate(self, subElements=True, removeMainElems=False): result = self.getFileBuffer() if not subElements: # Dump the root tag in this buffer, but not its content. 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))) else: iter = BufferIterator(self) currentIndex = self.getStartIndex(removeMainElems) while iter.hasNext(): index, evalEntry = iter.next() result.write(self.content[currentIndex:index]) currentIndex = index + 1 if isinstance(evalEntry, Expression): try: result.dumpContent(evalEntry.evaluate(self.env.context)) except Exception, e: PodError.dump(result, EVAL_EXPR_ERROR % ( evalEntry.expr, e), dumpTb=False) else: # It is a subBuffer if evalEntry.action: evalEntry.action.execute() else: result.write(evalEntry.content) stopIndex = self.getStopIndex(removeMainElems) if currentIndex < (stopIndex-1): result.write(self.content[currentIndex:stopIndex])
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.''' if not subElements: # Dump the root tag in this buffer, but not its content. 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))) else: iter = BufferIterator(self) currentIndex = self.getStartIndex(removeMainElems) while iter.hasNext(): index, evalEntry = iter.next() result.write(self.content[currentIndex:index]) currentIndex = index + 1 if isinstance(evalEntry, Expression): try: res, escape = evalEntry.evaluate(context) if escape: result.dumpContent(res) else: result.write(res) except Exception, e: if not self.env.raiseOnError: PodError.dump(result, EVAL_EXPR_ERROR % ( evalEntry.expr, e), dumpTb=False) else: raise Exception(EVAL_EXPR_ERROR %(evalEntry.expr,e)) elif isinstance(evalEntry, Attributes) or \ isinstance(evalEntry, Attribute): result.write(evalEntry.evaluate(context)) else: # It is a subBuffer
def evaluate(self, subElements=True, removeMainElems=False): result = self.getFileBuffer() if not subElements: # Dump the root tag in this buffer, but not its content. 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))) else: iter = BufferIterator(self) currentIndex = self.getStartIndex(removeMainElems) while iter.hasNext(): index, evalEntry = iter.next() result.write(self.content[currentIndex:index]) currentIndex = index + 1 if isinstance(evalEntry, Expression): try: result.dumpContent(evalEntry.evaluate( self.env.context)) except Exception, e: PodError.dump(result, EVAL_EXPR_ERROR % (evalEntry.expr, e), dumpTb=False) else: # It is a subBuffer if evalEntry.action: evalEntry.action.execute() else: result.write(evalEntry.content) stopIndex = self.getStopIndex(removeMainElems) if currentIndex < (stopIndex - 1): result.write(self.content[currentIndex:stopIndex])
def manageError(self, result, context, errorMessage, dumpTb=True): '''Manage the encountered error: dump it into the buffer or raise an exception.''' if self.buffer.env.raiseOnError: if not self.buffer.pod: # Add in the error message the line nb where the errors occurs # within the PX. 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) raise Exception(errorMessage) # Create a temporary buffer to dump the error. If I reuse this buffer to # dump the error (what I did before), and we are, at some depth, in a # for loop, this buffer will contain the error message and not the # content to repeat anymore. It means that this error will also show up # for every subsequent iteration. tempBuffer = self.buffer.clone() PodError.dump(tempBuffer, errorMessage, withinElement=self.elem, dumpTb=dumpTb) tempBuffer.evaluate(result, context)
def writeError(self, errorMessage, dumpTb=True): # Empty the buffer self.buffer.__init__(self.buffer.env, self.buffer.parent) PodError.dump(self.buffer, errorMessage, withinElement=self.elem, dumpTb=dumpTb) self.buffer.evaluate()
def reifyNote(self): '''Recreate the note as close to the original as possible''' # Use some fake buffer and dump the note in it. Reuse the code for # dumping an error, also dumped as a note. r = self.StringBuffer() PodError.dump(r, '</text:p>\n<text:p>'.join(self.statements), dumpTb=False) return r
def createPodActions(self, statements): '''Tries to create action(s) based on p_statements. 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(s).''' r = -1 try: # Check that the statement group is not empty if not statements: raise ParsingError(EMPTY_NOTE) # Get the main statement (starting with "do...") and check it main = statements[0] aRes = self.Rex.action.match(main) if not aRes: raise ParsingError(BAD_STATEMENT % main) 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)) # Find the target element in the buffer i = self.getIndex(podElem) if i == -1: raise ParsingError(ELEMENT_NOT_FOUND % (podElem, str([ e.__class__.__name__.lower() \ for e in self.elements.values()]))) podElem = self.elements[i] # Create the main action self.action = self.createPodAction(actionType, statements, statementName, subExpr, podElem, minus) # Parse the remaining statements, that can contain any number of # secondary actions and a from clause. fromClause = last = None for statement in statements[1:]: # Get the "from" clause if statement.startswith('from') or \ statement.startswith('from+'): fromInfo = self.Rex.from_.match(statement) if not fromInfo: raise ParsingError(BAD_FROM_CLAUSE % fromClause) fromClause = fromInfo.groups() # Get any secondary statement else: info = self.Rex.subAction.match(statement) if not info: raise ParsingError(BAD_SUB_STATEMENT % statement) actionType, subExpr = info.groups() last = self.createPodAction(actionType, statements, '', subExpr, podElem, None, main=False) self.action.addSubAction(last) # Link the "from" clause if fromClause: target = last or self.action target.setFrom(*fromClause) success, msg = self.action.check() if not success: raise ParsingError(msg) r = i except ParsingError as ppe: PodError.dump(self, ppe, removeFirstLine=True) return r
def addExpression(self, expression, tiedHook=None): # At 2013-02-06, this method was not called within the whole test suite. try: expr = Expression(expression, self.pod) if tiedHook: tiedHook.tiedExpression = expr res, escape = expr.evaluate(self.env.context) if escape: self.dumpContent(res) else: self.write(res) except Exception, e: PodError.dump(self, EVAL_EXPR_ERROR % (expression, e), dumpTb=False)
def addExpression(self, expression, elem=None, tiedHook=None): try: expr = Expression(expression, self.pod) if tiedHook: tiedHook.tiedExpression = expr res, escape = expr.evaluate(self.env.context) if escape: self.dumpContent(res) else: self.write(res) except Exception as e: if not self.env.raiseOnError: PodError.dump(self, EVAL_EXPR_ERROR % (expression, e), dumpTb=False) else: raise Exception(EVAL_EXPR_ERROR % (expression, e))
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.''' if not subElements: # Dump the root tag in this buffer, but not its content 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))) else: if removeMainElems: self.removeAutomaticExpressions() iter = BufferIterator(self) currentIndex = self.getStartIndex(removeMainElems) while iter.hasNext(): index, evalEntry = next(iter) result.write(self.content[currentIndex:index]) currentIndex = index + 1 if isinstance(evalEntry, Expression): try: res, escape = evalEntry.evaluate(context) if escape: result.dumpContent(res) else: result.write(res) except EvaluationError as e: # This exception has already been treated (see the # "except" block below). Simply re-raise it when needed. if self.env.raiseOnError: raise e except Exception as e: if not self.env.raiseOnError: PodError.dump( result, EVAL_EXPR_ERROR % (evalEntry.expr, e)) else: raise EvaluationError(EVAL_EXPR_ERROR % \ (evalEntry.expr, '\n'+Traceback.get(5))) elif isinstance(evalEntry, Attributes) or \ isinstance(evalEntry, Attribute): result.write(evalEntry.evaluate(context)) else: # It is a subBuffer if evalEntry.action: evalEntry.action.execute(result, context) else: result.write(evalEntry.content) stopIndex = self.getStopIndex(removeMainElems) if currentIndex < (stopIndex - 1): result.write(self.content[currentIndex:stopIndex])
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.''' if not subElements: # Dump the root tag in this buffer, but not its content. 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))) else: iter = BufferIterator(self) currentIndex = self.getStartIndex(removeMainElems) while iter.hasNext(): index, evalEntry = iter.next() result.write(self.content[currentIndex:index]) currentIndex = index + 1 if isinstance(evalEntry, Expression): try: res, escape = evalEntry.evaluate(context) if escape: result.dumpContent(res) else: result.write(res) except (Exception, e): #print "OHHH YEAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" #result.dumpContent('') if not self.env.raiseOnError: PodError.dump(result, EVAL_EXPR_ERROR % (evalEntry.expr, e), dumpTb=False) else: raise Exception(EVAL_EXPR_ERROR % (evalEntry.expr, e)) elif isinstance(evalEntry, Attributes) or \ isinstance(evalEntry, Attribute): result.write(evalEntry.evaluate(context)) else: # It is a subBuffer if evalEntry.action: evalEntry.action.execute(result, context) else: result.write(evalEntry.content) stopIndex = self.getStopIndex(removeMainElems) if currentIndex < (stopIndex - 1): result.write(self.content[currentIndex:stopIndex])
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.''' if not subElements: # Dump the root tag in this buffer, but not its content 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))) else: if removeMainElems: self.removeAutomaticExpressions() iter = BufferIterator(self) currentIndex = self.getStartIndex(removeMainElems) while iter.hasNext(): index, evalEntry = next(iter) result.write(self.content[currentIndex:index]) currentIndex = index + 1 if isinstance(evalEntry, Expression): try: res, escape = evalEntry.evaluate(context) if escape: result.dumpContent(res) else: result.write(res) except EvaluationError as e: # This exception has already been treated (see the # "except" block below). Simply re-raise it when needed. if self.env.raiseOnError: raise e except Exception as e: if not self.env.raiseOnError: PodError.dump(result, EVAL_EXPR_ERROR % ( evalEntry.expr, e)) else: raise EvaluationError(EVAL_EXPR_ERROR % \ (evalEntry.expr, '\n'+Traceback.get(5))) elif isinstance(evalEntry, Attributes) or \ isinstance(evalEntry, Attribute): result.write(evalEntry.evaluate(context)) else: # It is a subBuffer if evalEntry.action: evalEntry.action.execute(result, context) else: result.write(evalEntry.content) stopIndex = self.getStopIndex(removeMainElems) if currentIndex < (stopIndex-1): result.write(self.content[currentIndex:stopIndex])
def manageError(self, result, context, errorMessage, dumpTb=True): '''Manage the encountered error: dump it into the buffer or raise an exception.''' if self.buffer.env.raiseOnError: if not self.buffer.pod: # Add in the error message the line nb where the errors occurs # within the PX. 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) raise Exception(errorMessage) # Empty the buffer (pod-only) self.buffer.__init__(self.buffer.env, self.buffer.parent) PodError.dump(self.buffer, errorMessage, withinElement=self.elem, dumpTb=dumpTb) self.buffer.evaluate(result, context)
def addExpression(self, expression, tiedHook=None): # At 2013-02-06, this method was not called within the whole test suite. try: self.dumpContent(Expression(expression).evaluate(self.env.context)) except Exception, e: PodError.dump(self, EVAL_EXPR_ERROR % (expression, e), dumpTb=False)
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() \ for e in self.elements.values()]))) 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 if self.env.namedIfActions.has_key(self.action.name): 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: if not self.env.namedIfActions.has_key(ifReference): 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) elif actionType == 'with': variables = self._getVariables(subExpr) self.action = VariablesAction(statementName, self, podElem, minus, variables, source, fromClause) else: # null action if not fromClause: raise ParsingError(NULL_ACTION_ERROR) self.action = NullAction(statementName, self, None, podElem, None, source, fromClause) res = indexPodElem except ParsingError, ppe: PodError.dump(self, ppe, removeFirstLine=True)
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() \ for e in self.elements.values()]))) 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 if self.env.namedIfActions.has_key(self.action.name): 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: if not self.env.namedIfActions.has_key(ifReference): 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) elif actionType == 'with': variables = self._getVariables(subExpr) self.action = VariablesAction(statementName, self, podElem, minus, variables, source, fromClause) else: # null action if not fromClause: raise ParsingError(NULL_ACTION_ERROR) self.action = NullAction(statementName, self, None, podElem, None, source, fromClause) res = indexPodElem except ParsingError, ppe: PodError.dump(self, ppe, removeFirstLine=True)