From fecf27165f9846d0e9d1d60c73c71400ec5d3f02 Mon Sep 17 00:00:00 2001 From: Andrew Katz Date: Thu, 16 Jul 2020 18:47:56 -0400 Subject: [PATCH] Fix unicode issue for windows (force an encoding) --- src/wireviz/Harness.py | 10 +++++----- src/wireviz/build_examples.py | 11 +++++++---- src/wireviz/wireviz.py | 8 ++++---- src/wireviz/wv_helper.py | 8 ++++++-- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index e06c2a3..28e4138 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -7,7 +7,7 @@ from wireviz import wv_colors, wv_helper from wireviz.wv_colors import get_color_hex from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \ nested_html_table, flatten2d, index_if_list, html_line_breaks, \ - graphviz_line_breaks, remove_line_breaks + graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write from collections import Counter from typing import List from pathlib import Path @@ -303,15 +303,15 @@ class Harness: graph.save(filename=f'{filename}.gv') # bom output bom_list = self.bom_list() - with open(f'{filename}.bom.tsv', 'w') as file: + with open_file_write(f'{filename}.bom.tsv') as file: file.write(tuplelist2tsv(bom_list)) # HTML output - with open(f'{filename}.html', 'w') as file: + with open_file_write(f'{filename}.html') as file: file.write('\n') - file.write('') + file.write('') file.write('

Diagram

') - with open(f'{filename}.svg') as svg: + with open_file_read(f'{filename}.svg') as svg: file.write(re.sub( '^<[?]xml [^?>]*[?]>[^<]*]*>', '', diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 530c5af..4713c0a 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -5,6 +5,9 @@ import os import sys from fnmatch import fnmatch +# noinspection PyUnresolvedReferences +from wv_helper import open_file_write, open_file_read + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from wireviz import wireviz @@ -25,7 +28,7 @@ def build_demos(): wireviz.parse_file(abspath) def build_examples(): - with open(os.path.join(examples_path, readme), 'w') as file: + with open_file_write(os.path.join(examples_path, readme)) as file: file.write('# Example gallery\n') for fn in os.listdir(examples_path): if fnmatch(fn, "ex*.yml"): @@ -43,7 +46,7 @@ def build_examples(): file.write(f'[Source]({fn}) - [Bill of Materials]({outfile_name}.bom.tsv)\n\n\n') def build_tutorials(): - with open(os.path.join(tutorials_path, readme), 'w') as file: + with open_file_write(os.path.join(tutorials_path, readme)) as file: file.write('# WireViz Tutorial\n') for fn in os.listdir(tutorials_path): if fnmatch(fn, "tutorial*.yml"): @@ -55,12 +58,12 @@ def build_tutorials(): outfile_name = abspath.split(".yml")[0] - with open(outfile_name + '.md', 'r') as info: + with open_file_read(outfile_name + '.md') as info: for line in info: file.write(line.replace('## ', '## {} - '.format(i))) file.write(f'\n[Source]({fn}):\n\n') - with open(abspath, 'r') as src: + with open_file_read(abspath) as src: file.write('```yaml\n') for line in src: file.write(line) diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 6c9a186..1805af9 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -14,7 +14,7 @@ if __name__ == '__main__': from wireviz.Harness import Harness -from wireviz.wv_helper import expand +from wireviz.wv_helper import expand, open_file_read def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any: @@ -198,7 +198,7 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None: - with open(yaml_file, 'r') as file: + with open_file_read(yaml_file) as file: yaml_input = file.read() if not file_out: @@ -228,14 +228,14 @@ def main(): print(f'Error: input file {args.input_file} inaccessible or does not exist, check path') sys.exit(1) - with open(args.input_file) as fh: + with open_file_read(args.input_file) as fh: yaml_input = fh.read() if args.prepend_file: if not os.path.exists(args.prepend_file): print(f'Error: prepend input file {args.prepend_file} inaccessible or does not exist, check path') sys.exit(1) - with open(args.prepend_file) as fh: + with open_file_read(args.prepend_file) as fh: prepend = fh.read() yaml_input = prepend + yaml_input diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 0f9a22f..024c3ef 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -3,8 +3,6 @@ from typing import List -from wireviz import wv_colors - awg_equiv_table = { '0.09': '28', '0.14': '26', @@ -114,3 +112,9 @@ def graphviz_line_breaks(inp): def remove_line_breaks(inp): return inp.replace('\n', ' ').rstrip() if isinstance(inp, str) else inp +def open_file_read(filename): + # TODO: Intelligently determine encoding + return open(filename, 'r', encoding='UTF-8') + +def open_file_write(filename): + return open(filename, 'w', encoding='UTF-8') \ No newline at end of file