*** empty log message ***
[pli.git] / pli / interface / constructor.py
blobca10c8bee1883c58f6638cd9fdca1645c51a8446
1 #=======================================================================
3 __version__ = '''0.0.04'''
4 __sub_version__ = '''20040831161913'''
5 __copyright__ = '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 import inspect
11 import types
13 import pli.functional as func
14 import pli.interface.interface as interface
18 #-----------------------------------------------------------------------
19 #-------------------------------------------------callablescompatible---
20 # TODO find a better name and place....
21 def callablescompatible(c0, c1, method_aware=False):
22 '''
23 this will compare two callables by signature...
24 '''
25 argspec = [None, None]
26 i = 0
27 for c in (c0, c1):
28 if type(c) is types.MethodType and method_aware == True:
29 argspec[i] = inspect.getargspec(c)
30 # check if bound...
31 if c.im_self != None:
32 argspec[i] = list(argspec[i])
33 # bound --> instance
34 # remove the first argument...
35 argspec[i][0] = argspec[i][0][1:]
36 if len(argspec[i][3]) > len(argspec[i][0]):
37 # this is a rare but just in case.... (for methods
38 # of the form: meth(self=<...>, ...) )
39 argspec[i][3] = argspec[i][3][1:]
40 argspec[i] = tuple(argspec[i])
41 elif type(c) in (types.MethodType, types.FunctionType):
42 argspec[i] = inspect.getargspec(c)
43 else:
44 argspec[i] = inspect.getargspec(c.__call__)
45 i += 1
46 return argspec[0] == argspec[1]
50 #-----------------------------------------------------------------------
51 #-----------------------------------------------------------likevalue---
52 ##!!! revise !!!##
53 def likevalue(obj, method_writable=False):
54 '''
55 this will create an interface property dict that describes the argument.
57 NOTE: this is usefull as it will create a predicate that will check
58 function/method signature.
59 '''
60 res = {}
61 # type or predicate...
62 if callable(obj):
63 # function signature...
64 res['predicate'] = func.curry(callablescompatible, obj, method_aware=True)
65 if not method_writable:
66 res['writable'] = False
67 else:
68 res['type'] = type(obj)
69 # doc...
70 if hasattr(obj, '__doc__'):
71 res['doc'] = obj.__doc__
72 return res
75 #-----------------------------------------------------createinterface---
76 ##!!! revise !!!##
77 def createinterface(obj, name=None, doc=None, methods_writable=False):
78 '''
79 this will generate an interface from an example object.
80 '''
81 if name == None:
82 try:
83 name = 'I' + hasattr(obj, '__name__') and obj.__name__ or obj.__class__.__name__
84 except:
85 name = 'IUnnamed'
86 format = {}
87 names = dir(obj)
88 for n in names:
89 format[n] = likevalue(getattr(obj, n), method_writable=methods_writable)
90 class I(interface.Interface):
91 if doc != None:
92 __doc__ = doc
93 __format__ = format
94 I.__name__ = name
95 return I
99 #=======================================================================
100 # vim:set ts=4 sw=4 nowrap :