@@ -186,40 +186,37 @@ def FormatEventValues(self, event_values):
186
186
187
187
188
188
class EventFormatter (object ):
189
- """Base class to format event data using a format string.
190
-
191
- Define the (long) format string and the short format string by defining
192
- FORMAT_STRING and FORMAT_STRING_SHORT. The syntax of the format strings
193
- is similar to that of format() where the place holder for a certain
194
- event object attribute is defined as {attribute_name}.
189
+ """Base class to format event values.
195
190
196
191
Attributes:
197
192
custom_helpers (list[str]): identifiers of custom event formatter helpers.
198
193
helpers (list[EventFormatterHelper]): event formatter helpers.
199
194
"""
200
195
201
- # The data type is a unique identifier for the event data. The current
202
- # approach is to define it as human readable string in the format
203
- # root:branch: ... :leaf, e.g. a page visited entry inside a Chrome History
204
- # database is defined as: chrome:history:page_visited.
205
- DATA_TYPE = 'internal'
206
-
207
- # The format string.
208
- FORMAT_STRING = ''
209
- FORMAT_STRING_SHORT = ''
210
-
211
196
# The format string can be defined as:
212
197
# {name}, {name:format}, {name!conversion}, {name!conversion:format}
213
198
_FORMAT_STRING_ATTRIBUTE_NAME_RE = re .compile (
214
199
'{([a-z][a-zA-Z0-9_]*)[!]?[^:}]*[:]?[^}]*}' )
215
200
216
- def __init__ (self ):
217
- """Initializes an event formatter object."""
201
+ def __init__ (self , data_type = 'internal' ):
202
+ """Initializes an event formatter.
203
+
204
+ Args:
205
+ data_type (Optional[str]): unique identifier for the event data supported
206
+ by the formatter.
207
+ """
218
208
super (EventFormatter , self ).__init__ ()
209
+ self ._data_type = data_type
219
210
self ._format_string_attribute_names = None
211
+
220
212
self .custom_helpers = []
221
213
self .helpers = []
222
214
215
+ @property
216
+ def data_type (self ):
217
+ """str: unique identifier for the event data supported by the formatter."""
218
+ return self ._data_type .lower ()
219
+
223
220
def _FormatMessage (self , format_string , event_values ):
224
221
"""Determines the formatted message.
225
222
@@ -293,18 +290,13 @@ def FormatEventValues(self, event_values):
293
290
for helper in self .helpers :
294
291
helper .FormatEventValues (event_values )
295
292
293
+ @abc .abstractmethod
296
294
def GetFormatStringAttributeNames (self ):
297
295
"""Retrieves the attribute names in the format string.
298
296
299
297
Returns:
300
298
set(str): attribute names.
301
299
"""
302
- if self ._format_string_attribute_names is None :
303
- self ._format_string_attribute_names = (
304
- self ._FORMAT_STRING_ATTRIBUTE_NAME_RE .findall (
305
- self .FORMAT_STRING ))
306
-
307
- return set (self ._format_string_attribute_names )
308
300
309
301
# pylint: disable=unused-argument
310
302
def AddCustomHelper (
@@ -328,6 +320,7 @@ def AddHelper(self, helper):
328
320
"""
329
321
self .helpers .append (helper )
330
322
323
+ @abc .abstractmethod
331
324
def GetMessage (self , event_values ):
332
325
"""Determines the message.
333
326
@@ -337,8 +330,8 @@ def GetMessage(self, event_values):
337
330
Returns:
338
331
str: message.
339
332
"""
340
- return self ._FormatMessage (self .FORMAT_STRING , event_values )
341
333
334
+ @abc .abstractmethod
342
335
def GetMessageShort (self , event_values ):
343
336
"""Determines the short message.
344
337
@@ -348,10 +341,72 @@ def GetMessageShort(self, event_values):
348
341
Returns:
349
342
str: short message.
350
343
"""
351
- if self .FORMAT_STRING_SHORT :
352
- format_string = self .FORMAT_STRING_SHORT
344
+
345
+
346
+ class BasicEventFormatter (EventFormatter ):
347
+ """Format event values using a message format string.
348
+
349
+ Attributes:
350
+ custom_helpers (list[str]): identifiers of custom event formatter helpers.
351
+ helpers (list[EventFormatterHelper]): event formatter helpers.
352
+ """
353
+
354
+ def __init__ (
355
+ self , data_type = 'basic' , format_string = None , format_string_short = None ):
356
+ """Initializes a basic event formatter.
357
+
358
+ The syntax of the format strings is similar to that of format() where
359
+ the place holder for a certain event object attribute is defined as
360
+ {attribute_name}.
361
+
362
+ Args:
363
+ data_type (Optional[str]): unique identifier for the event data supported
364
+ by the formatter.
365
+ format_string (Optional[str]): (long) message format string.
366
+ format_string_short (Optional[str]): short message format string.
367
+ """
368
+ super (BasicEventFormatter , self ).__init__ (data_type = data_type )
369
+ self ._format_string_attribute_names = None
370
+ self ._format_string = format_string
371
+ self ._format_string_short = format_string_short
372
+
373
+ def GetFormatStringAttributeNames (self ):
374
+ """Retrieves the attribute names in the format string.
375
+
376
+ Returns:
377
+ set(str): attribute names.
378
+ """
379
+ if self ._format_string_attribute_names is None :
380
+ self ._format_string_attribute_names = (
381
+ self ._FORMAT_STRING_ATTRIBUTE_NAME_RE .findall (
382
+ self ._format_string ))
383
+
384
+ return set (self ._format_string_attribute_names )
385
+
386
+ def GetMessage (self , event_values ):
387
+ """Determines the message.
388
+
389
+ Args:
390
+ event_values (dict[str, object]): event values.
391
+
392
+ Returns:
393
+ str: message.
394
+ """
395
+ return self ._FormatMessage (self ._format_string , event_values )
396
+
397
+ def GetMessageShort (self , event_values ):
398
+ """Determines the short message.
399
+
400
+ Args:
401
+ event_values (dict[str, object]): event values.
402
+
403
+ Returns:
404
+ str: short message.
405
+ """
406
+ if self ._format_string_short :
407
+ format_string = self ._format_string_short
353
408
else :
354
- format_string = self .FORMAT_STRING
409
+ format_string = self ._format_string
355
410
356
411
short_message_string = self ._FormatMessage (format_string , event_values )
357
412
@@ -363,28 +418,38 @@ def GetMessageShort(self, event_values):
363
418
364
419
365
420
class ConditionalEventFormatter (EventFormatter ):
366
- """Base class to conditionally format event data using format string pieces.
421
+ """Conditionally format event values using format string pieces."""
367
422
368
- Define the (long) format string and the short format string by defining
369
- FORMAT_STRING_PIECES and FORMAT_STRING_SHORT_PIECES. The syntax of the
370
- format strings pieces is similar to of the event formatter
371
- (EventFormatter). Every format string piece should contain a single
372
- attribute name or none.
423
+ _DEFAULT_FORMAT_STRING_SEPARATOR = ' '
373
424
374
- FORMAT_STRING_SEPARATOR is used to control the string which the separate
375
- string pieces should be joined. It contains a space by default.
376
- """
377
- # The format string pieces.
378
- FORMAT_STRING_PIECES = ['' ]
379
- FORMAT_STRING_SHORT_PIECES = ['' ]
425
+ def __init__ (
426
+ self , data_type = 'conditional' , format_string_pieces = None ,
427
+ format_string_separator = None , format_string_short_pieces = None ):
428
+ """Initializes a conditional event formatter.
429
+
430
+ The syntax of the format strings pieces is similar to of the basic event
431
+ formatter (BasicEventFormatter). Every format string piece should contain
432
+ at maximum one unique attribute name. Format string pieces without an
433
+ attribute name are supported.
380
434
381
- # The separator used to join the string pieces.
382
- FORMAT_STRING_SEPARATOR = ' '
435
+ Args:
436
+ data_type (Optional[str]): unique identifier for the event data supported
437
+ by the formatter.
438
+ format_string_pieces (Optional[list[str]]): (long) message format string
439
+ pieces.
440
+ format_string_separator (Optional[str]): string by which separate format
441
+ string pieces should be joined.
442
+ format_string_short_pieces (Optional[list[str]]): short message format
443
+ string pieces.
444
+ """
445
+ if format_string_separator is None :
446
+ format_string_separator = self ._DEFAULT_FORMAT_STRING_SEPARATOR
383
447
384
- def __init__ (self ):
385
- """Initializes the conditional formatter."""
386
- super (ConditionalEventFormatter , self ).__init__ ()
448
+ super (ConditionalEventFormatter , self ).__init__ (data_type = data_type )
449
+ self ._format_string_pieces = format_string_pieces or []
387
450
self ._format_string_pieces_map = []
451
+ self ._format_string_separator = format_string_separator
452
+ self ._format_string_short_pieces = format_string_short_pieces or []
388
453
self ._format_string_short_pieces_map = []
389
454
390
455
def _CreateFormatStringMap (
@@ -433,11 +498,11 @@ def _CreateFormatStringMaps(self):
433
498
"""
434
499
self ._format_string_pieces_map = []
435
500
self ._CreateFormatStringMap (
436
- self .FORMAT_STRING_PIECES , self ._format_string_pieces_map )
501
+ self ._format_string_pieces , self ._format_string_pieces_map )
437
502
438
503
self ._format_string_short_pieces_map = []
439
504
self ._CreateFormatStringMap (
440
- self .FORMAT_STRING_SHORT_PIECES , self ._format_string_short_pieces_map )
505
+ self ._format_string_short_pieces , self ._format_string_short_pieces_map )
441
506
442
507
def _ConditionalFormatMessage (
443
508
self , format_string_pieces , format_string_pieces_map , event_values ):
@@ -469,7 +534,7 @@ def _ConditionalFormatMessage(
469
534
470
535
string_pieces .append (format_string_pieces [map_index ])
471
536
472
- format_string = self .FORMAT_STRING_SEPARATOR .join (string_pieces )
537
+ format_string = self ._format_string_separator .join (string_pieces )
473
538
474
539
return self ._FormatMessage (format_string , event_values )
475
540
@@ -481,7 +546,7 @@ def GetFormatStringAttributeNames(self):
481
546
"""
482
547
if self ._format_string_attribute_names is None :
483
548
self ._format_string_attribute_names = []
484
- for format_string_piece in self .FORMAT_STRING_PIECES :
549
+ for format_string_piece in self ._format_string_pieces :
485
550
attribute_names = self ._FORMAT_STRING_ATTRIBUTE_NAME_RE .findall (
486
551
format_string_piece )
487
552
@@ -503,7 +568,8 @@ def GetMessage(self, event_values):
503
568
self ._CreateFormatStringMaps ()
504
569
505
570
return self ._ConditionalFormatMessage (
506
- self .FORMAT_STRING_PIECES , self ._format_string_pieces_map , event_values )
571
+ self ._format_string_pieces , self ._format_string_pieces_map ,
572
+ event_values )
507
573
508
574
def GetMessageShort (self , event_values ):
509
575
"""Determines the short message.
@@ -517,12 +583,12 @@ def GetMessageShort(self, event_values):
517
583
if not self ._format_string_pieces_map :
518
584
self ._CreateFormatStringMaps ()
519
585
520
- if (self .FORMAT_STRING_SHORT_PIECES and
521
- self .FORMAT_STRING_SHORT_PIECES != ['' ]):
522
- format_string_pieces = self .FORMAT_STRING_SHORT_PIECES
586
+ if (self ._format_string_short_pieces and
587
+ self ._format_string_short_pieces != ['' ]):
588
+ format_string_pieces = self ._format_string_short_pieces
523
589
format_string_pieces_map = self ._format_string_short_pieces_map
524
590
else :
525
- format_string_pieces = self .FORMAT_STRING_PIECES
591
+ format_string_pieces = self ._format_string_pieces
526
592
format_string_pieces_map = self ._format_string_pieces_map
527
593
528
594
short_message_string = self ._ConditionalFormatMessage (
0 commit comments