55import os
66from pathlib import Path
77import sys
8- from typing import Any , Tuple
8+ from typing import Any , Tuple , Optional , Union
99
1010import yaml
1111from PIL import Image
1717from wireviz import __version__
1818from wireviz .DataClasses import Metadata , Options , Tweak
1919from wireviz .Harness import Harness
20- from wireviz .wv_helper import expand , open_file_read
20+ from wireviz .wv_helper import expand , open_file_read , open_file_write
2121
2222
23- def parse (yaml_input : str , file_out : ( str , Path ) = None , return_types : ( None , str , Tuple [str ]) = None ) -> Any :
23+ def parse (yaml_input : str , file_out : Union [ str , Path ] = None , return_types : Optional [ Union [ str , Tuple [str ]]] = None ) -> Any :
2424 """
2525 Parses yaml input string and does the high-level harness conversion
2626
@@ -210,7 +210,7 @@ def check_designators(what, where): # helper function
210210 return tuple (returns ) if len (returns ) != 1 else returns [0 ]
211211
212212
213- def parse_file (yaml_file : str , file_out : ( str , Path ) = None ) -> None :
213+ def parse_file (yaml_file : str , file_out : Union [ str , Path ] = None ) -> None :
214214 with open_file_read (yaml_file ) as file :
215215 yaml_input = file .read ()
216216
@@ -227,56 +227,50 @@ def parse_cmdline():
227227 description = 'Generate cable and wiring harness documentation from YAML descriptions' ,
228228 )
229229 parser .add_argument ('-V' , '--version' , action = 'version' , version = '%(prog)s ' + __version__ )
230- parser .add_argument ('input_file' , action = 'store' , type = str , metavar = 'YAML_FILE' )
231- parser .add_argument ('-o' , '--output_file' , action = 'store' , type = str , metavar = 'OUTPUT' )
230+ parser .add_argument ('input_file' , action = 'store' , type = Path , metavar = 'YAML_FILE' )
231+ parser .add_argument ('-o' , '--output_file' , action = 'store' , type = Path , metavar = 'OUTPUT' )
232232 # Not implemented: parser.add_argument('--generate-bom', action='store_true', default=True)
233- parser .add_argument ('--prepend-file' , action = 'store' , type = str , metavar = 'YAML_FILE' )
233+ parser .add_argument ('--prepend-file' , action = 'store' , type = Path , metavar = 'YAML_FILE' )
234234 return parser .parse_args ()
235235
236- def save_yaml_to_png (file_out ,yaml_input ):
237- with Image .open (fp = f'{ file_out } .png' ) as im :
236+ def save_yaml_to_png (file_out :Path , yaml_input ):
237+ file_out = file_out .with_suffix ('.png' )
238+ with Image .open (fp = file_out ) as im :
238239 txt = PngInfo ()
239- txt .add_itxt ('yaml' ,yaml_input ,zip = True )
240- im .save (fp = f' { file_out } .png' , pnginfo = txt )
240+ txt .add_itxt ('yaml' , yaml_input ,zip = True )
241+ im .save (fp = file_out , pnginfo = txt )
241242
242- def read_yaml_from_png (file_in ):
243- with Image .open (fp = f' { file_in } . png' ) as im :
243+ def read_yaml_from_png (file_in : Path ):
244+ with Image .open (fp = file_in . with_suffix ( '. png') ) as im :
244245 im .load ()
245246 return im .text ['yaml' ]
246247
247248def main ():
248249
249250 args = parse_cmdline ()
250-
251- if not os . path . exists ( args .input_file ):
251+ input_file_base = args . input_file . parent / args . input_file . stem
252+ if not args .input_file . is_file ( ):
252253 print (f'Error: input file { args .input_file } inaccessible or does not exist, check path' )
253254 sys .exit (1 )
254255
255- if ".png" in args .input_file :
256- yaml_input = read_yaml_from_png (args . input_file . replace ( '.png' , '' ) )
257- with open ( args . input_file . replace ( '.png' , ' _out.yaml' ), 'w' ) as fh :
256+ if ".png" == args .input_file . suffix :
257+ yaml_input = read_yaml_from_png (input_file_base )
258+ with open_file_write ( input_file_base . parent / ( input_file_base . stem + ' _out.yaml' )) as fh :
258259 fh .write (yaml_input ) # Extract yaml to separate file
259260 else :
260261 with open_file_read (args .input_file ) as fh :
261262 yaml_input = fh .read ()
262263
263264 if args .prepend_file :
264- if not os . path . exists ( args .prepend_file ):
265+ if not args .prepend_file . is_file ( ):
265266 print (f'Error: prepend input file { args .prepend_file } inaccessible or does not exist, check path' )
266267 sys .exit (1 )
267268 with open_file_read (args .prepend_file ) as fh :
268269 prepend = fh .read ()
269270 yaml_input = prepend + yaml_input
270271
271- if not args .output_file :
272- file_out = args .input_file
273- pre , _ = os .path .splitext (file_out )
274- file_out = pre # extension will be added by graphviz output function
275- else :
276- file_out = args .output_file
277- file_out = os .path .abspath (file_out )
278-
279- parse (yaml_input , file_out = file_out )
272+ file_out = args .output_file if args .output_file else input_file_base
273+ parse (yaml_input , file_out = file_out .resolve ())
280274
281275
282276if __name__ == '__main__' :
0 commit comments