util: add open_auto helper function

This helper function is meant to be used throughout autospec as a
replacement for most uses of open(), especially whenever reading
arbitrary data from upstream sources.

It reads/writes data in UTF-8 always, with invalid UTF-8 characters
escaped with Python's "surrogate" escaping mechanism. This ensures that
any Latin-1 or other non-UTF-8 compatible encoded data can successfully
be read in and later written out without data corruption.

Signed-off-by: Patrick McCarty <patrick.mccarty@intel.com>
This commit is contained in:
Patrick McCarty
2019-06-24 12:21:02 -07:00
committed by William Douglas
parent 3d7189ab95
commit dbf07550d5
+13
View File
@@ -96,3 +96,16 @@ def write_out(filename, content, mode="w", encode=None):
"""File.write convenience wrapper."""
with open(filename, mode, encoding=encode) as require_f:
require_f.write(content)
def open_auto(*args, **kwargs):
"""
Open a file with UTF-8 encoding, and "surrogate" escape characters that are
not valid UTF-8 to avoid data corruption.
"""
# 'encoding' and 'errors' are fourth and fifth positional arguments, so
# restrict the args tuple to (file, mode, buffering) at most
assert len(args) <= 3
assert 'encoding' not in kwargs
assert 'errors' not in kwargs
return open(*args, encoding="utf-8", errors="surrogateescape", **kwargs)