root/trunk/RBJabber/RBJabber/SubjectObserver/__init__.py

Revision 253, 4.9 kB (checked in by sholloway, 6 years ago)

Changed to BSD License

Line 
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 """Subject/Observer with bells on.
23
24 The goal of this package is to make both observers and subjects very simple to create
25 maintain, while retaining both flexable and powerful subjects / observers.  The simplest
26 observers are just callable objects, and the simplest subjects keep a list of observers
27 to notify.  More advanced classes include bidable, attributed, and categorized
28 subject/observers.
29
30 This main module collects and blends many of the subject and observer types to create
31 extremely useful subjects and observers. 
32 """
33
34 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35 #~ Imports
36 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37
38 import Observer as _Observer
39 import Subject as _Subject
40 import CategorySubject as _CategorySubject
41 import AttributedSubject as _AttributedSubject
42 import BidableSubject as _BidableSubject
43
44 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45 #~ Definitions
46 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47
48 class AttributedCategorySubject(_AttributedSubject.AttributedSubjectMixin, _CategorySubject.CategorySubjectMixin, _Subject.Subject):
49     pass
50 class AttributedBidableSubject(_AttributedSubject.AttributedSubjectMixin, _BidableSubject.BidableSubjectMixin, _Subject.Subject):
51     pass
52 class AttributedBidableCategorySubject(_AttributedSubject.AttributedSubjectMixin, _BidableSubject.BidableSubjectMixin, _CategorySubject.CategorySubjectMixin, _Subject.Subject):
53     pass
54
55 class BidableCategorySubject(_BidableSubject.BidableSubjectMixin, _CategorySubject.CategorySubjectMixin, _Subject.Subject):
56     pass
57 class BidableAttributedSubject(_AttributedSubject.AttributedSubjectMixin, _BidableSubject.BidableSubjectMixin, _Subject.Subject):
58     pass
59 class BidableAttributedCategorySubject(_AttributedSubject.AttributedSubjectMixin, _BidableSubject.BidableSubjectMixin, _CategorySubject.CategorySubjectMixin, _Subject.Subject):
60     pass
61
62 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63
64 class ProxyBidableCategorySubjectMixin(object):
65     """Allows some subject to proxy for a different subject as an observer of that subject,
66     while maintaining bid-sequences and categories"""
67
68     def __call__(self, subject, **UpdateDict):
69         pass
70
71     def _ProxyObserverList(self, category):
72         return self._ObserverList(category[0])
73
74     def Bid(self, subject, **UpdateDict):
75         for value in UpdateDict.iteritems():
76             for obs in self._ProxyObserverList(value):
77                 subject._GetBid(obs, UpdateDict)
78  
79 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80
81 class ProxyBidableSubjectMixin(object):
82     """Allows some subject to proxy for a different subject as an observer of that subject,
83     while maintaining bid-sequences"""
84
85     def __call__(self, subject, **UpdateDict):
86         pass
87        
88     def _ProxyObserverList(self):
89         return self._ObserverList()
90
91     def Bid(self, subject, **UpdateDict):
92         for obs in self._ProxyObserverList():
93             subject._GetBid(obs, UpdateDict)
94  
95 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96 #~ Testing
97 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
99 def _Test_SubjectObservers():
100     def printo1(subject, **UpdateDict):
101         print "O1: ", UpdateDict
102     def printo2(subject, **UpdateDict):
103         print "O2: ", UpdateDict
104     def printo3(subject, **UpdateDict):
105         print "O3: ", UpdateDict
106     s1 = _Subject.Subject()
107     s2 = _AttributedSubject.AttributedSubject()
108     s3 = AttributedCategorySubject()
109
110     s1.AddObserver(printo1)
111     s1.AddObserver(printo2, priority=-1)
112     s2.AddObserver(printo2)
113
114     s1.UpdateObservers(Hello='World')
115     s2.UpdateObservers(Goodbye='Complexity')
116
117     s2.NewValue = 'A New Value'
118
119     print "Locked"
120     lock = s1.Lock()
121     s1.UpdateObservers(Locked='Update')
122     print "Post-lock"
123     del lock
124     print "Unlocked"
125
126     o3 = _Observer.Observer(printo3)
127     s3.AddObserver('CategoryA', printo1)
128     s3.AddObserver('CategoryB', printo2)
129     s3.AddObserver('CategoryC', o3)
130
131     s3.Nobody = 1
132     s3.CategoryA = 'Hello'
133     s3.CategoryC = 'Goodbye'
134
135 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136
137 if __name__=='__main__':
138     print "Testing..."
139     _Test_SubjectObservers()
140     print "Test complete."
141
Note: See TracBrowser for help on using the browser.