| 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 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 23 |
#~ Imports |
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
|
|---|
| 26 |
from RBFoundation import ContextApply |
|---|
| 27 |
|
|---|
| 28 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 29 |
#~ Definitions |
|---|
| 30 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 31 |
|
|---|
| 32 |
class Pointcut(object): |
|---|
| 33 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 34 |
#~ Constants / Variables / Etc. |
|---|
| 35 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 36 |
|
|---|
| 37 |
class Pass(Exception): pass |
|---|
| 38 |
|
|---|
| 39 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 40 |
#~ Special |
|---|
| 41 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 42 |
|
|---|
| 43 |
def __init__(self, name, *args, **kw): |
|---|
| 44 |
self.__name__ = name |
|---|
| 45 |
self.__doc__ = kw.get('doc', '') |
|---|
| 46 |
|
|---|
| 47 |
if args: |
|---|
| 48 |
self.PointcutAdvice = PointcutAdviceCollection() |
|---|
| 49 |
map(self.PointcutAdvice.AddAdvice, args) |
|---|
| 50 |
|
|---|
| 51 |
def __get__(self, obj, klass): |
|---|
| 52 |
if obj: |
|---|
| 53 |
method = ContextApply.ContextApply_s_p(obj._AspectedOperation, getattr) |
|---|
| 54 |
return self.PointcutAdvice('get', method, obj, self.__name__) |
|---|
| 55 |
else: return self |
|---|
| 56 |
|
|---|
| 57 |
def __set__(self, obj, value): |
|---|
| 58 |
method = ContextApply.ContextApply_s_p(obj._AspectedOperation, setattr) |
|---|
| 59 |
return self.PointcutAdvice('set', method, obj, self.__name__, value) |
|---|
| 60 |
|
|---|
| 61 |
def __delete__(self, obj): |
|---|
| 62 |
method = ContextApply.ContextApply_s_p(obj._AspectedOperation, delattr) |
|---|
| 63 |
return self.PointcutAdvice('delete', method, obj, self.__name__) |
|---|
| 64 |
|
|---|
| 65 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 66 |
#~ Public Methods |
|---|
| 67 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 68 |
|
|---|
| 69 |
def PointcutAdvice(self, AdviceType, method, *args, **kw): |
|---|
| 70 |
return method(*args, **kw) |
|---|
| 71 |
|
|---|
| 72 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 73 |
|
|---|
| 74 |
class PointcutAdviceCollection(object): |
|---|
| 75 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 76 |
#~ Special |
|---|
| 77 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 78 |
|
|---|
| 79 |
def __init__(self): |
|---|
| 80 |
self.Advise = {} |
|---|
| 81 |
|
|---|
| 82 |
def __call__(self, AdviceType, method, obj, name, *args, **kw): |
|---|
| 83 |
try: |
|---|
| 84 |
# Advise the 'Before', AdviceType pointcuts |
|---|
| 85 |
for Advise in self.Advise.get(('before', AdviceType), []): |
|---|
| 86 |
AdviseResult = Advise.AdviseBefore(obj, name, *args, **kw) |
|---|
| 87 |
if AdviseResult is not None: args = (AdviseResult,) + args[1:] |
|---|
| 88 |
|
|---|
| 89 |
# Apply the method |
|---|
| 90 |
result = method(obj, name, *args, **kw) |
|---|
| 91 |
|
|---|
| 92 |
# Advise the 'After', AdviceType pointcuts |
|---|
| 93 |
for Advise in self.Advise.get(('after', AdviceType), []): |
|---|
| 94 |
result = Advise.AdviseAfter(obj, name, result, *args, **kw) |
|---|
| 95 |
|
|---|
| 96 |
return result |
|---|
| 97 |
except Pointcut.Pass, PassResult: |
|---|
| 98 |
return PassResult |
|---|
| 99 |
|
|---|
| 100 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 101 |
#~ Public Methods |
|---|
| 102 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 103 |
|
|---|
| 104 |
def AddAdvice(self, Advice): |
|---|
| 105 |
AdviseType = Advice.AdviseType |
|---|
| 106 |
for AdviseTime in Advice.AdviseTimes: |
|---|
| 107 |
self.AddAdviceEx(Advice, AdviseTime, AdviseType) |
|---|
| 108 |
|
|---|
| 109 |
def AddAdviceEx(self, Advice, AdviseTime=None, AdviseType=None): |
|---|
| 110 |
self.Advise.setdefault((AdviseTime, AdviseType), []).append(Advice) |
|---|
| 111 |
|
|---|