#!/usr/bin/env python
#
# Copyright (C) 2014 Carlos Jenkins <carlos@jenkins.co.cr>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

"""
codeco command line application.
"""

from sys import exit
from os.path import isfile, isdir
from argparse import ArgumentParser, ArgumentError

from pygments.styles import get_all_styles

from codeco.processor import Processor, default_tpl


def input_file(path):
    """
    'Type' for argparse - checks that path is a file.
    """
    if not isfile(path):
        raise ArgumentError('{0} doesn\'t exist.'.format(path))
    return path


def output_file(path):
    """
    'Type' for argparse - checks that file is not a directory.
    """
    if isdir(path):
        raise ArgumentError('{0} is a directory.'.format(path))
    return path


def main():
    # Create parser
    parser = ArgumentParser(
        description='codeco command line application.'
    )

    # Define arguments
    parser.add_argument(
        'code', type=input_file,
        help='path to code file.',
    )
    parser.add_argument(
        'annotations', type=input_file,
        help='path to annotations file.',
    )
    parser.add_argument(
        '-o', '--output', type=output_file,
        help='path to output file.',
        default=None,
    )
    parser.add_argument(
        '-t', '--template', type=input_file,
        help='path to template file.',
        default=None,
    )
    parser.add_argument(
        '-d', '--title',
        help='document title.',
        default='',
    )
    parser.add_argument(
        '-s', '--style',
        help='syntax highlighting style.',
        choices=sorted(get_all_styles()),
        default='monokai',
    )
    parser.add_argument(
        '-c', '--create', type=output_file,
        help='create a template file.',
        default=None,
    )

    # Parse arguments
    args = parser.parse_args()

    # Handle arguments
    #  Create template if requested
    if args.create is not None:
        with open(args.create, 'w') as f:
            f.write(default_tpl)
        exit(0)

    #  Load template if available
    template = None
    if args.template is not None:
        template = args.template.read()
        args.template.close()

    #  Create document
    proc = Processor()
    result = proc.create_document(
        args.code, args.annotations,
        title=args.title, tpl=template,
        out_file=args.output, codestyle=args.style,
    )

    #  Write output
    if args.output is None:
        print(result)


if __name__ == '__main__':
    main()
