-
-
Save daniellopez0708/ae33148b6653f04d71569703299025b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PipelineRunner(object): | |
''' | |
Very simple pipline. | |
Just run passed processors in order with passing context from one to | |
another. | |
You can also set log level for processors. | |
''' | |
def __init__(self, pipeline=None, log_level=logging.INFO): | |
self.pipeline = pipeline or [] | |
self.context = {} | |
self.log = logging.getLogger(self.__class__.__name__) | |
self.log.setLevel(log_level) | |
self.log_level = log_level | |
self.set_log_level() | |
def set_context(self, data): | |
self.context = data | |
def add(self, processor): | |
if not isinstance(processor, PipelineProcessor): | |
raise Exception( | |
'Processor should be an isinstance of PipelineProcessor.') | |
processor.log.setLevel(self.log_level) | |
self.pipeline.append(processor) | |
def remove(self, name): | |
for i, p in enumerate(self.pipeline): | |
if p.__class__.__name__ == name: | |
del self.pipeline[i] | |
return True | |
return False | |
def set_log_level(self): | |
for p in self.pipeline: | |
p.log.setLevel(self.log_level) | |
def run(self): | |
for p in self.pipeline: | |
self.context = p(self.context) | |
self.log.debug("Frame #%d processed.", self.context['frame_number']) | |
return self.context | |
class PipelineProcessor(object): | |
''' | |
Base class for processors. | |
''' | |
def __init__(self): | |
self.log = logging.getLogger(self.__class__.__name__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment