*** empty log message ***
[pli.git] / pli / pattern / proxy / utils.py
blob9581c50708197de0d357abe94fde35d0dd07b21f
1 #=======================================================================
3 __version__ = '''0.0.04'''
4 __sub_version__ = '''20040911234403'''
5 __copyright__ = '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 import types
11 import new
12 import sys
15 #-----------------------------------------------------------------------
16 #----------------------------------------createmethodwrappersinobject---
17 ##!!! REVISE (rewrite or remove) !!!##
18 def createmethodwrappersinobject(source_obj, method_list, wrapper, target_obj):
19 '''
20 this will attach methods mentioned in method_list to the target object
21 that will wrap methods of the source object.
23 WARNING: this will render the target object unpicklable...
24 NOTE: this can be used for classes...
25 '''
26 for meth in method_list:
27 if hasattr(source_obj, meth):
28 setattr(target_obj, meth, wrapper(getattr(source_dict, meth)))
29 return target_obj
32 #------------------------------------------------createmethodwrappers---
33 ##!!! REVISE (rewrite or remove) !!!##
34 def createmethodwrappers(source_dict, method_list, wrapper, target_dict=None):
35 '''
36 this will wrap methods mentioned in method_list from the source dict and
37 will return a dict containing the wrappers (will update and return target_dict
38 if given).
39 '''
40 if target_dict != None:
41 res = target_dict
42 else:
43 res = {}
44 for meth in method_list:
45 if meth in source_dict:
46 res[meth] = wrapper(source_dict[meth])
47 return res
51 #-----------------------------------------------------------------------
52 #--------------------------------------------------genericproxymethod---
53 def proxymethod(method_name, source_attr, depth=1):
54 '''
55 this will create a proxy to the method name in the containing namespace.
57 NOTE: this will add the method_name to the containing namespace.
58 NOTE: source_attr is to be used as the attr name referencing the source object.
59 '''
60 # text of the new function....
61 txt = '''\
62 def %(method_name)s(self, *p, **n):
63 """
64 this is the proxy to %(method_name)s method.
65 """
66 return self.%(source_attr)s.%(method_name)s(*p, **n)
67 proxy = %(method_name)s'''
68 # execute the above code...
69 exec (txt % {'method_name': method_name, 'source_attr': source_attr})
70 # update the NS...
71 sys._getframe(depth).f_locals[method_name] = proxy
74 #--------------------------------------------------------proxymethods---
75 def proxymethods(names, source_attr):
76 '''
77 this will generate a direct proxy for each name.
78 '''
79 for name in names:
80 proxymethod(name, source_attr, depth=2)
84 #-----------------------------------------------------------------------
85 #------------------------------------------------------swapmethodself---
86 def swapmethodself(meth, wrapper, use_wrapper_as_class=True):
87 '''
88 '''
89 return new.instancemethod(meth.im_func,
90 wrapper,
91 use_wrapper_as_class and \
92 wrapper or meth.im_class)
95 #------------------------------------------------------wrapmethodself---
96 def wrapmethodself(meth, wrapper, use_wrapper_as_class=True):
97 '''
98 '''
99 return new.instancemethod(meth.im_func,
100 wrapper(meth.im_self),
101 use_wrapper_as_class and \
102 wrapper or meth.im_class)
106 #=======================================================================
107 # vim:set ts=4 sw=4 nowrap :