ModelCode
import ModelDeleted
class MissingViewException(Exception): pass
class MissingControllerException(Exception): pass
class Model: """The database."""
def __init__(self): """Construct the instance and initialize the defaults."""
self.__dict = {} self.__dict['views'] = {} self.__dict['controllers'] = {} self.__modelDeleted = ModelDeleted.ModelDeleted()
return
def __del__(self): """Inform all the views and controllers that the model is being deleted. Finally, destroy the instance."""
## print "Model.__del__" self.finished() return
def finished(self): """Manually break loops."""
## print "Model.finished" self.notifyViews(self.__modelDeleted) self.notifyControllers(self.__modelDeleted)
self.__dict = {} self.__dict['views'] = {} self.__dict['controllers'] = {}
return
def __remove(self, aType, which): del self.__dict[aType][which] return
def __addEvent(self, aType, which, anEvent): if not self.__dict[aType].has_key(which): self.__dict[aType][which] = []
self.__dict[aType][which].append([anEvent, anEvent.getReason()]) return
def __removeEvent(self, aType, which, anEvent): self.__dict[aType][which].remove([anEvent, anEvent.getReason()]) return
def __notify(self, aType, aReason): for anItem in self.__dict[aType].keys(): for event, reason in self.__dict[aType][anItem]: if reason == aReason: event.execute(aReason) return
def __eventList(self, aType, anItem): return self.__dict[aType][anItem]
def removeView(self, aView): """Removes all the events for a single view"""
try: self.__remove(
views
, aView) except KeyError: raise MissingViewException, aViewreturn
def getViewEventList(self, aView): """Returns a list of all views."""
result = [] try: result = self.__eventList(
views
, aView) except KeyError: raise MissingViewException, aViewreturn result
def addViewEvent(self, aView, anEvent): """Adds an event for a single view"""
self.__addEvent(
views
, aView, anEvent) returndef removeViewEvent(self, aView, anEvent): """Removes an event for a single view"""
try: self.__removeEvent(
views
, aView, anEvent) except ValueError: raise MissingViewException, aViewreturn
def notifyViews(self, aReason): """Only notify the views that are interested"""
self.__notify(
views
, aReason)return
def getControllerEventList(self, aController): """Returns a list of controllers."""
result = [] try: result = self.__eventList(
controllers
, aController) except KeyError: raise MissingControlelrException, aControllerreturn result
def removeController(self, aController): """Removes all the events for a single controller"""
try: self.__remove(
controllers
, aController) except KeyError: raise MissingControllerException, aControllerreturn
def addControllerEvent(self, aController, anEvent): """Adds an event for a single controller"""
self.__addEvent(
controllers
, aController, anEvent) returndef removeControllerEvent(self, aController, anEvent): """Removes an event for a single controller"""
try: self.__removeEvent(
controllers
, aController, anEvent) except ValueError: raise MissingControllerException, viewreturn
def notifyControllers(self, aReason): """Only notify the controllers that are interested"""
self.__notify(
controllers
, aReason) return