From 1890be21c220a00d707e52ee2dd37e84809248fd Mon Sep 17 00:00:00 2001 From: nigelmegitt Date: Tue, 8 May 2018 16:41:11 +0100 Subject: [PATCH] Use `codecs.open` and ignore errors This strips out unencodable characters and if not fixes, at least masks #483 by using `codecs.open` and telling it to ignore errors. --- ebu_tt_live/carriage/filesystem.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ebu_tt_live/carriage/filesystem.py b/ebu_tt_live/carriage/filesystem.py index 09ad04ed7..1b2434570 100644 --- a/ebu_tt_live/carriage/filesystem.py +++ b/ebu_tt_live/carriage/filesystem.py @@ -9,6 +9,7 @@ import six import os import time +import codecs log = logging.getLogger(__name__) @@ -154,7 +155,7 @@ def emit_data(self, data, sequence_identifier=None, sequence_number=None, # can be selected once at the beginning and dereferenced rather than repeating # if statements. filepath = os.path.join(self._dirpath, filename) - with open(filepath, 'w') as destfile: + with codecs.open(filepath, mode='w', errors='ignore') as destfile: destfile.write(data) destfile.flush() @@ -199,7 +200,7 @@ def emit_data(self, data, sequence_identifier=None, sequence_number=None, new_manifest_line = CFG_MANIFEST_LINE_PATTERN.format( availability_time=timedelta_to_str_manifest(availability_time), filename=filename) - with open(self._manifest_path, 'a') as f: + with codecs.open(self._manifest_path, mode='a', errors='ignore') as f: f.write(new_manifest_line) @@ -237,11 +238,11 @@ def __init__(self, manifest_path, custom_consumer, do_tail): self._manifest_path = manifest_path self._custom_consumer = custom_consumer self._do_tail = do_tail - with open(manifest_path, 'r') as manifest: + with codecs.open(manifest_path, 'r') as manifest: self._manifest_lines_iter = iter(manifest.readlines()) def resume_reading(self): - with open(self._manifest_path, 'r') as manifest_file: + with codecs.open(self._manifest_path, 'r') as manifest_file: while True: manifest_line = manifest_file.readline() if not manifest_line: @@ -257,7 +258,7 @@ def resume_reading(self): availability_time_str, xml_file_name = manifest_line.rstrip().split(',') xml_file_path = os.path.join(self._dirpath, xml_file_name) xml_content = None - with open(xml_file_path, 'r') as xml_file: + with codecs.open(xml_file_path, 'r') as xml_file: xml_content = xml_file.read() data = [availability_time_str, xml_content] self._custom_consumer.on_new_data(data)