__author__ = 'blaz'


class FlowController:
    """
    Flow controller class used for wrapping the flow, and contorol the parsing
    process.
    """

    def __init__(self, config):
        """
        The flow is created from  single processing classes which are the
        implementation of saper.flow.flowelems.flow_elem interface.

        @type config: saper.config.ConfigLoader
        """
        self.elems = []
        elems_list = config.get('saper', 'flow_elems').split()
        for elem_class in elems_list:
            module = __import__('saper.flow.flowelems',fromlist=[elem_class])
            cls = getattr(module, elem_class)
            self.elems.append(cls(config))

    def process_flow(self, document):
        """
        Method processes given document via defined flow and returns the result.

        @type document: corpus2.DocumentPtr
        @rtype: corpus2.DocumentPtr
        """
        for elem in self.elems:
            document = elem.feed_flow(document)
        return document