예제 #1
0
class Logger(component.Component):
    """
        Class to log events sent to it via the box router. It will default to capturing
        all events and storing them separately by EventSeries. Logger.all_events will have all
        events combined in correct order. 

        Paramters
        ---------
        series : list of EventSeries
        event_types : list of classes (default: event.Event)
    """
    def __init__(self, length=None, series=[], event_types=[event.Event]):
        super(Logger, self).__init__()

        self.length = length
        self.series = series
        self.event_types = event_types
        self.data = {}
        self.all_events = EventList(attrs=['timestamp'], repr_col=True)

        self._add_bindings()

    def _add_bindings(self):
        for series in self.series:
            self.add_series_binding(series, self.handle_events)

        for event_type in self.event_types:
            self.add_event_listener(event_type, self.handle_events)

    def handle_events(self, event):
        self.log(event)

    def get_key(self, event):
        try:
            key = event.series 
        except AttributeError:
            key = event.__class__

        return str(key)

    def log(self, event):
        key = self.get_key(event)
        lst = self.data.setdefault(key, EventList())
        lst.append(event)
        self.all_events.append(event)

    def __getattr__(self, key):
        if key in self.data:
            return self.data[key]
        raise AttributeError()

    def __repr__(self):
        out = []
        out.append(self.__class__.__name__)
        for k, v in self.data.iteritems():
            out.append("{0}: {1} items".format(k, len(v)))
        return '\n'.join(out)
예제 #2
0
    def __init__(self, length=None, series=[], event_types=[event.Event]):
        super(Logger, self).__init__()

        self.length = length
        self.series = series
        self.event_types = event_types
        self.data = {}
        self.all_events = EventList(attrs=['timestamp'], repr_col=True)

        self._add_bindings()