diff options
| author | Michael Williamson <mike@zwobble.org> | 2018-11-02 20:10:18 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2018-11-02 20:10:49 +0000 |
| commit | a4e3fda8924860eabcba7fe3a77e4613c53e21d7 (patch) | |
| tree | 724c72813f135e9a2e5a5fff89140b3a49b686c3 /mammoth | |
| parent | 677dc9432e20d5bc099192163626bf15216c34ef (diff) | |
Always write files as UTF-8 in CLI
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/cli.py | 39 |
1 files changed, 14 insertions, 25 deletions
diff --git a/mammoth/cli.py b/mammoth/cli.py index 8161a3f..b759412 100644 --- a/mammoth/cli.py +++ b/mammoth/cli.py @@ -1,8 +1,8 @@ import argparse -import contextlib -import sys +import io import os import shutil +import sys import mammoth from . import writers @@ -36,8 +36,7 @@ def main(): sys.stderr.write(message.message) sys.stderr.write("\n") - with _open_output(output_path) as output: - output.write(result.value) + _write_output(output_path, result.value) class ImageWriter(object): @@ -57,28 +56,18 @@ class ImageWriter(object): return {"src": image_filename} -@contextlib.contextmanager -def _open_output(name): - if name is None: - try: - yield Output(sys.stdout) - finally: - sys.stdout.flush() - else: - with open(name, "w") as output: - yield Output(output) - - -class Output(object): - def __init__(self, fileobj): - self._fileobj = fileobj - - def write(self, value): - if sys.version_info[0] <= 2: - self._fileobj.write(value.encode("utf8")) +def _write_output(path, contents): + if path is None: + if sys.version_info[0] <= 2: + stdout = sys.stdout else: - self._fileobj.write(value) - + stdout = sys.stdout.buffer + + stdout.write(contents.encode("utf-8")) + stdout.flush() + else: + with io.open(path, "w", encoding="utf-8") as fileobj: + fileobj.write(contents) def _parse_args(): |
