# Copyright (c) 2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program 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 2 of the License, or (at your option) any later
# version.
#
# This program 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
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307
# USA.

"""
EngineModel
"""

__revision__ = "$Id: EngineModel.py,v 1.7 2001/12/06 10:45:17 syt Exp $"

from narval.utils.dp import Observable

class EngineModel(Observable):
    """
    Model of Engine matching
    Use Singleton pattern, herits from Observable
    """

    _DICT = {}

    def __init__(self):
        self.__dict__ = self._DICT

        if not self.__dict__ :
            # init as observable
            Observable.__init__(self)
            # identifier lists
            self._event_queue = []
            self._scheduler_queue = []
            # identifiers/text lists
            self._event_queue_messages = {}
            self._scheduler_queue_messages = {}
            # messages list
            self._messages_list = []

    # event queue methods ######################

    def event_queue_append(self,eq):
        if not eq in self._event_queue:
            self._event_queue.append(eq)

    def get_event_queue(self):
        return self._event_queue

    # event queue messages methods #############

    def event_queue_messages_append(self,key,eqm):
        self._event_queue_messages[key] = eqm
        self.notify_observers()
            
    def get_event_queue_messages(self):
        return self._event_queue_messages


    # scheduler queue methods ##################

    def scheduler_queue_append(self,sq):
        if not sq in self._scheduler_queue:
            self._scheduler_queue.append(sq)

    def get_scheduler_queue(self):
        return self._scheduler_queue

    # scheduler queue messages methods #########

    def scheduler_queue_messages_append(self,id,sqm):
        self._scheduler_queue_messages[id] = sqm
        self.notify_observers()

    def get_scheduler_queue_messages(self):
        return self._scheduler_queue_messages

    # messages list methods ####################

    def messages_list_append(self,lm):
        if not lm in self._messages_list:
            self._messages_list.append(lm)
            self.notify_observers()

    def get_messages_list(self):
        return self._messages_list

    # clean methods ############################

    def clean_event(self):
        for id in self._event_queue[:]:
            self._event_queue.remove(id)
            del self._event_queue_messages[id]
        self.notify_observers()

    def clean_scheduler(self):
        for id in self._scheduler_queue[:]:
            self._scheduler_queue.remove(id)
            del self._scheduler_queue_messages[id]
        self.notify_observers()

    def clean_messages(self):
        self._messages_list = []
        self.notify_observers()
