#!/usr/bin/env python
"""
..
    Copyright: LeafLabs, LLC, 2014
    License: MIT License
    Author: bnewbold@leaflabs.com
    Date: Feb 2014

Pretty printer with filtering for dealing with large Xilinx ISE build output
logs.

Call with --help for usage info.
"""

from __future__ import print_function
import argparse
#import logging as log

from xmsgs import *


# ========== Script/Args =========== #
def main():
    parser = argparse.ArgumentParser(
        description="Shows the diff! Of the things!",
        usage="%(prog)s [options]")
    parser.add_argument('-v', '--verbose', action="count",
        help="more verbose (debugging) output")
    parser.add_argument('-f', '--full', action="store_true",
        help="display full output")
    parser.add_argument('-b', '--by-file', action="store_true",
        help="Display outputs by filename")
    parser.add_argument('-s', '--skip-paths', nargs="+",
        help="Skip paths (can be globs)")
    parser.add_argument('--show-path', action="store_true",
        help="show file paths (when possible); implies --full")
    parser.add_argument('file', type=str,
        help="")
    parser.add_argument('-i', '--ignore', nargs="+", type=int,
        help="list of warnings to ignore (integers)")
    parser.add_argument('-t', '--types', nargs="+", type=str,
        help="list of types to handle (error, warning, info, severe)")

    args = parser.parse_args()

    #if args.verbose > 0:
    #    log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
    #else:
    #    log.basicConfig(format="%(levelname)s: %(message)s")

    xmsgs_configure(
        disable_color=(not os.isatty(sys.stdout.fileno())),
        ignore_list=args.ignore,
        types=args.types,
        skip_paths=args.skip_paths)

    if os.path.isdir(args.file):
        the_file = glob.glob(args.file + "/*xmsgs")
    else:
        the_file = [args.file, ]

    (msgs, counts) = parse(the_file)
    if args.by_file:
        print_by_file(msgs, full=args.full)
    else:
        print_msgs(msgs, None, full=args.full)
    print_counts(counts)

if __name__ == '__main__':
    main()

