Commit ac7e00f9 authored by Lucas Bourneuf's avatar Lucas Bourneuf

dot program to use is now exposed as high-level option

parent f0e53cb7
......@@ -26,21 +26,21 @@ EXT_TO_TYPE = utils.reverse_dict({
LOADABLE = {'Python', 'ASP', 'json/ASP'}
def single_image_from_filenames(fnames:[str], outfile:str=None, dotfile:str=None, nb_model:int=0, return_image:bool=True, verbosity:int=0) -> Image or None:
def single_image_from_filenames(fnames:[str], outfile:str=None, dotfile:str=None, nb_model:int=0, return_image:bool=True, dot_prog:str='dot', verbosity:int=0) -> Image or None:
pipeline = build_pipeline(fnames, verbosity)
final_context = run(pipeline, verbosity=verbosity)
return compile_to_single_image(final_context, nb_model=nb_model, outfile=outfile, dotfile=dotfile, return_image=return_image, verbosity=verbosity)
return compile_to_single_image(final_context, nb_model=nb_model, outfile=outfile, dotfile=dotfile, return_image=return_image, dot_prog=dot_prog, verbosity=verbosity)
def multiple_images_from_filenames(fnames:[str], outfile:str='out-{}.png', dotfile:str=None, nb_model:int=0, return_image:bool=True, verbosity:int=0) -> Image or None:
def multiple_images_from_filenames(fnames:[str], outfile:str='out-{}.png', dotfile:str=None, nb_model:int=0, return_image:bool=True, dot_prog:str='dot', verbosity:int=0) -> Image or None:
pipeline = build_pipeline(fnames, verbosity)
final_context = run(pipeline, verbosity=verbosity)
return tuple(compile_to_images(final_context, nb_model=nb_model, outfile_template=outfile, dotfile_template=dotfile, return_image=return_image, verbosity=verbosity))
return tuple(compile_to_images(final_context, nb_model=nb_model, outfile_template=outfile, dotfile_template=dotfile, return_image=return_image, dot_prog=dot_prog, verbosity=verbosity))
def gif_from_filenames(fnames:[str], giffile:str=None, dotfile_template:str=None, duration:int=1000, loop:int=0, nb_model:int=0, verbosity:int=0) -> bytes or str:
def gif_from_filenames(fnames:[str], giffile:str=None, dotfile_template:str=None, duration:int=1000, loop:int=0, nb_model:int=0, dot_prog:str='dot', verbosity:int=0) -> bytes or str:
"""Make a gif, with each ASP model as an image. Save it in outfile and dotfile_template"""
pipeline = build_pipeline(fnames, verbosity)
final_context = run(pipeline, verbosity=verbosity)
first, *lasts = compile_to_images(final_context, dotfile_template=dotfile_template, return_image=True, nb_model=nb_model, verbosity=verbosity)
first, *lasts = compile_to_images(final_context, dotfile_template=dotfile_template, return_image=True, nb_model=nb_model, dot_prog=dot_prog, verbosity=verbosity)
output = io.BytesIO() if giffile is None else giffile
first.save(output, format='gif', append_images=lasts, duration=duration, loop=loop, save_all=True)
return output.getvalue() if giffile is None else giffile
......@@ -117,7 +117,7 @@ def solve_context(context:str, *, nb_model:int=0) -> clyngor.Answers:
def compile_to_single_image(context:str, outfile:str=None, dotfile:str=None,
nb_model:int=0, return_image:bool=True,
verbosity:int=0) -> Image or None:
dot_prog:str='dot', verbosity:int=0) -> Image or None:
"Return a pillow.Image object, or write it to outfile if given"
configs = asp_to_dot.visual_config_from_asp(
solve_context(context, nb_model=nb_model)
......@@ -128,7 +128,7 @@ def compile_to_single_image(context:str, outfile:str=None, dotfile:str=None,
with tempfile.NamedTemporaryFile(delete=False) as fd:
outfile = fd.name
del_outfile = True
dot = dot_writer.dot_to_png(dot, outfile, dotfile=dotfile)
dot = dot_writer.dot_to_png(dot, outfile, dotfile=dotfile, prog=dot_prog)
if return_image:
img = Image.open(outfile)
if del_outfile:
......@@ -138,7 +138,7 @@ def compile_to_single_image(context:str, outfile:str=None, dotfile:str=None,
def compile_to_images(context:str, outfile_template:str=None, dotfile_template:str=None,
nb_model:int=0, return_image:bool=True,
verbosity:int=0) -> [Image]:
dot_prog:str='dot', verbosity:int=0) -> [Image]:
"""Yield pillow.Image objects, and write it to outfile if given
outfile_template -- template name for png files, or None
......@@ -162,7 +162,7 @@ def compile_to_images(context:str, outfile_template:str=None, dotfile_template:s
else:
outfile = outfile_template.format(idx)
dotfile = dotfile_template.format(idx) if dotfile_template else None
dot = dot_writer.dot_to_png(dot, outfile, dotfile=dotfile)
dot = dot_writer.dot_to_png(dot, outfile, dotfile=dotfile, prog=dot_prog)
if return_image:
img = Image.open(outfile)
if del_outfile:
......
......@@ -97,7 +97,7 @@ def dot_to_png(dot_lines:iter, outfile:str, dotfile:str=DEFAULT_DOT_FILE,
graphs = iter(pydot.graph_from_dot_data(dot_lines))
for graph in graphs:
with open(outfile, 'wb') as fd:
fd.write(graph.create(prog=prog, format='png'))
fd.write(graph.create(prog=prog or DEFAULT_PROG_FILE, format='png'))
break
# TODO: handle multiple graphs (see #10)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment