__main__.py 2.7 KB
Newer Older
1 2 3 4
"""Entry point for package.

To play with scripts parameters, you must use the configuration
file instead of the direct enumeration of filenames.
5 6 7 8 9

"""

import os
import argparse
10
import itertools
11
from . import core
12
from . import __version__
13 14 15 16 17 18


def parse_cli(args:iter=None) -> dict:
    # main parser
    parser = argparse.ArgumentParser(description=__doc__)

19
    parser.add_argument('infiles', type=str, nargs='*', metavar='MODULE',
20 21
                        default=[], help='files containing ASP or Python code')
    parser.add_argument('--outfile', '-o', type=str, default='out.png',
22
                        help="output file. Will be overwritten with png data. Can be templated with '{}'")
23
    parser.add_argument('--dotfile', '-d', type=str, default=None,
24
                        help="output file. Will be overwritten with dot data. Can be templated with '{}'")
25 26
    parser.add_argument('--config', '-c', type=str, default=None,
                        help="configuration file, specifying scripts and their options")
27
    parser.add_argument('--gif-duration', '-gd', type=int, default=500,
28 29 30
                        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.")
31
    parser.add_argument('-v', '--verbosity', action='count', default=0)
Lucas Bourneuf's avatar
Lucas Bourneuf committed
32
    parser.add_argument('--version', action='version', version=f'biseau {__version__}')
33 34

    # flags
Lucas Bourneuf's avatar
bugfix  
Lucas Bourneuf committed
35
    parser.add_argument('--gif', '-g', action='store_true', default=None,
36
                        help="Do not merge graphs ; build a gif with frame for each model")
37 38 39 40 41 42

    return parser.parse_args(args)


if __name__ == '__main__':
    args = parse_cli()
43 44 45 46 47 48
    all_infiles = args.infiles
    if args.config:
        all_infiles = itertools.chain(
            all_infiles,
            core.build_pipeline.from_configfile(args.config, verbosity=args.verbosity)
        )
49 50 51 52 53 54 55 56 57 58
    if os.path.splitext(args.outfile)[1] == '.gif' and args.gif is None:
        args.gif = True  # turn on the gif behavior

    if args.gif:
        core.gif_from_filenames(
            all_infiles,
            giffile=args.outfile,
            dotfile_template=args.dotfile,
            duration=args.gif_duration,
            verbosity=args.verbosity,
59
            nb_model=args.nb_model,
60 61
        )
    else:
62 63 64 65 66
        if args.outfile and '{}' in args.outfile:
            builder = core.multiple_images_from_filenames
        else:
            builder = core.single_image_from_filenames
        builder(
67 68 69 70 71
            all_infiles,
            dotfile=args.dotfile,
            outfile=args.outfile,
            return_image=False,
            verbosity=args.verbosity,
72
            nb_model=args.nb_model,
73
        )