]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/introspect.py
237ed198e501ac48ec2c904b1bb5d6d50032688d
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.
90 constructor
= getConstructor(object)
91 if constructor
is not None:
94 name
= object.__name
__
96 if inspect
.isbuiltin(object):
97 # Builtin functions don't have an argspec that we can get.
99 elif inspect
.isfunction(object):
100 # tip1 is a string like: "getCallTip(command='', locals=None)"
101 argspec
= apply(inspect
.formatargspec
, inspect
.getargspec(object))
103 # The first parameter to a method is a reference to the
104 # instance, usually coded as "self", and is passed
105 # automatically by Python and therefore we want to drop it.
106 temp
= argspec
.split(',')
107 if len(temp
) == 1: # No other arguments.
109 else: # Drop the first argument.
110 argspec
= '(' + ','.join(temp
[1:]).lstrip()
111 tip1
= name
+ argspec
112 doc
= inspect
.getdoc(object)
114 # tip2 is the first separated line of the docstring, like:
115 # "Return call tip text for a command."
116 # tip3 is the rest of the docstring, like:
117 # "The call tip information will be based on ... <snip>
118 docpieces
= doc
.split('\n\n')
120 tip3
= '\n\n'.join(docpieces
[1:])
121 tip
= '%s\n\n%s\n\n%s' % (tip1
, tip2
, tip3
)
128 def getConstructor(object):
129 """Return constructor for class object, or None if there isn't one."""
131 return object.__init
__.im_func
132 except AttributeError:
133 for base
in object.__bases
__:
134 constructor
= getConstructor(base
)
135 if constructor
is not None:
139 def getRoot(command
, terminator
=None):
140 """Return the rightmost root portion of an arbitrary Python command.
142 The command would normally terminate with a "(" or ".". Anything after
143 the terminator will be dropped, allowing you to get back to the root.
144 Return only the root portion that can be eval()'d without side effects.
147 validChars
= "._" + string
.uppercase
+ string
.lowercase
+ string
.digits
149 # Drop the final terminator and anything that follows.
150 pieces
= command
.split(terminator
)
152 command
= terminator
.join(pieces
[:-1])
153 if len(command
) == 0:
155 elif command
in ("''", '""', '""""""', '[]', '()', '{}'):
156 # Let empty type delimiter pairs go through.
159 # Go backward through the command until we hit an "invalid" character.
161 while i
and command
[i
-1] in validChars
:
163 # Detect situations where we are in the middle of a string.
164 # This code catches the simplest case, but needs to catch others.
165 if command
[i
-1] in ("'", '"'):
166 # We're in the middle of a string so we aren't dealing with an
167 # object and it would be misleading to return anything here.
170 # Grab everything from the "invalid" character to the end.