fixed a typo...
[pli.git] / pli / interface / constructor.py
blob55d6e3084599607027887d9637790eec86fdbfce
1 #=======================================================================
3 __version__ = '''0.0.04'''
4 __sub_version__ = '''20040927223921'''
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 #------------------------------------------------------dict2interface---
76 def dict2interface(dict, name=None, doc=None):
77 '''
78 '''
79 ns = {'__format__': dict}
80 if doc != None:
81 ns['__doc__'] = doc
82 return interface._Interface(name == None and 'unnamed' or name, (object,), ns)
84 ###-------------------------------------------------------obj2interface---
85 ##interface = dict2interface
88 #-------------------------------------------------------obj2interface---
89 ##!!! revise !!!##
90 def obj2interface(obj, name=None, doc=None, methods_writable=False):
91 '''
92 this will generate an interface from an example object.
93 '''
94 if name == None:
95 try:
96 name = 'I' + hasattr(obj, '__name__') and obj.__name__ or obj.__class__.__name__
97 except:
98 name = 'IUnnamed'
99 format = {}
100 names = dir(obj)
101 for n in names:
102 format[n] = likevalue(getattr(obj, n), method_writable=methods_writable)
103 class I(interface.Interface):
104 if doc != None:
105 __doc__ = doc
106 __format__ = format
107 I.__name__ = name
108 return I
112 #=======================================================================
113 # vim:set ts=4 sw=4 nowrap :