Skip to content

Commit cb5bfc3

Browse files
author
Jonathan Van Eenwyk
committed
Sanitize dut_id for use in output file name
Sometimes the DUT id may contain characters that aren't appropriate for file names. Sanitize the dut_id, so that we can safely use any ID.
1 parent 95f15c4 commit cb5bfc3

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

openhtf/output/callbacks/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import collections
2424
import contextlib
25+
import os
26+
import re
2527
import shutil
2628
import tempfile
2729

@@ -90,6 +92,8 @@ def create_file_name(self, test_record):
9092
record_dict = data.convert_to_base_types(
9193
test_record, ignore_keys=('code_info', 'phases', 'log_records'))
9294
if self._pattern_formattable:
95+
record_dict['dut_id'] = self._get_valid_filename(record_dict['dut_id'])
96+
9397
return util.format_string(self.filename_pattern, record_dict)
9498
else:
9599
raise ValueError(
@@ -122,3 +126,9 @@ def __call__(self, test_record):
122126
else:
123127
raise TypeError('Expected string or iterable but got {}.'.format(
124128
type(serialized_record)))
129+
130+
@staticmethod
131+
def _get_valid_filename(s):
132+
# Reference: https://stackoverflow.com/a/46801075
133+
s = str(s).strip().replace(' ', '_')
134+
return re.sub(r'(?u)[^-\w.]', '_', s)

0 commit comments

Comments
 (0)