Fix unicode issue for windows (force an encoding)

This commit is contained in:
Andrew Katz 2020-07-16 18:47:56 -04:00 committed by Daniel Rojas
parent b147aa0eb1
commit fecf27165f
4 changed files with 22 additions and 15 deletions

View File

@ -7,7 +7,7 @@ from wireviz import wv_colors, wv_helper
from wireviz.wv_colors import get_color_hex from wireviz.wv_colors import get_color_hex
from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \ from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \
nested_html_table, flatten2d, index_if_list, html_line_breaks, \ 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 collections import Counter
from typing import List from typing import List
from pathlib import Path from pathlib import Path
@ -303,15 +303,15 @@ class Harness:
graph.save(filename=f'{filename}.gv') graph.save(filename=f'{filename}.gv')
# bom output # bom output
bom_list = self.bom_list() 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)) file.write(tuplelist2tsv(bom_list))
# HTML output # HTML output
with open(f'{filename}.html', 'w') as file: with open_file_write(f'{filename}.html') as file:
file.write('<!DOCTYPE html>\n') file.write('<!DOCTYPE html>\n')
file.write('<html><body style="font-family:Arial">') file.write('<html><head><meta charset="UTF-8"></head><body style="font-family:Arial">')
file.write('<h1>Diagram</h1>') file.write('<h1>Diagram</h1>')
with open(f'{filename}.svg') as svg: with open_file_read(f'{filename}.svg') as svg:
file.write(re.sub( file.write(re.sub(
'^<[?]xml [^?>]*[?]>[^<]*<!DOCTYPE [^>]*>', '^<[?]xml [^?>]*[?]>[^<]*<!DOCTYPE [^>]*>',
'<!-- XML and DOCTYPE declarations from SVG file removed -->', '<!-- XML and DOCTYPE declarations from SVG file removed -->',

View File

@ -5,6 +5,9 @@ import os
import sys import sys
from fnmatch import fnmatch 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__), '..')) sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from wireviz import wireviz from wireviz import wireviz
@ -25,7 +28,7 @@ def build_demos():
wireviz.parse_file(abspath) wireviz.parse_file(abspath)
def build_examples(): 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') file.write('# Example gallery\n')
for fn in os.listdir(examples_path): for fn in os.listdir(examples_path):
if fnmatch(fn, "ex*.yml"): 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') file.write(f'[Source]({fn}) - [Bill of Materials]({outfile_name}.bom.tsv)\n\n\n')
def build_tutorials(): 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') file.write('# WireViz Tutorial\n')
for fn in os.listdir(tutorials_path): for fn in os.listdir(tutorials_path):
if fnmatch(fn, "tutorial*.yml"): if fnmatch(fn, "tutorial*.yml"):
@ -55,12 +58,12 @@ def build_tutorials():
outfile_name = abspath.split(".yml")[0] 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: for line in info:
file.write(line.replace('## ', '## {} - '.format(i))) file.write(line.replace('## ', '## {} - '.format(i)))
file.write(f'\n[Source]({fn}):\n\n') 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') file.write('```yaml\n')
for line in src: for line in src:
file.write(line) file.write(line)

View File

@ -14,7 +14,7 @@ if __name__ == '__main__':
from wireviz.Harness import Harness 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: 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: 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() yaml_input = file.read()
if not file_out: 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') print(f'Error: input file {args.input_file} inaccessible or does not exist, check path')
sys.exit(1) sys.exit(1)
with open(args.input_file) as fh: with open_file_read(args.input_file) as fh:
yaml_input = fh.read() yaml_input = fh.read()
if args.prepend_file: if args.prepend_file:
if not os.path.exists(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') print(f'Error: prepend input file {args.prepend_file} inaccessible or does not exist, check path')
sys.exit(1) sys.exit(1)
with open(args.prepend_file) as fh: with open_file_read(args.prepend_file) as fh:
prepend = fh.read() prepend = fh.read()
yaml_input = prepend + yaml_input yaml_input = prepend + yaml_input

View File

@ -3,8 +3,6 @@
from typing import List from typing import List
from wireviz import wv_colors
awg_equiv_table = { awg_equiv_table = {
'0.09': '28', '0.09': '28',
'0.14': '26', '0.14': '26',
@ -114,3 +112,9 @@ def graphviz_line_breaks(inp):
def remove_line_breaks(inp): def remove_line_breaks(inp):
return inp.replace('\n', ' ').rstrip() if isinstance(inp, str) else 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')