def __init__(self, server_url, parse_event_body=True, last_event_seen=None): self.server_url = server_url self.last_event_seen = last_event_seen self.deserializer = Deserializer() self.parse_event_body = parse_event_body self.stream_finished = False
def __init__(self, url, event_callback=None, error_callback=None, connection_close_callback=None, source_start_callback=None, source_finish_callback=None, ioloop=None, parse_event_body=True, separate_events=True, reconnect=True, disable_compression=False): """Creates a new client for a given stream URL. The client connects to the stream URL given by 'url'. For every single received event, the 'event_callback' function is invoked. It receives an event object as parameter. If 'separate_events' is set to None, then the event callback will receive a list of events instead of a single events. If a 'ioloop' object is given, the client will block on it apon calling the 'start()' method. If not, it will block on the default 'ioloop' of Tornado. """ if isinstance(event_callback, Filter): event_callback = event_callback.filter_event self.url = url self.event_callback = event_callback self.error_callback = error_callback self.connection_close_callback = connection_close_callback self.source_start_callback = source_start_callback self.source_finish_callback = source_finish_callback self.ioloop = ioloop or tornado.ioloop.IOLoop.instance() self.parse_event_body = parse_event_body self.separate_events = separate_events self._closed = False self._looping = False self._deserializer = Deserializer() self.last_event = None self.reconnect = reconnect self.disable_compression = disable_compression self.connection_attempts = 0
def count_events(filename): """Counts the number of events in a file and their total size. Returns a tuple (num_events, num_bytes). """ num_bytes = 0 num_events = 0 if filename.endswith('.gz'): file_ = gzip.GzipFile(filename, 'r') else: file_ = open(filename, 'r') deserializer = Deserializer() while True: data = file_.read(1024) if data == '': break evs = deserializer.deserialize(data, parse_body=False, complete=False) num_bytes += len(data) num_events += len(evs) file_.close() return num_events, num_bytes