]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/introspect.py
7c0211a2fa11ce0ecf23da188b650330217a2839
1 """Provides a variety of introspective-type support functions for things
2 like call tips and command auto completion."""
4 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
6 __date__
= "August 8, 2001"
7 __version__
= "$Revision$"[11:-2]
12 def getAutoCompleteList(command
='', locals=None, includeMagic
=1, \
13 includeSingle
=1, includeDouble
=1):
14 """Return list of auto-completion options for command.
16 The list of options will be based on the locals namespace."""
18 # Get the proper chunk of code from the command.
19 root
= getRoot(command
, terminator
='.')
21 object = eval(root
, locals)
22 attributes
= getAttributeNames(object, includeMagic
, \
23 includeSingle
, includeDouble
)
28 def getAttributeNames(object, includeMagic
=1, includeSingle
=1, includeDouble
=1):
29 """Return list of unique attributes, including inherited, for an object."""
33 try: attributes
+= object._getAttributeNames
()
35 # Get all attribute names, removing duplicates from the attribute list.
36 for item
in getAllAttributeNames(object):
38 attributes
+= dict.keys()
41 attributes
= filter(lambda item
: item
[0]!='_' or item
[1]=='_', attributes
)
43 attributes
= filter(lambda item
: item
[:2]!='__', attributes
)
46 def getAllAttributeNames(object):
47 """Return list of all attributes, including inherited, for an object.
49 Recursively walk through a class and all base classes.
52 # Wake up sleepy objects - a hack for ZODB objects in "ghost" state.
53 wakeupcall
= dir(object)
55 # Get attributes available through the normal convention.
56 attributes
+= dir(object)
57 # For a class instance, get the attributes for the class.
58 if hasattr(object, '__class__'):
59 # Break a circular reference. This happens with extension classes.
60 if object.__class
__ is object:
63 attributes
+= getAllAttributeNames(object.__class
__)
64 # Also get attributes from any and all parent classes.
65 if hasattr(object, '__bases__'):
66 for base
in object.__bases
__:
67 attributes
+= getAllAttributeNames(base
)
70 def getCallTip(command
='', locals=None):
71 """Return call tip text for a command.
73 The call tip information will be based on the locals namespace."""
75 # Get the proper chunk of code from the command.
76 root
= getRoot(command
, terminator
='(')
78 object = eval(root
, locals)
82 if hasattr(object, '__name__'): # Make sure this is a useable object.
83 # Switch to the object that has the information we need.
84 if inspect
.ismethod(object) or hasattr(object, 'im_func'):
85 # Get the function from the object otherwise inspect.getargspec()
86 # complains that the object isn't a Python function.
87 object = object.im_func
89 elif inspect
.isclass(object):
90 # Get the __init__ method function for the class.
92 object = object.__init
__.im_func
94 except AttributeError:
95 for base
in object.__bases
__:
96 constructor
= _find_constructor(base
)
97 if constructor
is not None:
101 name
= object.__name
__
103 if inspect
.isbuiltin(object):
104 # Builtin functions don't have an argspec that we can get.
106 elif inspect
.isfunction(object):
107 # tip1 is a string like: "getCallTip(command='', locals=None)"
108 argspec
= apply(inspect
.formatargspec
, inspect
.getargspec(object))
110 # The first parameter to a method is a reference to the
111 # instance, usually coded as "self", and is passed
112 # automatically by Python and therefore we want to drop it.
113 temp
= argspec
.split(',')
114 if len(temp
) == 1: # No other arguments.
116 else: # Drop the first argument.
117 argspec
= '(' + ','.join(temp
[1:]).lstrip()
118 tip1
= name
+ argspec
119 doc
= inspect
.getdoc(object)
121 # tip2 is the first separated line of the docstring, like:
122 # "Return call tip text for a command."
123 # tip3 is the rest of the docstring, like:
124 # "The call tip information will be based on ... <snip>
125 docpieces
= doc
.split('\n\n')
127 tip3
= '\n\n'.join(docpieces
[1:])
128 tip
= '%s\n\n%s\n\n%s' % (tip1
, tip2
, tip3
)
135 def getRoot(command
, terminator
=None):
136 """Return the rightmost root portion of an arbitrary Python command.
138 The command would normally terminate with a "(" or ".". Anything after
139 the terminator will be dropped, allowing you to get back to the root.
140 Return only the root portion that can be eval()'d without side effects.
143 validChars
= "._" + string
.uppercase
+ string
.lowercase
+ string
.digits
145 # Drop the final terminator and anything that follows.
146 pieces
= command
.split(terminator
)
148 command
= terminator
.join(pieces
[:-1])
149 if len(command
) == 0:
151 elif command
in ("''", '""', '""""""', '[]', '()', '{}'):
152 # Let empty type delimiter pairs go through.
155 # Go backward through the command until we hit an "invalid" character.
157 while i
and command
[i
-1] in validChars
:
159 # Detect situations where we are in the middle of a string.
160 # This code catches the simplest case, but needs to catch others.
161 if command
[i
-1] in ("'", '"'):
162 # We're in the middle of a string so we aren't dealing with an
163 # object and it would be misleading to return anything here.
166 # Grab everything from the "invalid" character to the end.