Package rats :: Module observer
[hide private]
[frames] | no frames]

Source Code for Module rats.observer

 1  # -*- coding: utf-8 -*- 
 2   
 3  # Miville 
 4  # Copyright (C) 2008 Société des arts technologiques (SAT) 
 5  # http://www.sat.qc.ca 
 6  # All rights reserved. 
 7  # 
 8  # This file is free software: you can redistribute it and/or modify 
 9  # it under the terms of the GNU General Public License as published by 
10  # the Free Software Foundation, either version 2 of the License, or 
11  # (at your option) any later version. 
12  # 
13  # Miville is distributed in the hope that it will be useful, 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  # GNU General Public License for more details. 
17  # 
18  # You should have received a copy of the GNU General Public License 
19  # along with Miville.  If not, see <http://www.gnu.org/licenses/>. 
20   
21  """ 
22  Subject and Observer classes to implement the Model-View-Controller pattern. 
23  """ 
24   
25  import weakref 
26   
27 -class Observer(object):
28 """Instances of Observer are notified of changes happening in the 29 Subject instances they are attached to. 30 It's possible for an Observer to watch many Subject. 31 """ 32
33 - def __init__(self, subjects):
34 self.subjects = [] 35 if isinstance(subjects, tuple): 36 for subject in subjects: 37 self.append(subject) 38 else: 39 self.append(subjects)
40
41 - def append(self, subject):
42 """ 43 Adds a subject to be observed by this Observer instance. 44 """ 45 if isinstance(subject, Subject): 46 self.subjects.append(subject) 47 subject._attach(self) # should we make an excepption/error message
48 # if subject isn't a Subject instance ? 49
50 - def update(self, origin, key, value):
51 """Called when an attribute of the observed object is changed. 52 Should be overridden. 53 54 @param origin: observed object for which the attribute is changed (caller) 55 @type origin: any 56 @param key: attribute changed (default to the function that called it. in api.py) 57 @type key: string 58 @param value: value of the attribute after being set 59 @type value: any 60 """ 61 raise NotImplementedError
62 63
64 -class Subject(object):
65 """ 66 Subject watched by an Observer. 67 68 This can be the "Model" in the Model-View-Controller pattern. 69 """
70 - def __init__(self):
71 self.observers = weakref.WeakValueDictionary()
72
73 - def _attach(self, observer):
74 ob_id = id(observer) 75 if ob_id not in self.observers: 76 self.observers[ob_id] = observer
77
78 - def notify(self, caller, value, key=None):
79 """ 80 Calls all its observers that an attribute has changed. 81 82 Usage: self.notify(self,'brown','color') 83 WARNING: the signature has changed since miville. 84 args key and valud have been interchanged. 85 """ 86 if not key: 87 raise Exception("Warning: support for None keys has been removed") 88 # key = common.get_def_name() 89 for observer in self.observers.itervalues(): 90 observer.update(caller, key, value)
91