160 try: |
160 try: |
161 return self.matches[state] |
161 return self.matches[state] |
162 except IndexError: |
162 except IndexError: |
163 return None |
163 return None |
164 |
164 |
|
165 def _callable_postfix(self, val, word): |
|
166 """ |
|
167 Protected method to check for a callable. |
|
168 |
|
169 @param val value to check (object) |
|
170 @param word word to ammend (string) |
|
171 @return ammended word (string) |
|
172 """ |
|
173 if hasattr(val, '__call__'): |
|
174 word = word + "(" |
|
175 return word |
|
176 |
165 def global_matches(self, text): |
177 def global_matches(self, text): |
166 """ |
178 """ |
167 Public method to compute matches when text is a simple name. |
179 Public method to compute matches when text is a simple name. |
168 |
180 |
169 @param text The text to be completed. (string) |
181 @param text The text to be completed. (string) |
171 defined in self.namespace that match. |
183 defined in self.namespace that match. |
172 """ |
184 """ |
173 import keyword |
185 import keyword |
174 matches = [] |
186 matches = [] |
175 n = len(text) |
187 n = len(text) |
176 for list in [keyword.kwlist, |
188 for word in keyword.kwlist: |
177 __builtin__.__dict__.keys(), |
189 if word[:n] == text: |
178 self.namespace.keys()]: |
190 matches.append(word) |
179 for word in list: |
191 for nspace in [__builtin__.__dict__, self.namespace]: |
180 if word[:n] == text and \ |
192 for word, val in nspace.items(): |
181 word != "__builtins__" and \ |
193 if word[:n] == text and word != "__builtins__": |
182 word not in matches: |
194 matches.append(self._callable_postfix(val, word)) |
183 matches.append(word) |
|
184 return matches |
195 return matches |
185 |
196 |
186 def attr_matches(self, text): |
197 def attr_matches(self, text): |
187 """ |
198 """ |
188 Public method to compute matches when text contains a dot. |
199 Public method to compute matches when text contains a dot. |
210 m = re.match(r"(\S+(\.\w+)*)\.(\w*)", text) |
221 m = re.match(r"(\S+(\.\w+)*)\.(\w*)", text) |
211 |
222 |
212 if not m: |
223 if not m: |
213 return |
224 return |
214 expr, attr = m.group(1, 3) |
225 expr, attr = m.group(1, 3) |
215 object = eval(expr, self.namespace) |
226 try: |
216 words = dir(object) |
227 thisobject = eval(expr, self.namespace) |
|
228 except Exception: |
|
229 return [] |
|
230 |
|
231 # get the content of the object, except __builtins__ |
|
232 words = dir(thisobject) |
|
233 if "__builtins__" in words: |
|
234 words.remove("__builtins__") |
|
235 |
217 if hasattr(object, '__class__'): |
236 if hasattr(object, '__class__'): |
218 words.append('__class__') |
237 words.append('__class__') |
219 words = words + get_class_members(object.__class__) |
238 words = words + get_class_members(object.__class__) |
220 matches = [] |
239 matches = [] |
221 n = len(attr) |
240 n = len(attr) |
222 for word in words: |
241 for word in words: |
223 try: |
242 try: |
224 if word[:n] == attr and word != "__builtins__": |
243 if word[:n] == attr and hasattr(thisobject, word): |
225 match = "%s.%s" % (expr, word) |
244 val = getattr(thisobject, word) |
226 if match not in matches: |
245 word = self._callable_postfix( |
227 matches.append(match) |
246 val, "%s.%s" % (expr, word)) |
228 except: |
247 matches.append(word) |
229 # some badly behaved objects pollute dir() with non-strings, |
248 # some badly behaved objects pollute dir() with non-strings, |
230 # which cause the completion to fail. This way we skip the |
249 # which cause the completion to fail. This way we skip the |
231 # bad entries and can still continue processing the others. |
250 # bad entries and can still continue processing the others. |
232 pass |
251 pass |
233 return matches |
252 return matches |