#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#==============================================================================
# Copyright (C) 2011 by Loïc Grobol
#
# This file is part of CTESLO.
# 
#    CTESLO is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
# 
#    CTESLO is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
# 
#    You should have received a copy of the GNU General Public License
#    along with CTESLO.  If not, see <http://www.gnu.org/licenses/>.
#==============================================================================

"""
Created on 6 juil. 2011

@author: Loïc Grobol <loic.grobol@gmail.com>
"""


import sys, functools
import argparse

# Ugly hack to allow absolute import from the root folder
# whatever its name is. Please forgive the heresy.
if __name__ == "__main__" and __package__ is None:
    from sys import path
    from os.path import dirname as dir
    path.append(dir(dir(path[0])))
    __package__ = "formats.tagged_csv"

from libcteslo.dflib.tokenization import _tokens_cat, DEFAULT_DELIMITERS

def clean_stream(in_stream, out_stream):
    cat = functools.partial(_tokens_cat,
                            default_delimiter=" ",
                            delimiters=DEFAULT_DELIMITERS)
    buffer = ""
    for line in in_stream:
        if line.strip():
            lexeme = line.split("\t")[0]
            for word in [word for word in DEFAULT_DELIMITERS.split(lexeme) if word]:
                buffer = cat(buffer, word)
        else:
            out_stream.write("{0}\n".format(buffer))
            buffer = ""
    out_stream.write("{0}\n".format(buffer))

def main(argv=sys.argv[1:]):
    """Main function."""
    parser = argparse.ArgumentParser(description="Textify tagged csv.")
    parser.add_argument("--in_stream", "-i",
                         type=argparse.FileType('r'),
                         default=sys.stdin,
                         help="File to be textified. Standard input by default.")
    parser.add_argument("--out_stream", "-o",
                         type=argparse.FileType('w'),
                         default=sys.stdout,
                         help="Clean file to be written. Standard output by default.")

    args = parser.parse_args(argv)

    if args.in_stream.isatty():
        parser.print_help()
    else:
        clean_stream(args.in_stream, args.out_stream)

if __name__ == "__main__":
    result = main()
    sys.exit(result)
