#!/usr/bin/env python

import argparse
import sys

from csvkit import convert
from csvkit.cli import init_common_parser, extract_csv_reader_kwargs, install_exception_handler

def main(args):
    """
    Command line-utility that convert tabular data formats into CSV.
    """
    install_exception_handler(args.verbose)
   
    kwargs = extract_csv_reader_kwargs(args)

    if args.file.name == '<stdin>' and not args.format:
        sys.exit('You must specify a format when inputting data via STDIN (pipe).')

    if args.format:
        format = args.format

        if format not in convert.SUPPORTED_FORMATS:
            sys.exit('"%s" is not a supported format' % args.format)

    elif args.schema:
        format = 'fixed'
    else:
        format = convert.guess_format(args.file.name)

        if not format:
            sys.exit('Unable to automatically determine the format of the input file. Try specifying a format with --format.')

    if args.schema:
        kwargs['schema'] = args.schema

    # Fixed width can be processed as a stream
    if format == 'fixed':
        kwargs['output'] = sys.stdout

    sys.stdout.write(convert.convert(args.file, format, **kwargs))

if __name__ == "__main__":
    parser = init_common_parser(description='Convert common, but less awesome, tabular data formats to CSV.', epilog='Many command line flags only pertain to specific input formats.')
    parser.add_argument('-f', '--format', dest='format',
                        help='The format of the input file. If not specified will be inferred from the file type. Supported formats: %s.' % ', '.join(sorted(convert.SUPPORTED_FORMATS)))
    parser.add_argument('-s', '--schema', dest='schema', type=argparse.FileType('r'),
                        help='Specifies a CSV-formatted schema file for converting fixed-width files.  See documentation for details.')

    main(parser.parse_args())
