Source code for xmm.pipeline.steps.debug

import sys
from itertools import islice
from pprint import pprint

from .base import PipelineStep


[docs]class DebugStep(PipelineStep): """ Debug step. Print parts of or the entire current state and or context. """ def __init__(self, state=False, context=False, err=False): """ Set up a new debug step. :param boolean|list state: Print the entire state or a slice of it. Slice form can be supplied as a tuple, e.g. ``[null, 2]`` is equal to ``[:2]``. When a slice is supplied, following steps will only act on this slice! :param boolean|str context: Print the context or only an item. :param boolean err: Print to STDERR instead of STDOUT. """ self.debug_state = state self.debug_context = context self.stream = sys.stderr if err else sys.stdout def map(self, state): if isinstance(self.debug_state, (list, tuple)): state = islice(state, *self.debug_state) for i, document in enumerate(state): if self.debug_state is not False: print(i, end=': ') pprint(document, stream=self.stream) yield document def process_step(self, state, context): return self.map(state), context def cleanup(self, context, exception): if self.debug_context: if isinstance(self.debug_context, str): pprint(context[self.debug_context], stream=self.stream) else: pprint(context, stream=self.stream) return context