Skip to content

Commit

Permalink
Moved timelib MONTH_DICT to PyparsingSingleLineTextParser #910 (#3553)
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz authored Apr 12, 2021
1 parent 579a0f3 commit 8acb8de
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 40 deletions.
15 changes: 0 additions & 15 deletions plaso/lib/timelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@
# pylint: disable=missing-type-doc,missing-return-type-doc


MONTH_DICT = {
'jan': 1,
'feb': 2,
'mar': 3,
'apr': 4,
'may': 5,
'jun': 6,
'jul': 7,
'aug': 8,
'sep': 9,
'oct': 10,
'nov': 11,
'dec': 12}


class Timestamp(object):
"""Class for converting timestamps to Plaso timestamps.
Expand Down
3 changes: 1 addition & 2 deletions plaso/parsers/apache_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from plaso.containers import time_events
from plaso.lib import errors
from plaso.lib import definitions
from plaso.lib import timelib
from plaso.parsers import manager
from plaso.parsers import text_parser

Expand Down Expand Up @@ -182,7 +181,7 @@ def _GetISO8601String(self, structure):
month = self._GetValueFromStructure(structure, 'month')

try:
month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)
except AttributeError as exception:
raise ValueError('unable to parse month with error: {0!s}.'.format(
exception))
Expand Down
3 changes: 1 addition & 2 deletions plaso/parsers/mac_appfirewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from plaso.containers import time_events
from plaso.lib import errors
from plaso.lib import definitions
from plaso.lib import timelib
from plaso.parsers import logger
from plaso.parsers import manager
from plaso.parsers import text_parser
Expand Down Expand Up @@ -109,7 +108,7 @@ def _GetTimeElementsTuple(self, structure):

# Note that dfdatetime_time_elements.TimeElements will raise ValueError
# for an invalid month.
month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)

if month != 0 and month < self._last_month:
# Gap detected between years.
Expand Down
3 changes: 1 addition & 2 deletions plaso/parsers/mac_securityd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from plaso.containers import time_events
from plaso.lib import errors
from plaso.lib import definitions
from plaso.lib import timelib
from plaso.parsers import logger
from plaso.parsers import manager
from plaso.parsers import text_parser
Expand Down Expand Up @@ -118,7 +117,7 @@ def _GetTimeElementsTuple(self, structure):

# Note that dfdatetime_time_elements.TimeElements will raise ValueError
# for an invalid month.
month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)

if month != 0 and month < self._last_month:
# Gap detected between years.
Expand Down
3 changes: 1 addition & 2 deletions plaso/parsers/mac_wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from plaso.containers import time_events
from plaso.lib import errors
from plaso.lib import definitions
from plaso.lib import timelib
from plaso.parsers import logger
from plaso.parsers import manager
from plaso.parsers import text_parser
Expand Down Expand Up @@ -195,7 +194,7 @@ def _GetTimeElementsTuple(self, key, structure):

# Note that dfdatetime_time_elements.TimeElements will raise ValueError
# for an invalid month.
month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)

if month != 0 and month < self._last_month:
# Gap detected between years.
Expand Down
3 changes: 1 addition & 2 deletions plaso/parsers/syslog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from plaso.containers import time_events
from plaso.lib import errors
from plaso.lib import definitions
from plaso.lib import timelib
from plaso.parsers import logger
from plaso.parsers import manager
from plaso.parsers import text_parser
Expand Down Expand Up @@ -321,7 +320,7 @@ def ParseRecord(self, parser_mediator, key, structure):

month = self._GetValueFromStructure(structure, 'month')
try:
month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)
except AttributeError:
parser_mediator.ProduceExtractionWarning(
'invalid month value: {0!s}'.format(month))
Expand Down
14 changes: 14 additions & 0 deletions plaso/parsers/text_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ class PyparsingSingleLineTextParser(interface.FileObjectParser):
# Allow for a maximum of 40 empty lines before we bail out.
_MAXIMUM_DEPTH = 40

_MONTH_DICT = {
'jan': 1,
'feb': 2,
'mar': 3,
'apr': 4,
'may': 5,
'jun': 6,
'jul': 7,
'aug': 8,
'sep': 9,
'oct': 10,
'nov': 11,
'dec': 12}

def __init__(self):
"""Initializes a parser."""
super(PyparsingSingleLineTextParser, self).__init__()
Expand Down
3 changes: 1 addition & 2 deletions plaso/parsers/vsftpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from plaso.containers import time_events
from plaso.lib import errors
from plaso.lib import definitions
from plaso.lib import timelib
from plaso.parsers import logger
from plaso.parsers import text_parser
from plaso.parsers import manager
Expand Down Expand Up @@ -76,7 +75,7 @@ def _GetTimeElementsTuple(self, structure):
"""
time_elements_tuple = self._GetValueFromStructure(structure, 'date_time')
_, month, day_of_month, hours, minutes, seconds, year = time_elements_tuple
month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)
return (year, month, day_of_month, hours, minutes, seconds)


Expand Down
7 changes: 3 additions & 4 deletions plaso/parsers/xchatlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
from plaso.containers import time_events
from plaso.lib import errors
from plaso.lib import definitions
from plaso.lib import timelib
from plaso.parsers import logger
from plaso.parsers import manager
from plaso.parsers import text_parser
Expand Down Expand Up @@ -169,7 +168,7 @@ def _GetTimeElementsTuple(self, structure):
# TODO: what if time_elements_tuple is None.
month, day, hours, minutes, seconds = time_elements_tuple

month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)

if month != 0 and month < self._last_month:
# Gap detected between years.
Expand All @@ -190,7 +189,7 @@ def _ParseHeader(self, parser_mediator, structure):
# TODO: what if time_elements_tuple is None.
_, month, day, hours, minutes, seconds, year = time_elements_tuple

month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)

time_elements_tuple = (year, month, day, hours, minutes, seconds)

Expand Down Expand Up @@ -323,7 +322,7 @@ def VerifyStructure(self, parser_mediator, line):
time_elements_tuple))
return False

month = timelib.MONTH_DICT.get(month.lower(), 0)
month = self._MONTH_DICT.get(month.lower(), 0)

time_elements_tuple = (year, month, day, hours, minutes, seconds)

Expand Down
9 changes: 0 additions & 9 deletions tests/lib/timelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ def testCopyToIsoFormat(self):
timestamp, timezone=timezone)
self.assertEqual(date_time_string, '2013-03-14T21:20:08.850041+01:00')

def testMonthDict(self):
"""Test the month dict, both inside and outside of scope."""
self.assertEqual(timelib.MONTH_DICT['nov'], 11)
self.assertEqual(timelib.MONTH_DICT['jan'], 1)
self.assertEqual(timelib.MONTH_DICT['may'], 5)

month = timelib.MONTH_DICT.get('doesnotexist')
self.assertIsNone(month)

def testLocaltimeToUTC(self):
"""Test the localtime to UTC conversion."""
timezone = pytz.timezone('CET')
Expand Down

0 comments on commit 8acb8de

Please sign in to comment.