Commit 7b9a6c85 authored by Lucas Bourneuf's avatar Lucas Bourneuf

nb_model argument + fix multiple image generation

parent 9dc74f26
from .module_loader import Script
from .core import single_image_from_filenames, gif_from_filenames
from .core import single_image_from_filenames, multiple_images_from_filenames, gif_from_filenames
__version__ = '0.0.5.dev0'
......@@ -9,6 +9,7 @@ import os
import argparse
import itertools
from . import core
from . import __version__
def parse_cli(args:iter=None) -> dict:
......@@ -18,13 +19,15 @@ def parse_cli(args:iter=None) -> dict:
parser.add_argument('infiles', type=str, nargs='*', metavar='MODULE',
default=[], help='files containing ASP or Python code')
parser.add_argument('--outfile', '-o', type=str, default='out.png',
help="output file. Will be overwritten with png data. Can be templated with '{model_number}'")
help="output file. Will be overwritten with png data. Can be templated with '{}'")
parser.add_argument('--dotfile', '-d', type=str, default=None,
help="output file. Will be overwritten with dot data. Can be templated with '{model_number}'")
help="output file. Will be overwritten with dot data. Can be templated with '{}'")
parser.add_argument('--config', '-c', type=str, default=None,
help="configuration file, specifying scripts and their options")
parser.add_argument('--gif-duration', '-gd', type=int, default=500,
help="gif duration in second")
help="gif duration in millisecond")
parser.add_argument('--nb-model', '-n', type=int, default=0,
help="number of model of the final context to handle. 0 for all.")
parser.add_argument('-v', '--verbosity', action='count', default=0)
# flags
......@@ -52,12 +55,18 @@ if __name__ == '__main__':
dotfile_template=args.dotfile,
duration=args.gif_duration,
verbosity=args.verbosity,
nb_model=args.nb_model,
)
else:
core.single_image_from_filenames(
if args.outfile and '{}' in args.outfile:
builder = core.multiple_images_from_filenames
else:
builder = core.single_image_from_filenames
builder(
all_infiles,
dotfile=args.dotfile,
outfile=args.outfile,
return_image=False,
verbosity=args.verbosity,
nb_model=args.nb_model,
)
......@@ -26,17 +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, 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, verbosity:int=0) -> Image or None:
pipeline = build_pipeline(fnames, verbosity)
final_context = run(pipeline, verbosity=verbosity)
return compile_to_single_image(final_context, 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, 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:
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))
def gif_from_filenames(fnames:[str], giffile:str=None, dotfile_template:str=None, duration:int=1000, loop: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, 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, verbosity=verbosity)
first, *lasts = compile_to_images(final_context, dotfile_template=dotfile_template, return_image=True, nb_model=nb_model, 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
......@@ -106,15 +110,16 @@ def run(scripts:[Script], initial_context:str='', verbosity:int=0) -> str:
return context
def solve_context(context:str) -> clyngor.Answers:
return clyngor.solve(inline=context).by_predicate.careful_parsing.int_not_parsed
def solve_context(context:str, *, nb_model:int=0) -> clyngor.Answers:
return clyngor.solve(inline=context, nb_model=nb_model, options='--project').by_predicate.careful_parsing.int_not_parsed
def compile_to_single_image(context:str, outfile:str=None, dotfile:str=None,
return_image:bool=True, verbosity:int=0) -> Image or None:
nb_model:int=0, return_image:bool=True,
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)
solve_context(context, nb_model=nb_model)
)
dot = dot_writer.one_graph_from_configs(configs)
del_outfile = False
......@@ -131,7 +136,8 @@ 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,
return_image:bool=True, verbosity:int=0) -> [Image]:
nb_model:int=0, return_image:bool=True,
verbosity:int=0) -> [Image]:
"""Yield pillow.Image objects, and write it to outfile if given
outfile_template -- template name for png files, or None
......@@ -139,7 +145,7 @@ def compile_to_images(context:str, outfile_template:str=None, dotfile_template:s
"""
configs = asp_to_dot.visual_config_from_asp(
solve_context(context)
solve_context(context, nb_model=nb_model)
)
if outfile_template and '{}' not in outfile_template:
raise ValueError("Outfile argument is not a valid template")
......
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