Skip to content

Commit

Permalink
logging: Exclude raw log files in ReadDirectory()
Browse files Browse the repository at this point in the history
  • Loading branch information
asherbender committed Mar 9, 2016
1 parent 8271529 commit 82b6a66
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
48 changes: 36 additions & 12 deletions mcl/logging/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,11 @@ def __init__(self,
# logger.
for message in self.messages:
try:
if not issubclass(message, mcl.message.messages.Message):
msg = "The '%s' parameter must be a list/tuple of "
msg += "Message() objects."
raise TypeError(msg % 'messages')

LogConnection(os.path.join(directory, 'test'),
message,
revision=self.__revision,
Expand Down Expand Up @@ -1447,11 +1452,16 @@ class ReadDirectory(object):
source (str): Path to directory containing log files.
min_time (float): Minimum time to extract from log file in seconds.
max_time (float): Maximum time to extract from log file in seconds.
message (bool): If set to :py:data:`.True` messages will automatically
be decoded into the MCL message type stored in the log file. If set
to :py:data:`.False` (default), message data is returned as a
dictionary. Note: to read data as MCL messages, the messages must
be loaded into the namespace.
message (bool): If set to :py:data:`.False` (default), the logged data
is returned 'raw'. If set to :py:data:`.True` logged data will
automatically be decoded into the MCL message type stored in the
log file header. *Note*: to read data as MCL messages, the messages
must be loaded into the namespace.
ignore_raw (bool): If set to :py:data:`.True` (default), any raw log
files in the path `source` will be ignored. If set to
:py:data:`.False` an exception will be raised if any raw logs are
encountered.
Attributes:
messages (list): List of :py:class:`.Message` object stored in the
Expand All @@ -1470,7 +1480,8 @@ def __init__(self,
source,
min_time=None,
max_time=None,
message=False):
message=False,
ignore_raw=True):
"""Document the __init__ method at the class level."""

# Ensure source is specified as a string.
Expand All @@ -1491,6 +1502,11 @@ def __init__(self,
msg = "'message' must be a boolean."
raise TypeError(msg)

# Ignore raw log files.
if not isinstance(ignore_raw, bool):
msg = "'ignore_raw' must be a boolean."
raise TypeError(msg)

# Get all files in directory.
time_origin = None
self.__log_files = list()
Expand All @@ -1516,8 +1532,6 @@ def __init__(self,
min_time=min_time,
max_time=max_time,
message=self.__message)
self.__log_files.append(item)
self.__dumps.append(dump)
except:
raise

Expand All @@ -1526,6 +1540,20 @@ def __init__(self,
msg = "The dump file '%s' must have a header block."
raise ValueError(msg % os.path.join(source, f))

# Store message objects recorded in each log file.
if dump.header['type'] is None:
if ignore_raw:
continue
else:
msg = "The file '%s' contains raw data and cannot"
msg += "be loaded."
raise TypeError(msg % item)

# Save logging items.
self.__log_files.append(item)
self.__dumps.append(dump)
self.__messages.append(dump.header['type'])

# The header blocks must be created at the same time.
if not time_origin:
time_origin = dump.header['created']
Expand All @@ -1534,10 +1562,6 @@ def __init__(self,
msg += " Cannot continue."
raise ValueError(msg)

# Store message objects recorded in each log file.
if self.__message:
self.__messages.append(dump.header['type'])

# Store max/min time.
self.__min_time = min_time
self.__max_time = max_time
Expand Down
30 changes: 30 additions & 0 deletions mcl/logging/test/dataset/RawUnitTestData.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#-----------------------------------------------------------------
# MCL_LOG
# -- version 1.0
# -- revision
# -- created 1970-01-01 00:00:00
#
# Each line of this file records a packet of data transmitted over the
# network. The columns in this file are:
#
# 1) The time when the data frame was received relative
# to when this file was created.
# 2) The topic associated with the data frame.
# 3) The binary data stored as a hex string.
#
# The following data type was recorded in this file:
#
# >>> None
#
# <Time> <Topic> <Payload>
#-----------------------------------------------------------------
1.00000 '' 90
2.00000 '' 9100
3.00000 '' 9400010001
4.00000 '' 99000102000102000102
5.00000 '' dc001000010203000102030001020300010203
6.00000 '' dc001900010203040001020304000102030400010203040001020304
7.00000 '' dc0024000102030405000102030405000102030405000102030405000102030405000102030405
8.00000 '' dc003100010203040506000102030405060001020304050600010203040506000102030405060001020304050600010203040506
9.00000 '' dc004000010203040506070001020304050607000102030405060700010203040506070001020304050607000102030405060700010203040506070001020304050607
10.00000 '' dc0051000102030405060708000102030405060708000102030405060708000102030405060708000102030405060708000102030405060708000102030405060708000102030405060708000102030405060708
8 changes: 7 additions & 1 deletion mcl/logging/test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ def test_type(self):
def test_read_raw(self):
"""Test ReadFile() read raw file."""

rf = ReadFile(os.path.join(LOG_PATH, '../RawUnitTestData.log'))
rf = ReadFile(os.path.join(LOG_PATH, 'RawUnitTestData.log'))
self.assertEqual(rf.min_time, None)
self.assertEqual(rf.max_time, None)
self.assertEqual(rf.header['version'], '1.0')
Expand Down Expand Up @@ -765,6 +765,8 @@ def test_bad_init(self):
LogNetwork('connection', TMP_PATH)
with self.assertRaises(TypeError):
LogNetwork([UnitTestMessageA, 'connection'], TMP_PATH)
with self.assertRaises(TypeError):
LogNetwork([UnitTestMessageA, UnitTestMessageA.connection], TMP_PATH)

# Ensure max_entries is specified properly.
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -888,6 +890,10 @@ def test_initialisation(self):
def test_bad_init(self):
"""Test ReadDirectory() catches bad initialisation."""

# Raise error when raw logs are encountered.
with self.assertRaises(TypeError):
ReadDirectory(LOG_PATH, ignore_raw=False)

# Ensure failure if source is not a string.
with self.assertRaises(TypeError):
ReadDirectory(5)
Expand Down

0 comments on commit 82b6a66

Please sign in to comment.