Skip to content

Commit

Permalink
Use codecs.open and ignore errors
Browse files Browse the repository at this point in the history
This strips out unencodable characters and if not fixes, at least masks #483 by using `codecs.open` and telling it to ignore errors.
  • Loading branch information
nigelmegitt committed May 8, 2018
1 parent 00a94c9 commit 1890be2
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions ebu_tt_live/carriage/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import six
import os
import time
import codecs


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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:
Expand All @@ -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)

0 comments on commit 1890be2

Please sign in to comment.