#!/usr/bin/env python


# https://twistedmatrix.com/documents/16.2.0/core/howto/defer.html

from twisted.internet import reactor, defer
from twisted.internet.task import cooperate, coiterate
from twisted.internet.defer import gatherResults
from os.path import basename

from pprint import pprint
LONG = 15

def work_some(filename):
    base = basename(filename)
    with open(filename) as fp:
        for line in fp:
            if 'Sector' in line:
                print("{0:15} : {1}".format(base, line.strip()))
            yield

def work_done(value):
    print("Job done:", value)

# cooperate(work_some("../logs/proxy.log"))
# cooperate(work_some("../logs/proxy.log.2019_11_30"))

d1 = coiterate(work_some("../logs/proxy.log"))
# There's no way this way to tell work_done what file is done.
# d.addCallback(work_done)
d1.addCallback(lambda ignore: work_done('proxy.log'))

d2 = coiterate(work_some("../logs/proxy.log.2019_11_30"))
# d.addCallback(work_done)
d2.addCallback(lambda ignore: work_done('proxy.log.2019_11_30'))

# Gather the results, stop the reactor when they are all done.

gr = gatherResults([d1, d2])
gr.addCallback(lambda ignore: reactor.stop())

# Not needed, we stop when we need to.  ;)
# print("Stop the reactor in", LONG, "seconds...")
# reactor.callLater(LONG, reactor.stop)

print("starting the reactor")
reactor.run()


print("Reactor has been safely shutdown.  :P")