Skip to content

Commit 950bddc

Browse files
author
Jacob Erickson
committed
Change png load and argpares to pathlib styling
Adjusted some typehints that linting was complaining about.
1 parent ea61ab1 commit 950bddc

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

src/wireviz/wireviz.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
from pathlib import Path
77
import sys
8-
from typing import Any, Tuple
8+
from typing import Any, Tuple, Optional, Union
99

1010
import yaml
1111
from PIL import Image
@@ -16,10 +16,10 @@
1616

1717
from wireviz import __version__
1818
from wireviz.Harness import Harness
19-
from wireviz.wv_helper import expand, open_file_read
19+
from wireviz.wv_helper import expand, open_file_read, open_file_write
2020

2121

22-
def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any:
22+
def parse(yaml_input: str, file_out: Union[str, Path] = None, return_types: Optional[Union[str, Tuple[str]]] = None) -> Any:
2323
"""
2424
Parses yaml input string and does the high-level harness conversion
2525
@@ -203,7 +203,7 @@ def check_designators(what, where): # helper function
203203
return tuple(returns) if len(returns) != 1 else returns[0]
204204

205205

206-
def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None:
206+
def parse_file(yaml_file: str, file_out: Union[str, Path] = None) -> None:
207207
with open_file_read(yaml_file) as file:
208208
yaml_input = file.read()
209209

@@ -220,56 +220,50 @@ def parse_cmdline():
220220
description='Generate cable and wiring harness documentation from YAML descriptions',
221221
)
222222
parser.add_argument('-V', '--version', action='version', version='%(prog)s ' + __version__)
223-
parser.add_argument('input_file', action='store', type=str, metavar='YAML_FILE')
224-
parser.add_argument('-o', '--output_file', action='store', type=str, metavar='OUTPUT')
223+
parser.add_argument('input_file', action='store', type=Path, metavar='YAML_FILE')
224+
parser.add_argument('-o', '--output_file', action='store', type=Path, metavar='OUTPUT')
225225
# Not implemented: parser.add_argument('--generate-bom', action='store_true', default=True)
226-
parser.add_argument('--prepend-file', action='store', type=str, metavar='YAML_FILE')
226+
parser.add_argument('--prepend-file', action='store', type=Path, metavar='YAML_FILE')
227227
return parser.parse_args()
228228

229-
def save_yaml_to_png(file_out,yaml_input):
230-
with Image.open(fp=f'{file_out}.png') as im:
229+
def save_yaml_to_png(file_out:Path, yaml_input):
230+
file_out = file_out.with_suffix('.png')
231+
with Image.open(fp=file_out) as im:
231232
txt = PngInfo()
232-
txt.add_itxt('yaml',yaml_input,zip=True)
233-
im.save(fp=f'{file_out}.png',pnginfo=txt)
233+
txt.add_itxt('yaml', yaml_input,zip=True)
234+
im.save(fp=file_out, pnginfo=txt)
234235

235-
def read_yaml_from_png(file_in):
236-
with Image.open(fp=f'{file_in}.png') as im:
236+
def read_yaml_from_png(file_in:Path):
237+
with Image.open(fp=file_in.with_suffix('.png')) as im:
237238
im.load()
238239
return im.text['yaml']
239240

240241
def main():
241242

242243
args = parse_cmdline()
243-
244-
if not os.path.exists(args.input_file):
244+
input_file_base = args.input_file.parent / args.input_file.stem
245+
if not args.input_file.is_file():
245246
print(f'Error: input file {args.input_file} inaccessible or does not exist, check path')
246247
sys.exit(1)
247248

248-
if ".png" in args.input_file:
249-
yaml_input = read_yaml_from_png(args.input_file.replace('.png',''))
250-
with open(args.input_file.replace('.png','_out.yaml'),'w') as fh:
249+
if ".png" == args.input_file.suffix:
250+
yaml_input = read_yaml_from_png(input_file_base)
251+
with open_file_write(input_file_base.parent / (input_file_base.stem + '_out.yaml')) as fh:
251252
fh.write(yaml_input) # Extract yaml to separate file
252253
else:
253254
with open_file_read(args.input_file) as fh:
254255
yaml_input = fh.read()
255256

256257
if args.prepend_file:
257-
if not os.path.exists(args.prepend_file):
258+
if not args.prepend_file.is_file():
258259
print(f'Error: prepend input file {args.prepend_file} inaccessible or does not exist, check path')
259260
sys.exit(1)
260261
with open_file_read(args.prepend_file) as fh:
261262
prepend = fh.read()
262263
yaml_input = prepend + yaml_input
263264

264-
if not args.output_file:
265-
file_out = args.input_file
266-
pre, _ = os.path.splitext(file_out)
267-
file_out = pre # extension will be added by graphviz output function
268-
else:
269-
file_out = args.output_file
270-
file_out = os.path.abspath(file_out)
271-
272-
parse(yaml_input, file_out=file_out)
265+
file_out = args.output_file if args.output_file else input_file_base
266+
parse(yaml_input, file_out=file_out.resolve())
273267

274268

275269
if __name__ == '__main__':

0 commit comments

Comments
 (0)