#!/usr/bin/env python2.7
"""Script to clean up a text from Project Gutenberg."""


from __future__ import print_function
import argparse
import codecs
import sys

from gutenberg_cleaner import strip_headers
from gutenberg_cleaner.io import determine_encoding


def _encoded_input(encoding, mode='r'):
    """ArgumentType for encoded inputs.

    Args:
        encoding (str): The encoding for the input.
        mode (str, optional): The mode to open the input (default: read-only)

    Returns:
        function: A function that opens an input using the specified encoding.

    """
    def _opener(path):
        """Closure.

        """
        try:
            if isinstance(path, file):
                return codecs.getreader(encoding)(path)
            else:
                return codecs.open(path, mode,
                                   determine_encoding(path, encoding))
        except Exception as ex:
            raise argparse.ArgumentError(ex)

    return _opener


def _cli(encoding):
    """Command line interface.

    """
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('text', type=_encoded_input(encoding),
                        nargs='?', default=_encoded_input(encoding)(sys.stdin),
                        help='The file to clean up (default: stdin)')
    args = parser.parse_args()

    try:
        print(strip_headers(args.text).encode(encoding))
    finally:
        args.text.close()


if __name__ == '__main__':
    _cli('utf-8')
