Allow processors to retrieve their own value from the yield value, making it one line shorter each time the processor does not need its value before the teardown phase.

This commit is contained in:
Romain Dorgueil
2017-05-13 15:24:31 +02:00
parent 171fa3415b
commit 3560e974c4

View File

@ -49,6 +49,7 @@ class ContextCurrifier:
self.wrapped = wrapped self.wrapped = wrapped
self.context = tuple(initial_context) self.context = tuple(initial_context)
self._stack = [] self._stack = []
self._stack_values = []
def setup(self, *context): def setup(self, *context):
if len(self._stack): if len(self._stack):
@ -56,6 +57,7 @@ class ContextCurrifier:
for processor in resolve_processors(self.wrapped): for processor in resolve_processors(self.wrapped):
_processed = processor(self.wrapped, *context, *self.context) _processed = processor(self.wrapped, *context, *self.context)
_append_to_context = next(_processed) _append_to_context = next(_processed)
self._stack_values.append(_append_to_context)
if _append_to_context is not None: if _append_to_context is not None:
self.context += ensure_tuple(_append_to_context) self.context += ensure_tuple(_append_to_context)
self._stack.append(_processed) self._stack.append(_processed)
@ -68,7 +70,7 @@ class ContextCurrifier:
processor = self._stack.pop() processor = self._stack.pop()
try: try:
# todo yield from ? how to ? # todo yield from ? how to ?
next(processor) processor.send(self._stack_values.pop())
except StopIteration as exc: except StopIteration as exc:
# This is normal, and wanted. # This is normal, and wanted.
pass pass