Add command line argument parsing (input/output file)

This commit is contained in:
Daniel Rojas 2020-05-30 00:46:59 +02:00
parent f04441f903
commit 50e3441eaa
3 changed files with 153 additions and 126 deletions

2
src/.gitignore vendored
View File

@ -1,2 +1,2 @@
__pycache__/
_output/
_test/

7
src/batch.py Normal file
View File

@ -0,0 +1,7 @@
import yaml2wireviz
# TODO: make examples (progressively more complex), batch process all of them
yaml2wireviz.parse('../examples/example1.yml')
yaml2wireviz.parse('../examples/example2.yml')
yaml2wireviz.parse('../examples/ferrules.yml')
yaml2wireviz.parse('../examples/bundles.yml')

48
src/yaml2wireviz.py Normal file → Executable file
View File

@ -1,18 +1,8 @@
#!/usr/bin/env python3
import os
import yaml
import wireviz
filename = '../examples/example1.yml'
filename = '../examples/example2.yml'
filename = '../examples/ferrules.yml'
filename = '../examples/bundles.yml'
def check_designators(what, where):
for i, x in enumerate(what):
# print('Looking for {} in {}'.format(x,where[i]))
if x not in input[where[i]]:
return False
return True
def expand(input):
# input can be:
# - a singleton (normally str or int)
@ -41,7 +31,16 @@ def expand(input):
output.append(x)
return output
with open(filename, 'r') as stream:
def _parse(file_in, file_out):
def check_designators(what, where):
for i, x in enumerate(what):
# print('Looking for {} in {}'.format(x,where[i]))
if x not in input[where[i]]:
return False
return True
with open(file_in, 'r') as stream:
try:
input = yaml.safe_load(stream)
except yaml.YAMLError as exc:
@ -177,4 +176,25 @@ for con in input['connections']:
else:
raise Exception('Wrong number of connection parameters')
h.output(filename='output', format=('png','svg'), view=False)
h.output(filename=file_out, format=('png','svg'), view=False)
def parse(filename_in, filename_out=None):
fin = os.path.abspath(filename_in)
if filename_out:
fout = filename_out
else:
fout = fin
pre, ext = os.path.splitext(fout)
fout = pre # extension will be added by graphviz output function
fout = os.path.abspath(fout)
_parse(fin, fout)
if __name__ == '__main__':
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('file_input', nargs='?', default='_test/test.yml')
ap.add_argument('file_output', nargs='?', default=None)
args = ap.parse_args()
parse(args.file_input, args.file_output)