Add command line argument parsing (input/output file)
This commit is contained in:
parent
f04441f903
commit
50e3441eaa
2
src/.gitignore
vendored
2
src/.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
_output/
|
_test/
|
||||||
|
|||||||
7
src/batch.py
Normal file
7
src/batch.py
Normal 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
48
src/yaml2wireviz.py
Normal file → Executable file
@ -1,18 +1,8 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import os
|
||||||
import yaml
|
import yaml
|
||||||
import wireviz
|
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):
|
def expand(input):
|
||||||
# input can be:
|
# input can be:
|
||||||
# - a singleton (normally str or int)
|
# - a singleton (normally str or int)
|
||||||
@ -41,7 +31,16 @@ def expand(input):
|
|||||||
output.append(x)
|
output.append(x)
|
||||||
return output
|
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:
|
try:
|
||||||
input = yaml.safe_load(stream)
|
input = yaml.safe_load(stream)
|
||||||
except yaml.YAMLError as exc:
|
except yaml.YAMLError as exc:
|
||||||
@ -177,4 +176,25 @@ for con in input['connections']:
|
|||||||
else:
|
else:
|
||||||
raise Exception('Wrong number of connection parameters')
|
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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user