]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/introspect.py
4139ec66c892465c8833a6840479751e2391abad
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 __version__
= "$Revision$"[11:-2]
11 def getAutoCompleteList(command
='', locals=None, includeMagic
=1, \
12 includeSingle
=1, includeDouble
=1):
13 """Return list of auto-completion options for command.
15 The list of options will be based on the locals namespace."""
17 # Get the proper chunk of code from the command.
18 root
= getRoot(command
, terminator
='.')
20 object = eval(root
, locals)
21 attributes
= getAttributeNames(object, includeMagic
, \
22 includeSingle
, includeDouble
)
27 def getAttributeNames(object, includeMagic
=1, includeSingle
=1, includeDouble
=1):
28 """Return list of unique attributes, including inherited, for an object."""
32 try: attributes
+= object._getAttributeNames
()
34 # Get all attribute names, removing duplicates from the attribute list.
35 for item
in getAllAttributeNames(object):
37 attributes
+= dict.keys()
38 attributes
.sort(lambda x
, y
: cmp(x
.lower(), y
.lower()))
40 attributes
= filter(lambda item
: item
[0]!='_' or item
[1]=='_', attributes
)
42 attributes
= filter(lambda item
: item
[:2]!='__', attributes
)
45 def getAllAttributeNames(object):
46 """Return list of all attributes, including inherited, for an object.
48 Recursively walk through a class and all base classes.
51 # Wake up sleepy objects - a hack for ZODB objects in "ghost" state.
52 wakeupcall
= dir(object)
54 # Get attributes available through the normal convention.
55 attributes
+= dir(object)
56 # For a class instance, get the attributes for the class.
57 if hasattr(object, '__class__'):
58 # Break a circular reference. This happens with extension classes.
59 if object.__class
__ is object:
62 attributes
+= getAllAttributeNames(object.__class
__)
63 # Also get attributes from any and all parent classes.
64 if hasattr(object, '__bases__'):
65 for base
in object.__bases
__:
66 attributes
+= getAllAttributeNames(base
)
69 def getCallTip(command
='', locals=None):
70 """Return call tip text for a command.
72 The call tip information will be based on the locals namespace."""
74 # Get the proper chunk of code from the command.
75 root
= getRoot(command
, terminator
='(')
77 object = eval(root
, locals)
81 if hasattr(object, '__name__'): # Make sure this is a useable object.
82 # Switch to the object that has the information we need.
83 if inspect
.ismethod(object) or hasattr(object, 'im_func'):
84 # Get the function from the object otherwise inspect.getargspec()
85 # complains that the object isn't a Python function.
86 object = object.im_func
88 elif inspect
.isclass(object):
89 # Get the __init__ method function for the class.
91 object = object.__init
__.im_func
93 except AttributeError:
94 for base
in object.__bases
__:
95 constructor
= _find_constructor(base
)
96 if constructor
is not None:
100 name
= object.__name
__
102 if inspect
.isbuiltin(object):
103 # Builtin functions don't have an argspec that we can get.
105 elif inspect
.isfunction(object):
106 # tip1 is a string like: "getCallTip(command='', locals=None)"
107 argspec
= apply(inspect
.formatargspec
, inspect
.getargspec(object))
109 # The first parameter to a method is a reference to the
110 # instance, usually coded as "self", and is passed
111 # automatically by Python and therefore we want to drop it.
112 temp
= argspec
.split(',')
113 if len(temp
) == 1: # No other arguments.
115 else: # Drop the first argument.
116 argspec
= '(' + ','.join(temp
[1:]).lstrip()
117 tip1
= name
+ argspec
118 doc
= inspect
.getdoc(object)
120 # tip2 is the first separated line of the docstring, like:
121 # "Return call tip text for a command."
122 # tip3 is the rest of the docstring, like:
123 # "The call tip information will be based on ... <snip>
124 docpieces
= doc
.split('\n\n')
126 tip3
= '\n\n'.join(docpieces
[1:])
127 tip
= '%s\n\n%s\n\n%s' % (tip1
, tip2
, tip3
)
134 def getRoot(command
, terminator
=None):
135 """Return the rightmost root portion of an arbitrary Python command.
137 The command would normally terminate with a "(" or ".". Anything after
138 the terminator will be dropped, allowing you to get back to the root.
139 Return only the root portion that can be eval()'d without side effects.
142 validChars
= "._" + string
.uppercase
+ string
.lowercase
+ string
.digits
144 # Drop the final terminator and anything that follows.
145 pieces
= command
.split(terminator
)
147 command
= terminator
.join(pieces
[:-1])
148 if len(command
) == 0:
150 elif command
in ("''", '""', '""""""', '[]', '()', '{}'):
151 # Let empty type delimiter pairs go through.
154 # Go backward through the command until we hit an "invalid" character.
156 while i
and command
[i
-1] in validChars
:
158 # Detect situations where we are in the middle of a string.
159 # This code catches the simplest case, but needs to catch others.
160 if command
[i
-1] in ("'", '"'):
161 # We're in the middle of a string so we aren't dealing with an
162 # object and it would be misleading to return anything here.
165 # Grab everything from the "invalid" character to the end.