| 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 |
"""Adds bidding to the Subject/Observer mechanism so that |
|---|
| 23 |
observers can "voluntarily" be out-bid to provide for |
|---|
| 24 |
state-dependant event handling. |
|---|
| 25 |
|
|---|
| 26 |
In order to bid, the observer must have a 'Bid' attribute |
|---|
| 27 |
(value or callable), and the highest bidder wins, as defined |
|---|
| 28 |
by the > operator.""" |
|---|
| 29 |
|
|---|
| 30 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 31 |
#~ Imports |
|---|
| 32 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 33 |
|
|---|
| 34 |
from Subject import Subject |
|---|
| 35 |
|
|---|
| 36 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 37 |
#~ Definitions |
|---|
| 38 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 39 |
|
|---|
| 40 |
class BidableSubjectMixin(object): |
|---|
| 41 |
def _GetBid(self, observer, UpdateDict): |
|---|
| 42 |
bid = getattr(observer, 'Bid', None) |
|---|
| 43 |
if bid is None: |
|---|
| 44 |
# This client does not payattention to bids... |
|---|
| 45 |
observer(self, **UpdateDict) |
|---|
| 46 |
else: |
|---|
| 47 |
if callable(bid): |
|---|
| 48 |
bid = bid(self, **UpdateDict) |
|---|
| 49 |
if bid: |
|---|
| 50 |
if bid > self._bids[0]: |
|---|
| 51 |
self._bids = bid, [observer] |
|---|
| 52 |
elif bid == self._bids[0]: |
|---|
| 53 |
self._bids[-1].append(observer) |
|---|
| 54 |
|
|---|
| 55 |
def UpdateObserversEx(self, UpdateDict): |
|---|
| 56 |
self._bids = 0, [] |
|---|
| 57 |
result = self.__super.UpdateObserversEx(UpdateDict) |
|---|
| 58 |
if result and self._bids: |
|---|
| 59 |
for observer in self._bids[-1]: |
|---|
| 60 |
observer(self, **UpdateDict) |
|---|
| 61 |
result *= self._bids[-1] and 1 or 0 |
|---|
| 62 |
self._bids = None |
|---|
| 63 |
return result |
|---|
| 64 |
|
|---|
| 65 |
def UpdateObserver(self, observer, UpdateDict): |
|---|
| 66 |
if observer: self._GetBid(observer, UpdateDict) |
|---|
| 67 |
|
|---|
| 68 |
BidableSubjectMixin._BidableSubjectMixin__super = super(BidableSubjectMixin) |
|---|
| 69 |
|
|---|
| 70 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 71 |
|
|---|
| 72 |
class BidableSubject(BidableSubjectMixin, Subject): |
|---|
| 73 |
pass |
|---|
| 74 |
|
|---|