Source code for zircon.transformers.base
"""
"""
from abc import ABCMeta, abstractmethod
[docs]class BaseTransformer():
""" Abstract base class defining the Transformer interface.
A Transformer takes in messages, applies some transformation, and spits
them back out. It is a general piece of middleware in a data stream that
can be used to compress/decompress, encode/decode, or split/combine
messages in a data pipeline.
**Usage**::
def process(msg):
do_something(msg)
t = MyTransformer()
t.set_callback(process)
for msg in messages:
t.push(msg)
"""
__metaclass__ = ABCMeta
@abstractmethod
[docs] def set_callback(self, callback):
""" Set a function to be invoked for each outputted message.
"""
raise NotImplementedError()
@abstractmethod
[docs]class Transformer(BaseTransformer):
""" Transformer that acts as a pass-through, invoking the callback for
each message received with no alterations.
Extend this and override :func:`push` to implement a Transformer.
"""
def __init__(self):
self.callback = None
[docs] def output(self, msg):
""" If I have a callback, invoke it.
"""
if self.callback:
self.callback(msg)