Skip to content
Snippets Groups Projects
Commit 7413dc75 authored by Thomas Tanon's avatar Thomas Tanon
Browse files

Merge pull request #5 from ProjetPP/add-shortcuts

Add 'shortcuts' module.
parents a9e8e5b9 1b6c0e40
No related branches found
No related tags found
No related merge requests found
from .simplification import simplify
from ppp_datamodel.communication import TraceItem, Response
def traverse_until_fixpoint(predicate, tree):
"""Traverses the tree again and again until it is not modified."""
old_tree = None
tree = simplify(tree)
while tree and old_tree != tree:
old_tree = tree
tree = tree.traverse(predicate)
if not tree:
return None
tree = simplify(tree)
return tree
def build_answer(request, tree, measures, module_name):
trace = request.trace + [TraceItem(module_name, tree, measures)]
return Response(request.language, tree, measures, trace)
from ppp_libmodule.tests import PPPTestCase
from ppp_libmodule.http import HttpRequestHandler
from ppp_libmodule import shortcuts
from ppp_datamodel.nodes import Triple as T
from ppp_datamodel.nodes import Missing as M
from ppp_datamodel.nodes import Missing as R
from ppp_datamodel.communication import Response, TraceItem
def predicate(node):
if node == T(M(), M(), M()):
return R('foo')
elif node == R('foo'):
return R('bar')
elif node == M():
return node
else:
assert False, node
class RequestHandler:
def __init__(self, request):
self.request = request
def answer(self):
tree = self.request.tree.traverse(predicate)
if tree != self.request.tree:
# If we have modified the tree, it is relevant to return it
return [shortcuts.build_answer(self.request, tree, {}, 'test')]
else:
# Otherwise, we have nothing interesting to say.
return []
def app(environ, start_response):
"""Function called by the WSGI server."""
r = HttpRequestHandler(environ, start_response, RequestHandler).dispatch()
return r
class HttpTest(PPPTestCase(app)):
def testWorking(self):
t = T(M(), M(), M())
q = {'id': '1', 'language': 'en', 'tree': t.as_dict(),
'measures': {}, 'trace': []}
self.assertResponse(q, [Response('en', R('bar'), {},
[TraceItem('test', R('bar'), {})])])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment