Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions openhtf/output/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import collections
import contextlib
import os
import re
import shutil
import tempfile

Expand Down Expand Up @@ -90,6 +92,8 @@ def create_file_name(self, test_record):
record_dict = data.convert_to_base_types(
test_record, ignore_keys=('code_info', 'phases', 'log_records'))
if self._pattern_formattable:
record_dict['dut_id'] = self._get_valid_filename(record_dict['dut_id'])

return util.format_string(self.filename_pattern, record_dict)
else:
raise ValueError(
Expand Down Expand Up @@ -122,3 +126,9 @@ def __call__(self, test_record):
else:
raise TypeError('Expected string or iterable but got {}.'.format(
type(serialized_record)))

@staticmethod
def _get_valid_filename(s):
# Reference: https://stackoverflow.com/a/46801075
s = str(s).strip().replace(' ', '_')
return re.sub(r'(?u)[^-\w.]', '_', s)
Comment on lines +131 to +134
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've hit an issue before where the filename was too long for the filesystem, this could be guarded against too here.