#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from ConfigParser import NoOptionError, NoSectionError

import corpus2

from saper.saperio.readers.cclreader import CclReader
from saper.saperio.readers.plainreader import PlainReader
from saper.utils import yielders
from saper.config import config_loader


__author__ = 'blaz'

class TestClass:

    #READERS TESTS:

    def test_plain_text_reader(self):
        test_file_path = os.path.join(os.getcwd(), 'data', 'test_plain')
        fi = open(test_file_path, 'r')
        test_text = fi.read()
        fi.close()
        del fi
        config = config_loader.ConfigLoader(os.path.join('..', '..',
                                                         'cfg', 'example'))
        plain_reader = PlainReader(config)
        document = plain_reader.read_file(test_file_path)
        del plain_reader
        out = ""
        for par in document.paragraphs():
            for sent in par.sentences():
                for token in sent.tokens():
                    if token.after_space():
                        out += " %s" % token.orth_utf8()
                    else:
                        out += "%s" % token.orth_utf8()
        test_text = test_text.strip()
        out = out.strip()
        assert test_text == out, ("Test text:\n\n\t%s\n\n" % test_text) + \
                                 ("is different then result text\n\n\t%s" % out)


    def test_ccl_text_reader(self):
        test_file_path = os.path.join(os.getcwd(), 'data', 'test_ccl.xml')
        tagset = corpus2.get_named_tagset('nkjp')
        rdr = \
            corpus2.TokenReader_create_path_reader('ccl', tagset,
                                                   test_file_path)
        corpus_load_text = ""
        for token in yielders.tokens(rdr):
            if token.after_space():
                corpus_load_text += " %s" % token.orth_utf8()
            else:
                corpus_load_text += "%s" % token.orth_utf8()
        config = config_loader.ConfigLoader(os.path.join('..', '..',
                                                         'cfg', 'example'))
        ccl_reader = CclReader(config)
        document = ccl_reader.read_file(test_file_path)
        del ccl_reader
        reader_load_text = ""
        for par in document.paragraphs():
            for sent in par.sentences():
                for token in sent.tokens():
                    if token.after_space():
                        reader_load_text += " %s" % token.orth_utf8()
                    else:
                        reader_load_text += "%s" % token.orth_utf8()
        assert reader_load_text == corpus_load_text, \
            ("Reader loaded text:\n\n\t%s\n\n") % reader_load_text + \
            ("is different then corpus loaded:\n\n\t%s") % corpus_load_text

    # CONFIG PARSER TEST:

    def test_config_parser(self):
        config = config_loader.ConfigLoader(os.path.join('data', 'test_config'))
        ha = config.get('ass', 'hot_ass')
        assert 'yeah' == ha.strip('\n'), "Something is wrong %s " % ha
        try:
            config.get('ass', 'assassin') is None
        except:
            assert False, "Something is wrong"
        is_okay = False
        try:
            config.get('a','x')
        except NoSectionError:
            is_okay = True
        assert is_okay, "It's not okay"
        is_okay = False
        try:
            config.get('ass','x')
        except NoOptionError:
            is_okay = True
        assert is_okay, "It's not okay"
