| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
"""Observer implementation. |
|---|
| 23 |
|
|---|
| 24 |
Note: You only need an Observer instance if you need to place a bid value. |
|---|
| 25 |
otherwise the callable object can be passed straight to the subject. |
|---|
| 26 |
""" |
|---|
| 27 |
|
|---|
| 28 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 29 |
#~ Imports |
|---|
| 30 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 31 |
|
|---|
| 32 |
from RBFoundation.BindCallable import BoundCallable, BindCallable |
|---|
| 33 |
|
|---|
| 34 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 35 |
#~ Definitions |
|---|
| 36 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 37 |
|
|---|
| 38 |
class Observer(BoundCallable): |
|---|
| 39 |
"""Note: Only needed if you are adding a Bid value to the observer; |
|---|
| 40 |
otherwise the callable object can be passed straight to the subject.""" |
|---|
| 41 |
|
|---|
| 42 |
def __init__(self, callback=None, Bid=None): |
|---|
| 43 |
super(Observer, self).__init__(callback or self.Update) |
|---|
| 44 |
self.SetBid(Bid) |
|---|
| 45 |
|
|---|
| 46 |
def SetBid(self, Bid=None): |
|---|
| 47 |
if callable(Bid): |
|---|
| 48 |
self.Bid = BindCallable(Bid) |
|---|
| 49 |
elif Bid: |
|---|
| 50 |
self.Bid = Bid |
|---|
| 51 |
elif hasattr(self, 'Bid'): |
|---|
| 52 |
del self.Bid |
|---|
| 53 |
|
|---|
| 54 |
def Update(self, subject, **UpdateDict): |
|---|
| 55 |
print "On Update:", subject.__class__.__name__, UpdateDict |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 59 |
#~ Testing |
|---|
| 60 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 61 |
|
|---|
| 62 |
if __name__=='__main__': |
|---|
| 63 |
print "Testing..." |
|---|
| 64 |
import BidableSubject |
|---|
| 65 |
s = BidableSubject.BidableSubject() |
|---|
| 66 |
|
|---|
| 67 |
def AssertSubject(subject, value): |
|---|
| 68 |
assert subject is s |
|---|
| 69 |
def AssertFalse(subject, value): |
|---|
| 70 |
assert 0 |
|---|
| 71 |
|
|---|
| 72 |
s.AddObserver(Observer(AssertSubject, 10)) |
|---|
| 73 |
s.AddObserver(Observer(AssertFalse, 9)) |
|---|
| 74 |
s.AddObserver(Observer(AssertFalse, 8)) |
|---|
| 75 |
s.AddObserver(Observer(AssertFalse, -1)) |
|---|
| 76 |
# The following two will get called because of no bid value |
|---|
| 77 |
s.AddObserver(Observer(AssertSubject)) |
|---|
| 78 |
s.AddObserver(AssertSubject) |
|---|
| 79 |
|
|---|
| 80 |
s.UpdateObservers(value=42) |
|---|
| 81 |
print "Test complete." |
|---|
| 82 |
|
|---|