From 017f143a5fb3c082df4754f5764ad8845f64943b Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 22 Dec 2014 23:44:09 +0100 Subject: [PATCH] Add a binary mode to utils.read_file. This will be needed for #325. --- qutebrowser/utils/utils.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index d47920c71..4b40f997d 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -64,11 +64,13 @@ def compact_text(text, elidelength=None): return out -def read_file(filename): +def read_file(filename, binary=False): """Get the contents of a file contained with qutebrowser. Args: filename: The filename to open as string. + binary: Whether to return a binary string. + If False, the data is UTF-8-decoded. Return: The file contents as string. @@ -76,11 +78,17 @@ def read_file(filename): if hasattr(sys, 'frozen'): # cx_Freeze doesn't support pkg_resources :( fn = os.path.join(os.path.dirname(sys.executable), filename) - with open(fn, 'r', encoding='utf-8') as f: - return f.read() + if binary: + with open(fn, 'rb') as f: + return f.read() + else: + with open(fn, 'r', encoding='utf-8') as f: + return f.read() else: data = pkg_resources.resource_string(qutebrowser.__name__, filename) - return data.decode('UTF-8') + if not binary: + data = data.decode('UTF-8') + return data def actute_warning():