Skip to content
Snippets Groups Projects
Commit 7c779b3a authored by Ruben Verweij's avatar Ruben Verweij
Browse files

Small refactor of common functions

parent cb2c06f9
No related branches found
No related tags found
No related merge requests found
...@@ -115,28 +115,15 @@ def parse_date(text_info): ...@@ -115,28 +115,15 @@ def parse_date(text_info):
for line in text_info.values(): for line in text_info.values():
line = line.decode("utf8") line = line.decode("utf8")
# ND2s seem to randomly switch between 12- and 24-hour representations. # ND2s seem to randomly switch between 12- and 24-hour representations.
absolute_start_24 = _parse_date_24h(line)
absolute_start_12 = _parse_date_12h(line)
if not absolute_start_12 and not absolute_start_24:
continue
return absolute_start_12 if absolute_start_12 else absolute_start_24
return None
def _parse_date_12h(line):
try: try:
absolute_start_12 = datetime.strptime(line, "%m/%d/%Y %I:%M:%S %p") absolute_start = datetime.strptime(line, "%m/%d/%Y %H:%M:%S")
return absolute_start_12
except (TypeError, ValueError): except (TypeError, ValueError):
return None
def _parse_date_24h(line):
try: try:
absolute_start_24 = datetime.strptime(line, "%m/%d/%Y %H:%M:%S") absolute_start = datetime.strptime(line, "%m/%d/%Y %I:%M:%S %p")
return absolute_start_24
except (TypeError, ValueError): except (TypeError, ValueError):
return None absolute_start = None
return absolute_start
def _parse_metadata_item(data, cursor_position): def _parse_metadata_item(data, cursor_position):
...@@ -176,19 +163,38 @@ def read_metadata(data, count): ...@@ -176,19 +163,38 @@ def read_metadata(data, count):
""" """
if data is None: if data is None:
return None return None
data = six.BytesIO(data) data = six.BytesIO(data)
metadata = {} metadata = {}
for _ in range(count): for _ in range(count):
cursor_position = data.tell() cursor_position = data.tell()
header = data.read(2) header = data.read(2)
if not header: if not header:
# We've reached the end of some hierarchy of data # We've reached the end of some hierarchy of data
break break
if six.PY3: if six.PY3:
header = header.decode("utf8") header = header.decode("utf8")
data_type, name_length = map(ord, header) data_type, name_length = map(ord, header)
name = data.read(name_length * 2).decode("utf16")[:-1].encode("utf8") name = data.read(name_length * 2).decode("utf16")[:-1].encode("utf8")
value = _get_value(data, data_type, cursor_position) value = _get_value(data, data_type, cursor_position)
metadata = _add_to_metadata(metadata, name, value)
return metadata
def _add_to_metadata(metadata, name, value):
"""
Add the name value pair to the metadata dict
:param metadata:
:param name:
:param value:
:return:
"""
if name not in metadata.keys(): if name not in metadata.keys():
metadata[name] = value metadata[name] = value
else: else:
...@@ -196,7 +202,9 @@ def read_metadata(data, count): ...@@ -196,7 +202,9 @@ def read_metadata(data, count):
# We have encountered this key exactly once before. Since we're seeing it again, we know we # We have encountered this key exactly once before. Since we're seeing it again, we know we
# need to convert it to a list before proceeding. # need to convert it to a list before proceeding.
metadata[name] = [metadata[name]] metadata[name] = [metadata[name]]
# We've encountered this key before so we're guaranteed to be dealing with a list. Thus we append # We've encountered this key before so we're guaranteed to be dealing with a list. Thus we append
# the value to the already-existing list. # the value to the already-existing list.
metadata[name].append(value) metadata[name].append(value)
return metadata return metadata
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment