OLD | NEW |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """The fake storage intended for testing.""" | 2 """The fake storage intended for testing.""" |
3 | 3 |
4 import heapq | |
5 | |
6 from plaso.lib import definitions | 4 from plaso.lib import definitions |
7 from plaso.lib import py2to3 | |
8 from plaso.storage import interface | 5 from plaso.storage import interface |
9 | 6 from plaso.storage import event_heaps |
10 | |
11 class _EventsHeap(object): | |
12 """Events heap.""" | |
13 | |
14 def __init__(self): | |
15 """Initializes an events heap.""" | |
16 super(_EventsHeap, self).__init__() | |
17 self._heap = [] | |
18 | |
19 def PopEvent(self): | |
20 """Pops an event from the heap. | |
21 | |
22 Returns: | |
23 EventObject: event. | |
24 """ | |
25 try: | |
26 _, _, _, event = heapq.heappop(self._heap) | |
27 return event | |
28 | |
29 except IndexError: | |
30 return None | |
31 | |
32 def PopEvents(self): | |
33 """Pops events from the heap. | |
34 | |
35 Yields: | |
36 EventObject: event. | |
37 """ | |
38 event = self.PopEvent() | |
39 while event: | |
40 yield event | |
41 event = self.PopEvent() | |
42 | |
43 def PushEvent(self, event): | |
44 """Pushes an event onto the heap. | |
45 | |
46 Args: | |
47 event (EventObject): event. | |
48 """ | |
49 # TODO: replace this work-around for an event "comparable". | |
50 event_values = event.CopyToDict() | |
51 attributes = [] | |
52 for attribute_name, attribute_value in sorted(event_values.items()): | |
53 if isinstance(attribute_value, dict): | |
54 attribute_value = sorted(attribute_value.items()) | |
55 | |
56 elif isinstance(attribute_value, py2to3.BYTES_TYPE): | |
57 attribute_value = repr(attribute_value) | |
58 | |
59 comparable = u'{0:s}: {1!s}'.format(attribute_name, attribute_value) | |
60 attributes.append(comparable) | |
61 | |
62 comparable = u', '.join(attributes) | |
63 event_values = sorted(event.CopyToDict().items()) | |
64 heap_values = (event.timestamp, event.timestamp_desc, comparable, event) | |
65 heapq.heappush(self._heap, heap_values) | |
66 | 7 |
67 | 8 |
68 class FakeStorageWriter(interface.StorageWriter): | 9 class FakeStorageWriter(interface.StorageWriter): |
69 """Class that implements a fake storage writer object. | 10 """Class that implements a fake storage writer object. |
70 | 11 |
71 Attributes: | 12 Attributes: |
72 analysis_reports (list[AnalysisReport]): analysis reports. | 13 analysis_reports (list[AnalysisReport]): analysis reports. |
73 errors (list[ExtractionError]): extraction errors. | 14 errors (list[ExtractionError]): extraction errors. |
74 event_sources (list[EventSource]): event sources. | 15 event_sources (list[EventSource]): event sources. |
75 event_tags (list[EventTag]): event tags. | 16 event_tags (list[EventTag]): event tags. |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 228 |
288 Returns: | 229 Returns: |
289 generator(EventObject): event generator. | 230 generator(EventObject): event generator. |
290 | 231 |
291 Raises: | 232 Raises: |
292 IOError: when the storage writer is closed. | 233 IOError: when the storage writer is closed. |
293 """ | 234 """ |
294 if not self._is_open: | 235 if not self._is_open: |
295 raise IOError(u'Unable to read from closed storage writer.') | 236 raise IOError(u'Unable to read from closed storage writer.') |
296 | 237 |
297 events_heap = _EventsHeap() | 238 event_heap = event_heaps.EventHeap() |
298 | 239 |
299 for event in self._events: | 240 for event in self._events: |
300 if (time_range and ( | 241 if (time_range and ( |
301 event.timestamp < time_range.start_timestamp or | 242 event.timestamp < time_range.start_timestamp or |
302 event.timestamp > time_range.end_timestamp)): | 243 event.timestamp > time_range.end_timestamp)): |
303 continue | 244 continue |
304 | 245 |
305 events_heap.PushEvent(event) | 246 event_heap.PushEvent(event) |
306 | 247 |
307 return iter(events_heap.PopEvents()) | 248 return iter(event_heap.PopEvents()) |
308 | 249 |
309 def Open(self): | 250 def Open(self): |
310 """Opens the storage writer. | 251 """Opens the storage writer. |
311 | 252 |
312 Raises: | 253 Raises: |
313 IOError: if the storage writer is already opened. | 254 IOError: if the storage writer is already opened. |
314 """ | 255 """ |
315 if self._is_open: | 256 if self._is_open: |
316 raise IOError(u'Storage writer already opened.') | 257 raise IOError(u'Storage writer already opened.') |
317 | 258 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 Raises: | 376 Raises: |
436 IOError: if the storage type does not support writing a task | 377 IOError: if the storage type does not support writing a task |
437 start or when the storage writer is closed. | 378 start or when the storage writer is closed. |
438 """ | 379 """ |
439 self._RaiseIfNotWritable() | 380 self._RaiseIfNotWritable() |
440 | 381 |
441 if self._storage_type != definitions.STORAGE_TYPE_TASK: | 382 if self._storage_type != definitions.STORAGE_TYPE_TASK: |
442 raise IOError(u'Task start not supported by storage type.') | 383 raise IOError(u'Task start not supported by storage type.') |
443 | 384 |
444 self.task_start = self._task.CreateTaskStart() | 385 self.task_start = self._task.CreateTaskStart() |
OLD | NEW |