import anatomize.sentence.stream.base as sentence_stream
import io
import multiprocessing

class FileStream(object):
    
    def __init__(self, path):
       self.path = path

    def open(self):
        self.io = io.open(object.__getattribute__(self, "path"))

    def close(self):
        object.__getattribute__(self, "io").close()

    def __getattr__(self, attr):
        return getattr(object.__getattribute__(self, "io"), attr)


class ProviderConsumer(sentence_stream.AbstractProvider):

    def __init__(self, queue):
        self.queue = queue

    def next(self):
        r = self.queue.get()

        if r == None:
            raise EOFError()

        return r


class ProviderProducer(object):

    def __init__(self, nb_worker, **kwargs):
        queue_maxsize = kwargs.get("queue_maxsize", 40)

        self.nb_worker = nb_worker

        self.queue = multiprocessing.Queue(maxsize = queue_maxsize)
        self.provider_queue = multiprocessing.Queue()

        self.lock = multiprocessing.Lock()
        self.lock.acquire()

    def run(self):
        while True:
            p = self.provider_queue.get()
            if p == None:
                break

            p.open()

            try:
                while True:
                    self.queue.put(p.next())
            except EOFError:
                pass

            p.close()

            self.lock.release()

        for w in range(self.nb_worker):
            self.queue.put(None)
