''' 
依靠给各个组件的 dispatch 调用接口发指令来驱动所有工作
这是一个示例性质的原型,具体环境下需要调整
'''
from cppy.cp_util import *

class DataStorageManager():
    """ Models the contents of the file """
    def __init__(self):
        self._data = []

    def dispatch(self, message):
        if message[0] == 'init':
            return self._init(message[1])
        elif message[0] == 'words':
            return self._words()
        else:
            raise Exception("Message not understood " + message[0])

    def _init(self, path_to_file):
        self._data = re_split( read_file(path_to_file) )

    def _words(self):
        return self._data
   

class StopWordManager():
    """ Models the stop word filter """
    _stop_words = []

    def dispatch(self, message):
        if message[0] == 'init':
            return self._init()
        elif message[0] == 'is_stop_word':
            return self._is_stop_word(message[1])
        else:
            raise Exception("Message not understood " + message[0])
 
    def _init(self):        
        self._stop_words = get_stopwords()  

    def _is_stop_word(self, word):
        return word in self._stop_words


class WordFrequencyManager():
    """ Keeps the word frequency data """
    _word_freqs = {}

    def dispatch(self, message):
        if message[0] == 'increment_count':
            return self._increment_count(message[1])
        elif message[0] == 'sorted':
            return self._sorted()
        else:
            raise Exception("Message not understood " + message[0])
 
    def _increment_count(self, word):    
        self._word_freqs[word] = self._word_freqs.get(word,0) + 1
        
    def _sorted(self):
        return sort_dict(self._word_freqs)


class WordFrequencyController():

    def dispatch(self, message):
        if message[0] == 'init':
            return self._init(message[1])
        elif message[0] == 'run':
            return self._run()
        else:
            raise Exception("Message not understood " + message[0])
 
    def _init(self, path_to_file):
        self._storage_manager = DataStorageManager()
        self._stop_word_manager = StopWordManager()
        self._word_freq_manager = WordFrequencyManager()
        self._storage_manager.dispatch(['init', path_to_file])
        self._stop_word_manager.dispatch(['init'])

    def _run(self):
        for w in self._storage_manager.dispatch(['words']):
            if not self._stop_word_manager.dispatch(['is_stop_word', w]):
                self._word_freq_manager.dispatch(['increment_count', w])

        word_freqs = self._word_freq_manager.dispatch(['sorted'])
        print_word_freqs(word_freqs)        


if __name__ == '__main__':
    wfcontroller = WordFrequencyController()
    wfcontroller.dispatch(['init', testfilepath])
    wfcontroller.dispatch(['run'])