]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/introspect.py
482464735d491fc47189feab8f883f69b0373d50
1 """Provides a variety of introspective-type support functions for
2 things like call tips and command auto completion."""
4 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
6 __revision__
= "$Revision$"[11:-2]
8 from __future__
import nested_scopes
21 def getAutoCompleteList(command
='', locals=None, includeMagic
=1,
22 includeSingle
=1, includeDouble
=1):
23 """Return list of auto-completion options for command.
25 The list of options will be based on the locals namespace."""
27 # Get the proper chunk of code from the command.
28 root
= getRoot(command
, terminator
='.')
30 if locals is not None:
31 object = eval(root
, locals)
37 attributes
= getAttributeNames(object, includeMagic
,
38 includeSingle
, includeDouble
)
41 def getAttributeNames(object, includeMagic
=1, includeSingle
=1,
43 """Return list of unique attributes, including inherited, for object."""
46 if not hasattrAlwaysReturnsTrue(object):
47 # Add some attributes that don't always get picked up. If
48 # they don't apply, they'll get filtered out at the end.
49 attributes
+= ['__bases__', '__class__', '__dict__', '__name__',
50 'func_closure', 'func_code', 'func_defaults',
51 'func_dict', 'func_doc', 'func_globals', 'func_name']
53 try: attributes
+= object._getAttributeNames
()
55 # Get all attribute names.
56 attrdict
= getAllAttributeNames(object)
57 for attrlist
in attrdict
.values():
58 attributes
+= attrlist
59 # Remove duplicates from the attribute list.
60 for item
in attributes
:
62 attributes
= dict.keys()
63 attributes
.sort(lambda x
, y
: cmp(x
.upper(), y
.upper()))
65 attributes
= filter(lambda item
: item
[0]!='_' \
66 or item
[1]=='_', attributes
)
68 attributes
= filter(lambda item
: item
[:2]!='__', attributes
)
69 # Make sure we haven't picked up any bogus attributes somehow.
70 attributes
= [attribute
for attribute
in attributes \
71 if hasattr(object, attribute
)]
74 def hasattrAlwaysReturnsTrue(object):
75 return hasattr(object, 'bogu5_123_aTTri8ute')
77 def getAllAttributeNames(object):
78 """Return dict of all attributes, including inherited, for an object.
80 Recursively walk through a class and all base classes.
82 attrdict
= {} # (object, technique, count): [list of attributes]
84 # Do Not use hasattr() as a test anywhere in this function,
85 # because it is unreliable with remote objects: xmlrpc, soap, etc.
86 # They always return true for hasattr().
89 # Yes, this can fail if object is an instance of a class with
90 # __str__ (or __repr__) having a bug or raising an
95 # Wake up sleepy objects - a hack for ZODB objects in "ghost" state.
96 wakeupcall
= dir(object)
98 # Get attributes available through the normal convention.
99 attributes
= dir(object)
100 attrdict
[(key
, 'dir', len(attributes
))] = attributes
101 # Get attributes from the object's dictionary, if it has one.
103 attributes
= object.__dict
__.keys()
105 except: # Must catch all because object might have __getattr__.
108 attrdict
[(key
, '__dict__', len(attributes
))] = attributes
109 # For a class instance, get the attributes for the class.
111 klass
= object.__class
__
112 except: # Must catch all because object might have __getattr__.
116 # Break a circular reference. This happens with extension
120 attrdict
.update(getAllAttributeNames(klass
))
121 # Also get attributes from any and all parent classes.
123 bases
= object.__bases
__
124 except: # Must catch all because object might have __getattr__.
127 if isinstance(bases
, types
.TupleType
):
129 if type(base
) is types
.TypeType
:
130 # Break a circular reference. Happens in Python 2.2.
133 attrdict
.update(getAllAttributeNames(base
))
136 def getCallTip(command
='', locals=None):
137 """For a command, return a tuple of object name, argspec, tip text.
139 The call tip information will be based on the locals namespace."""
140 calltip
= ('', '', '') # object name, argspec, tip text.
141 # Get the proper chunk of code from the command.
142 root
= getRoot(command
, terminator
='(')
144 if locals is not None:
145 object = eval(root
, locals)
151 object, dropSelf
= getBaseObject(object)
153 name
= object.__name
__
154 except AttributeError:
158 if inspect
.isbuiltin(object):
159 # Builtin functions don't have an argspec that we can get.
161 elif inspect
.isfunction(object):
162 # tip1 is a string like: "getCallTip(command='', locals=None)"
163 argspec
= apply(inspect
.formatargspec
, inspect
.getargspec(object))
165 # The first parameter to a method is a reference to an
166 # instance, usually coded as "self", and is usually passed
167 # automatically by Python; therefore we want to drop it.
168 temp
= argspec
.split(',')
169 if len(temp
) == 1: # No other arguments.
171 else: # Drop the first argument.
172 argspec
= '(' + ','.join(temp
[1:]).lstrip()
173 tip1
= name
+ argspec
176 doc
= inspect
.getdoc(object)
178 # tip2 is the first separated line of the docstring, like:
179 # "Return call tip text for a command."
180 # tip3 is the rest of the docstring, like:
181 # "The call tip information will be based on ... <snip>
182 firstline
= doc
.split('\n')[0].lstrip()
183 if tip1
== firstline
:
187 docpieces
= doc
.split('\n\n')
189 tip3
= '\n\n'.join(docpieces
[1:])
190 tip
= '%s%s\n\n%s' % (tip1
, tip2
, tip3
)
193 calltip
= (name
, argspec
[1:-1], tip
.strip())
196 def getRoot(command
, terminator
=None):
197 """Return the rightmost root portion of an arbitrary Python command.
199 Return only the root portion that can be eval()'d without side
200 effects. The command would normally terminate with a '(' or
201 '.'. The terminator and anything after the terminator will be
203 command
= rtrimTerminus(command
, terminator
)
204 tokens
= getTokens(command
)
207 if tokens
[-1][0] is tokenize
.ENDMARKER
:
208 # Remove the end marker.
210 if terminator
== '.' and \
211 (tokens
[-1][1] <> '.' or tokens
[-1][0] is not tokenize
.OP
):
212 # Trap decimals in numbers, versus the dot operator.
215 # Strip off the terminator.
216 command
= command
[:-1]
217 command
= command
.rstrip()
218 tokens
= getTokens(command
)
224 emptyTypes
= ('[]', '()', '{}')
227 tokenstring
= token
[1]
229 if tokentype
is tokenize
.ENDMARKER
:
231 if tokentype
in (tokenize
.NAME
, tokenize
.STRING
, tokenize
.NUMBER
) \
232 and laststring
!= '.':
233 # We've reached something that's not part of the root.
234 if prefix
and line
[token
[3][1]] != ' ':
235 # If it doesn't have a space after it, remove the prefix.
238 if tokentype
in (tokenize
.NAME
, tokenize
.STRING
, tokenize
.NUMBER
) \
239 or (tokentype
is tokenize
.OP
and tokenstring
== '.'):
241 # The prefix isn't valid because it comes after a dot.
245 # start represents the last known good point in the line.
247 elif len(tokenstring
) == 1 and tokenstring
in ('[({])}'):
248 # Remember, we're working backwords.
249 # So prefix += tokenstring would be wrong.
250 if prefix
in emptyTypes
and tokenstring
in ('[({'):
251 # We've already got an empty type identified so now we
252 # are in a nested situation and we can break out with
256 prefix
= tokenstring
+ prefix
258 # We've reached something that's not part of the root.
260 laststring
= tokenstring
264 if prefix
in emptyTypes
:
265 # Empty types are safe to be eval()'d and introspected.
269 def getTokens(command
):
270 """Return list of token tuples for command."""
271 command
= str(command
) # In case the command is unicode, which fails.
272 f
= cStringIO
.StringIO(command
)
273 # tokens is a list of token tuples, each looking like:
274 # (type, string, (srow, scol), (erow, ecol), line)
276 # Can't use list comprehension:
277 # tokens = [token for token in tokenize.generate_tokens(f.readline)]
278 # because of need to append as much as possible before TokenError.
280 ## This code wasn't backward compatible with Python 2.1.3.
282 ## for token in tokenize.generate_tokens(f.readline):
283 ## tokens.append(token)
285 # This works with Python 2.1.3 (with nested_scopes).
288 tokenize
.tokenize_loop(f
.readline
, eater
)
289 except tokenize
.TokenError
:
290 # This is due to a premature EOF, which we expect since we are
291 # feeding in fragments of Python code.
295 def rtrimTerminus(command
, terminator
=None):
296 """Return command minus anything that fillows the final terminator."""
298 pieces
= command
.split(terminator
)
300 command
= terminator
.join(pieces
[:-1]) + terminator
303 def getBaseObject(object):
304 """Return base object and dropSelf indicator for an object."""
305 if inspect
.isbuiltin(object):
306 # Builtin functions don't have an argspec that we can get.
308 elif inspect
.ismethod(object):
309 # Get the function from the object otherwise
310 # inspect.getargspec() complains that the object isn't a
313 if object.im_self
is None:
314 # This is an unbound method so we do not drop self
315 # from the argspec, since an instance must be passed
320 object = object.im_func
321 except AttributeError:
323 elif inspect
.isclass(object):
324 # Get the __init__ method function for the class.
325 constructor
= getConstructor(object)
326 if constructor
is not None:
331 elif callable(object):
332 # Get the __call__ method instead.
334 object = object.__call
__.im_func
336 except AttributeError:
340 return object, dropSelf
342 def getConstructor(object):
343 """Return constructor for class object, or None if there isn't one."""
345 return object.__init
__.im_func
346 except AttributeError:
347 for base
in object.__bases
__:
348 constructor
= getConstructor(base
)
349 if constructor
is not None: