| 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 that can delegate multiple input updates to multiple |
|---|
| 23 |
receivers based on what the delegate observer is interested in. Note: This is less |
|---|
| 24 |
effecient than Subject-side category based filtering, but does not depend upon the |
|---|
| 25 |
subject implementing the category interface. |
|---|
| 26 |
""" |
|---|
| 27 |
|
|---|
| 28 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 29 |
#~ Imports |
|---|
| 30 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 31 |
|
|---|
| 32 |
from RBFoundation.BindCallable import BindCallable |
|---|
| 33 |
import ProxyBidableCategorySubjectMixin |
|---|
| 34 |
|
|---|
| 35 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 36 |
#~ Definitions |
|---|
| 37 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 38 |
|
|---|
| 39 |
class AssociativeObserver(ProxyBidableCategorySubjectMixin): |
|---|
| 40 |
def __init__(self): |
|---|
| 41 |
self._associations = {} |
|---|
| 42 |
|
|---|
| 43 |
def AddAssociation(self, category, observer): |
|---|
| 44 |
self._associations.setdefault(category, []).append(BindCallable(observer)) |
|---|
| 45 |
return self |
|---|
| 46 |
|
|---|
| 47 |
def _ObserverList(self, category): |
|---|
| 48 |
return self._associations.get(category, []) |
|---|
| 49 |
|
|---|
| 50 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 51 |
#~ Testing |
|---|
| 52 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 53 |
|
|---|
| 54 |
if __name__=='__main__': |
|---|
| 55 |
print "Testing..." |
|---|
| 56 |
import BidableSubject |
|---|
| 57 |
s = BidableSubject.BidableSubject() |
|---|
| 58 |
from Observer import Observer |
|---|
| 59 |
|
|---|
| 60 |
def AssertSubject(subject, value): |
|---|
| 61 |
assert subject is s |
|---|
| 62 |
def AssertFalse(subject, value): |
|---|
| 63 |
assert 0 |
|---|
| 64 |
|
|---|
| 65 |
ao = AssociativeObserver() |
|---|
| 66 |
s.AddObserver(ao) |
|---|
| 67 |
|
|---|
| 68 |
ao.AddAssociation('test', Observer(AssertFalse, 100)) |
|---|
| 69 |
ao.AddAssociation('test', Observer(AssertFalse, -1)) |
|---|
| 70 |
|
|---|
| 71 |
ao.AddAssociation('value', Observer(AssertFalse, 9)) |
|---|
| 72 |
ao.AddAssociation('value', Observer(AssertSubject, 10)) |
|---|
| 73 |
ao.AddAssociation('value', Observer(AssertSubject)) |
|---|
| 74 |
ao.AddAssociation('value', AssertSubject) |
|---|
| 75 |
|
|---|
| 76 |
ao.AddAssociation('value2', Observer(AssertSubject)) |
|---|
| 77 |
ao.AddAssociation('value2', AssertSubject) |
|---|
| 78 |
|
|---|
| 79 |
s.UpdateObservers(value=42) |
|---|
| 80 |
|
|---|
| 81 |
print "Test complete." |
|---|
| 82 |
|
|---|