From 1df45ab833e1ff91b7838a4221fdb65009ea5004 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sat, 2 Oct 2021 12:27:27 +0200 Subject: [PATCH] Implement working proof of concept --- src/wireviz/wv_cli.py | 60 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/src/wireviz/wv_cli.py b/src/wireviz/wv_cli.py index f38ad97..cbc2e82 100644 --- a/src/wireviz/wv_cli.py +++ b/src/wireviz/wv_cli.py @@ -1,17 +1,26 @@ import os +from pathlib import Path import sys import click -import wireviz.wireviz +from wireviz import APP_NAME, __version__ +import wireviz.wireviz as wv +from wireviz.wv_helper import open_file_read + @click.command() @click.argument('filepath', nargs=-1) +@click.option('-f', '--format', default='hpst') @click.option('-p', '--prepend', default=None) -@click.option('-o', '--output', default='hpst') +@click.option('-o', '--output-file', default=None) @click.option('-V', '--version', is_flag=True, default=False) -def main(filepath, prepend, output, version): - print('WireViz!') +def main(filepath, format, prepend, output_file, version): + print() + print(f'{APP_NAME} {__version__}') + if version: + return # print version number only and exit + # get list of files try: _ = iter(filepath) @@ -20,8 +29,47 @@ def main(filepath, prepend, output, version): else: filepaths = list(filepath) - for f in filepaths: - print(f) + # determine output formats + format_codes = {'p': 'png', 's': 'svg', 't': 'tsv', 'c': 'csv', 'h': 'html', 'P': 'pdf'} + return_types = [] + for code in format: + if code in format_codes: + return_types.append(format_codes[code]) + else: + raise Exception(f'Unknown output format: {code}') + return_types = tuple(sorted(set(return_types))) + return_types_str = f'[{"|".join(return_types)}]' if len(return_types) > 1 else return_types[0] + + # check prepend file + if prepend: + prepend = Path(prepend) + if not prepend.exists(): + raise Exception(f'File does not exist:\n{prepend}') + print('Prepend file:', prepend) + + with open_file_read(prepend) as file_handle: + prepend_input = file_handle.read() + '\n' + else: + prepend_input = '' + + # run WireVIz on each input file + for file in filepaths: + file = Path(file) + if not file.exists(): + raise Exception(f'File does not exist:\n{file}') + + file_out = file.with_suffix('') if not output_file else output_file + + print('Input file: ', file) + print('Output file: ', f'{file_out}.{return_types_str}') + + with open_file_read(file) as file_handle: + yaml_input = file_handle.read() + + yaml_input = prepend_input + yaml_input + + wv.parse(yaml_input, file_out=file_out) + print() if __name__ == '__main__':