# Tai Sakuma <sakuma@fnal.gov>
from AlphaTwirl.ProgressBar import ProgressReport

##____________________________________________________________________________||
class EventLoop(object):
    def __init__(self, eventBuilder, component, readers):
        self.eventBuilder = eventBuilder
        self.component = component
        self.readers = readers

    def __call__(self, progressReporter = None):
        events = self.eventBuilder.build(self.component)
        for event in events:
            self.reportProgress(progressReporter, event)
            for reader in self.readers:
                reader.event(event)
        return self.readers

    def reportProgress(self, progressReporter, event):
        if progressReporter is None: return
        report = ProgressReport(name = self.component.name, done = event.iEvent + 1, total = event.nEvents)
        progressReporter.report(report)

##____________________________________________________________________________||
class EventReaderBundle(object):

    def __init__(self, eventBuilder, eventLoopRunner):
        self._eventBuilder = eventBuilder
        self._eventLoopRunner = eventLoopRunner
        self._packages = [ ]

    def addReaderPackage(self, package):
        self._packages.append(package)

    def begin(self):
        self._eventLoopRunner.begin()

    def read(self, component):
        readers = [package.make(component.name) for package in self._packages]
        eventLoop = EventLoop(self._eventBuilder, component, readers)
        self._eventLoopRunner.run(eventLoop)

    def end(self):
        self._eventLoopRunner.end()

        for package in self._packages:
            package.collect()

##____________________________________________________________________________||

@@ -75,16 +75,274 @@
 Report%0A%0A
+##____________________________________________________________________________%7C%7C%0Aclass ProgressReportWriter(object):%0A    def write(self, component, event):%0A        return ProgressReport(name = component.name, done = event.iEvent + 1, total = event.nEvents)%0A%0A
 ##______
@@ -431,32 +431,32 @@
 ntLoop(object):%0A
-
     def __init__
@@ -599,24 +599,83 @@
 rs = readers
+%0A        self.progressReportWriter = ProgressReportWriter()
 %0A%0A    def __
@@ -1077,17 +1077,22 @@
 eport = 
-P
+self.p
 rogressR
@@ -1096,24 +1096,29 @@
 ssReport
-(name = 
+Writer.write(
 self.com
@@ -1127,61 +1127,15 @@
 nent
-.name, done = event.iEvent + 1, total = event.nEvents
+, event
 )%0A  

add a new class ProgressReportWriter, make EventLoop use it