Show More
The requested changes are too big and content was truncated. Show full diff
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,607 +1,607 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tools for inspecting Python objects. |
|
3 | 3 | |
|
4 | 4 | Uses syntax highlighting for presenting the various information elements. |
|
5 | 5 | |
|
6 | 6 | Similar in spirit to the inspect module, but all calls take a name argument to |
|
7 | 7 | reference the name under which an object is being read. |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | 14 | # the file COPYING, distributed as part of this software. |
|
15 | 15 | #***************************************************************************** |
|
16 | 16 | |
|
17 | 17 | __all__ = ['Inspector','InspectColors'] |
|
18 | 18 | |
|
19 | 19 | # stdlib modules |
|
20 | 20 | import __builtin__ |
|
21 | 21 | import StringIO |
|
22 | 22 | import inspect |
|
23 | 23 | import linecache |
|
24 | 24 | import os |
|
25 | 25 | import string |
|
26 | 26 | import sys |
|
27 | 27 | import types |
|
28 | 28 | |
|
29 | 29 | # IPython's own |
|
30 | 30 | from IPython.utils import PyColorize |
|
31 | 31 | from IPython.utils.genutils import page,indent,Term |
|
32 | 32 | from IPython.external.Itpl import itpl |
|
33 | from IPython.wildcard import list_namespace | |
|
33 | from IPython.utils.wildcard import list_namespace | |
|
34 | 34 | from IPython.utils.coloransi import * |
|
35 | 35 | |
|
36 | 36 | #**************************************************************************** |
|
37 | 37 | # HACK!!! This is a crude fix for bugs in python 2.3's inspect module. We |
|
38 | 38 | # simply monkeypatch inspect with code copied from python 2.4. |
|
39 | 39 | if sys.version_info[:2] == (2,3): |
|
40 | 40 | from inspect import ismodule, getabsfile, modulesbyfile |
|
41 | 41 | def getmodule(object): |
|
42 | 42 | """Return the module an object was defined in, or None if not found.""" |
|
43 | 43 | if ismodule(object): |
|
44 | 44 | return object |
|
45 | 45 | if hasattr(object, '__module__'): |
|
46 | 46 | return sys.modules.get(object.__module__) |
|
47 | 47 | try: |
|
48 | 48 | file = getabsfile(object) |
|
49 | 49 | except TypeError: |
|
50 | 50 | return None |
|
51 | 51 | if file in modulesbyfile: |
|
52 | 52 | return sys.modules.get(modulesbyfile[file]) |
|
53 | 53 | for module in sys.modules.values(): |
|
54 | 54 | if hasattr(module, '__file__'): |
|
55 | 55 | modulesbyfile[ |
|
56 | 56 | os.path.realpath( |
|
57 | 57 | getabsfile(module))] = module.__name__ |
|
58 | 58 | if file in modulesbyfile: |
|
59 | 59 | return sys.modules.get(modulesbyfile[file]) |
|
60 | 60 | main = sys.modules['__main__'] |
|
61 | 61 | if not hasattr(object, '__name__'): |
|
62 | 62 | return None |
|
63 | 63 | if hasattr(main, object.__name__): |
|
64 | 64 | mainobject = getattr(main, object.__name__) |
|
65 | 65 | if mainobject is object: |
|
66 | 66 | return main |
|
67 | 67 | builtin = sys.modules['__builtin__'] |
|
68 | 68 | if hasattr(builtin, object.__name__): |
|
69 | 69 | builtinobject = getattr(builtin, object.__name__) |
|
70 | 70 | if builtinobject is object: |
|
71 | 71 | return builtin |
|
72 | 72 | |
|
73 | 73 | inspect.getmodule = getmodule |
|
74 | 74 | |
|
75 | 75 | #**************************************************************************** |
|
76 | 76 | # Builtin color schemes |
|
77 | 77 | |
|
78 | 78 | Colors = TermColors # just a shorthand |
|
79 | 79 | |
|
80 | 80 | # Build a few color schemes |
|
81 | 81 | NoColor = ColorScheme( |
|
82 | 82 | 'NoColor',{ |
|
83 | 83 | 'header' : Colors.NoColor, |
|
84 | 84 | 'normal' : Colors.NoColor # color off (usu. Colors.Normal) |
|
85 | 85 | } ) |
|
86 | 86 | |
|
87 | 87 | LinuxColors = ColorScheme( |
|
88 | 88 | 'Linux',{ |
|
89 | 89 | 'header' : Colors.LightRed, |
|
90 | 90 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) |
|
91 | 91 | } ) |
|
92 | 92 | |
|
93 | 93 | LightBGColors = ColorScheme( |
|
94 | 94 | 'LightBG',{ |
|
95 | 95 | 'header' : Colors.Red, |
|
96 | 96 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) |
|
97 | 97 | } ) |
|
98 | 98 | |
|
99 | 99 | # Build table of color schemes (needed by the parser) |
|
100 | 100 | InspectColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors], |
|
101 | 101 | 'Linux') |
|
102 | 102 | |
|
103 | 103 | #**************************************************************************** |
|
104 | 104 | # Auxiliary functions |
|
105 | 105 | def getdoc(obj): |
|
106 | 106 | """Stable wrapper around inspect.getdoc. |
|
107 | 107 | |
|
108 | 108 | This can't crash because of attribute problems. |
|
109 | 109 | |
|
110 | 110 | It also attempts to call a getdoc() method on the given object. This |
|
111 | 111 | allows objects which provide their docstrings via non-standard mechanisms |
|
112 | 112 | (like Pyro proxies) to still be inspected by ipython's ? system.""" |
|
113 | 113 | |
|
114 | 114 | ds = None # default return value |
|
115 | 115 | try: |
|
116 | 116 | ds = inspect.getdoc(obj) |
|
117 | 117 | except: |
|
118 | 118 | # Harden against an inspect failure, which can occur with |
|
119 | 119 | # SWIG-wrapped extensions. |
|
120 | 120 | pass |
|
121 | 121 | # Allow objects to offer customized documentation via a getdoc method: |
|
122 | 122 | try: |
|
123 | 123 | ds2 = obj.getdoc() |
|
124 | 124 | except: |
|
125 | 125 | pass |
|
126 | 126 | else: |
|
127 | 127 | # if we get extra info, we add it to the normal docstring. |
|
128 | 128 | if ds is None: |
|
129 | 129 | ds = ds2 |
|
130 | 130 | else: |
|
131 | 131 | ds = '%s\n%s' % (ds,ds2) |
|
132 | 132 | return ds |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | def getsource(obj,is_binary=False): |
|
136 | 136 | """Wrapper around inspect.getsource. |
|
137 | 137 | |
|
138 | 138 | This can be modified by other projects to provide customized source |
|
139 | 139 | extraction. |
|
140 | 140 | |
|
141 | 141 | Inputs: |
|
142 | 142 | |
|
143 | 143 | - obj: an object whose source code we will attempt to extract. |
|
144 | 144 | |
|
145 | 145 | Optional inputs: |
|
146 | 146 | |
|
147 | 147 | - is_binary: whether the object is known to come from a binary source. |
|
148 | 148 | This implementation will skip returning any output for binary objects, but |
|
149 | 149 | custom extractors may know how to meaningfully process them.""" |
|
150 | 150 | |
|
151 | 151 | if is_binary: |
|
152 | 152 | return None |
|
153 | 153 | else: |
|
154 | 154 | try: |
|
155 | 155 | src = inspect.getsource(obj) |
|
156 | 156 | except TypeError: |
|
157 | 157 | if hasattr(obj,'__class__'): |
|
158 | 158 | src = inspect.getsource(obj.__class__) |
|
159 | 159 | return src |
|
160 | 160 | |
|
161 | 161 | def getargspec(obj): |
|
162 | 162 | """Get the names and default values of a function's arguments. |
|
163 | 163 | |
|
164 | 164 | A tuple of four things is returned: (args, varargs, varkw, defaults). |
|
165 | 165 | 'args' is a list of the argument names (it may contain nested lists). |
|
166 | 166 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
|
167 | 167 | 'defaults' is an n-tuple of the default values of the last n arguments. |
|
168 | 168 | |
|
169 | 169 | Modified version of inspect.getargspec from the Python Standard |
|
170 | 170 | Library.""" |
|
171 | 171 | |
|
172 | 172 | if inspect.isfunction(obj): |
|
173 | 173 | func_obj = obj |
|
174 | 174 | elif inspect.ismethod(obj): |
|
175 | 175 | func_obj = obj.im_func |
|
176 | 176 | else: |
|
177 | 177 | raise TypeError, 'arg is not a Python function' |
|
178 | 178 | args, varargs, varkw = inspect.getargs(func_obj.func_code) |
|
179 | 179 | return args, varargs, varkw, func_obj.func_defaults |
|
180 | 180 | |
|
181 | 181 | #**************************************************************************** |
|
182 | 182 | # Class definitions |
|
183 | 183 | |
|
184 | 184 | class myStringIO(StringIO.StringIO): |
|
185 | 185 | """Adds a writeln method to normal StringIO.""" |
|
186 | 186 | def writeln(self,*arg,**kw): |
|
187 | 187 | """Does a write() and then a write('\n')""" |
|
188 | 188 | self.write(*arg,**kw) |
|
189 | 189 | self.write('\n') |
|
190 | 190 | |
|
191 | 191 | |
|
192 | 192 | class Inspector: |
|
193 | 193 | def __init__(self,color_table,code_color_table,scheme, |
|
194 | 194 | str_detail_level=0): |
|
195 | 195 | self.color_table = color_table |
|
196 | 196 | self.parser = PyColorize.Parser(code_color_table,out='str') |
|
197 | 197 | self.format = self.parser.format |
|
198 | 198 | self.str_detail_level = str_detail_level |
|
199 | 199 | self.set_active_scheme(scheme) |
|
200 | 200 | |
|
201 | 201 | def __getdef(self,obj,oname=''): |
|
202 | 202 | """Return the definition header for any callable object. |
|
203 | 203 | |
|
204 | 204 | If any exception is generated, None is returned instead and the |
|
205 | 205 | exception is suppressed.""" |
|
206 | 206 | |
|
207 | 207 | try: |
|
208 | 208 | return oname + inspect.formatargspec(*getargspec(obj)) |
|
209 | 209 | except: |
|
210 | 210 | return None |
|
211 | 211 | |
|
212 | 212 | def __head(self,h): |
|
213 | 213 | """Return a header string with proper colors.""" |
|
214 | 214 | return '%s%s%s' % (self.color_table.active_colors.header,h, |
|
215 | 215 | self.color_table.active_colors.normal) |
|
216 | 216 | |
|
217 | 217 | def set_active_scheme(self,scheme): |
|
218 | 218 | self.color_table.set_active_scheme(scheme) |
|
219 | 219 | self.parser.color_table.set_active_scheme(scheme) |
|
220 | 220 | |
|
221 | 221 | def noinfo(self,msg,oname): |
|
222 | 222 | """Generic message when no information is found.""" |
|
223 | 223 | print 'No %s found' % msg, |
|
224 | 224 | if oname: |
|
225 | 225 | print 'for %s' % oname |
|
226 | 226 | else: |
|
227 | 227 | |
|
228 | 228 | |
|
229 | 229 | def pdef(self,obj,oname=''): |
|
230 | 230 | """Print the definition header for any callable object. |
|
231 | 231 | |
|
232 | 232 | If the object is a class, print the constructor information.""" |
|
233 | 233 | |
|
234 | 234 | if not callable(obj): |
|
235 | 235 | print 'Object is not callable.' |
|
236 | 236 | return |
|
237 | 237 | |
|
238 | 238 | header = '' |
|
239 | 239 | |
|
240 | 240 | if inspect.isclass(obj): |
|
241 | 241 | header = self.__head('Class constructor information:\n') |
|
242 | 242 | obj = obj.__init__ |
|
243 | 243 | elif type(obj) is types.InstanceType: |
|
244 | 244 | obj = obj.__call__ |
|
245 | 245 | |
|
246 | 246 | output = self.__getdef(obj,oname) |
|
247 | 247 | if output is None: |
|
248 | 248 | self.noinfo('definition header',oname) |
|
249 | 249 | else: |
|
250 | 250 | print >>Term.cout, header,self.format(output), |
|
251 | 251 | |
|
252 | 252 | def pdoc(self,obj,oname='',formatter = None): |
|
253 | 253 | """Print the docstring for any object. |
|
254 | 254 | |
|
255 | 255 | Optional: |
|
256 | 256 | -formatter: a function to run the docstring through for specially |
|
257 | 257 | formatted docstrings.""" |
|
258 | 258 | |
|
259 | 259 | head = self.__head # so that itpl can find it even if private |
|
260 | 260 | ds = getdoc(obj) |
|
261 | 261 | if formatter: |
|
262 | 262 | ds = formatter(ds) |
|
263 | 263 | if inspect.isclass(obj): |
|
264 | 264 | init_ds = getdoc(obj.__init__) |
|
265 | 265 | output = itpl('$head("Class Docstring:")\n' |
|
266 | 266 | '$indent(ds)\n' |
|
267 | 267 | '$head("Constructor Docstring"):\n' |
|
268 | 268 | '$indent(init_ds)') |
|
269 | 269 | elif (type(obj) is types.InstanceType or isinstance(obj,object)) \ |
|
270 | 270 | and hasattr(obj,'__call__'): |
|
271 | 271 | call_ds = getdoc(obj.__call__) |
|
272 | 272 | if call_ds: |
|
273 | 273 | output = itpl('$head("Class Docstring:")\n$indent(ds)\n' |
|
274 | 274 | '$head("Calling Docstring:")\n$indent(call_ds)') |
|
275 | 275 | else: |
|
276 | 276 | output = ds |
|
277 | 277 | else: |
|
278 | 278 | output = ds |
|
279 | 279 | if output is None: |
|
280 | 280 | self.noinfo('documentation',oname) |
|
281 | 281 | return |
|
282 | 282 | page(output) |
|
283 | 283 | |
|
284 | 284 | def psource(self,obj,oname=''): |
|
285 | 285 | """Print the source code for an object.""" |
|
286 | 286 | |
|
287 | 287 | # Flush the source cache because inspect can return out-of-date source |
|
288 | 288 | linecache.checkcache() |
|
289 | 289 | try: |
|
290 | 290 | src = getsource(obj) |
|
291 | 291 | except: |
|
292 | 292 | self.noinfo('source',oname) |
|
293 | 293 | else: |
|
294 | 294 | page(self.format(src)) |
|
295 | 295 | |
|
296 | 296 | def pfile(self,obj,oname=''): |
|
297 | 297 | """Show the whole file where an object was defined.""" |
|
298 | 298 | |
|
299 | 299 | try: |
|
300 | 300 | try: |
|
301 | 301 | lineno = inspect.getsourcelines(obj)[1] |
|
302 | 302 | except TypeError: |
|
303 | 303 | # For instances, try the class object like getsource() does |
|
304 | 304 | if hasattr(obj,'__class__'): |
|
305 | 305 | lineno = inspect.getsourcelines(obj.__class__)[1] |
|
306 | 306 | # Adjust the inspected object so getabsfile() below works |
|
307 | 307 | obj = obj.__class__ |
|
308 | 308 | except: |
|
309 | 309 | self.noinfo('file',oname) |
|
310 | 310 | return |
|
311 | 311 | |
|
312 | 312 | # We only reach this point if object was successfully queried |
|
313 | 313 | |
|
314 | 314 | # run contents of file through pager starting at line |
|
315 | 315 | # where the object is defined |
|
316 | 316 | ofile = inspect.getabsfile(obj) |
|
317 | 317 | |
|
318 | 318 | if (ofile.endswith('.so') or ofile.endswith('.dll')): |
|
319 | 319 | print 'File %r is binary, not printing.' % ofile |
|
320 | 320 | elif not os.path.isfile(ofile): |
|
321 | 321 | print 'File %r does not exist, not printing.' % ofile |
|
322 | 322 | else: |
|
323 | 323 | # Print only text files, not extension binaries. Note that |
|
324 | 324 | # getsourcelines returns lineno with 1-offset and page() uses |
|
325 | 325 | # 0-offset, so we must adjust. |
|
326 | 326 | page(self.format(open(ofile).read()),lineno-1) |
|
327 | 327 | |
|
328 | 328 | def pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0): |
|
329 | 329 | """Show detailed information about an object. |
|
330 | 330 | |
|
331 | 331 | Optional arguments: |
|
332 | 332 | |
|
333 | 333 | - oname: name of the variable pointing to the object. |
|
334 | 334 | |
|
335 | 335 | - formatter: special formatter for docstrings (see pdoc) |
|
336 | 336 | |
|
337 | 337 | - info: a structure with some information fields which may have been |
|
338 | 338 | precomputed already. |
|
339 | 339 | |
|
340 | 340 | - detail_level: if set to 1, more information is given. |
|
341 | 341 | """ |
|
342 | 342 | |
|
343 | 343 | obj_type = type(obj) |
|
344 | 344 | |
|
345 | 345 | header = self.__head |
|
346 | 346 | if info is None: |
|
347 | 347 | ismagic = 0 |
|
348 | 348 | isalias = 0 |
|
349 | 349 | ospace = '' |
|
350 | 350 | else: |
|
351 | 351 | ismagic = info.ismagic |
|
352 | 352 | isalias = info.isalias |
|
353 | 353 | ospace = info.namespace |
|
354 | 354 | # Get docstring, special-casing aliases: |
|
355 | 355 | if isalias: |
|
356 | 356 | if not callable(obj): |
|
357 | 357 | try: |
|
358 | 358 | ds = "Alias to the system command:\n %s" % obj[1] |
|
359 | 359 | except: |
|
360 | 360 | ds = "Alias: " + str(obj) |
|
361 | 361 | else: |
|
362 | 362 | ds = "Alias to " + str(obj) |
|
363 | 363 | if obj.__doc__: |
|
364 | 364 | ds += "\nDocstring:\n" + obj.__doc__ |
|
365 | 365 | else: |
|
366 | 366 | ds = getdoc(obj) |
|
367 | 367 | if ds is None: |
|
368 | 368 | ds = '<no docstring>' |
|
369 | 369 | if formatter is not None: |
|
370 | 370 | ds = formatter(ds) |
|
371 | 371 | |
|
372 | 372 | # store output in a list which gets joined with \n at the end. |
|
373 | 373 | out = myStringIO() |
|
374 | 374 | |
|
375 | 375 | string_max = 200 # max size of strings to show (snipped if longer) |
|
376 | 376 | shalf = int((string_max -5)/2) |
|
377 | 377 | |
|
378 | 378 | if ismagic: |
|
379 | 379 | obj_type_name = 'Magic function' |
|
380 | 380 | elif isalias: |
|
381 | 381 | obj_type_name = 'System alias' |
|
382 | 382 | else: |
|
383 | 383 | obj_type_name = obj_type.__name__ |
|
384 | 384 | out.writeln(header('Type:\t\t')+obj_type_name) |
|
385 | 385 | |
|
386 | 386 | try: |
|
387 | 387 | bclass = obj.__class__ |
|
388 | 388 | out.writeln(header('Base Class:\t')+str(bclass)) |
|
389 | 389 | except: pass |
|
390 | 390 | |
|
391 | 391 | # String form, but snip if too long in ? form (full in ??) |
|
392 | 392 | if detail_level >= self.str_detail_level: |
|
393 | 393 | try: |
|
394 | 394 | ostr = str(obj) |
|
395 | 395 | str_head = 'String Form:' |
|
396 | 396 | if not detail_level and len(ostr)>string_max: |
|
397 | 397 | ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:] |
|
398 | 398 | ostr = ("\n" + " " * len(str_head.expandtabs())).\ |
|
399 | 399 | join(map(string.strip,ostr.split("\n"))) |
|
400 | 400 | if ostr.find('\n') > -1: |
|
401 | 401 | # Print multi-line strings starting at the next line. |
|
402 | 402 | str_sep = '\n' |
|
403 | 403 | else: |
|
404 | 404 | str_sep = '\t' |
|
405 | 405 | out.writeln("%s%s%s" % (header(str_head),str_sep,ostr)) |
|
406 | 406 | except: |
|
407 | 407 | pass |
|
408 | 408 | |
|
409 | 409 | if ospace: |
|
410 | 410 | out.writeln(header('Namespace:\t')+ospace) |
|
411 | 411 | |
|
412 | 412 | # Length (for strings and lists) |
|
413 | 413 | try: |
|
414 | 414 | length = str(len(obj)) |
|
415 | 415 | out.writeln(header('Length:\t\t')+length) |
|
416 | 416 | except: pass |
|
417 | 417 | |
|
418 | 418 | # Filename where object was defined |
|
419 | 419 | binary_file = False |
|
420 | 420 | try: |
|
421 | 421 | try: |
|
422 | 422 | fname = inspect.getabsfile(obj) |
|
423 | 423 | except TypeError: |
|
424 | 424 | # For an instance, the file that matters is where its class was |
|
425 | 425 | # declared. |
|
426 | 426 | if hasattr(obj,'__class__'): |
|
427 | 427 | fname = inspect.getabsfile(obj.__class__) |
|
428 | 428 | if fname.endswith('<string>'): |
|
429 | 429 | fname = 'Dynamically generated function. No source code available.' |
|
430 | 430 | if (fname.endswith('.so') or fname.endswith('.dll')): |
|
431 | 431 | binary_file = True |
|
432 | 432 | out.writeln(header('File:\t\t')+fname) |
|
433 | 433 | except: |
|
434 | 434 | # if anything goes wrong, we don't want to show source, so it's as |
|
435 | 435 | # if the file was binary |
|
436 | 436 | binary_file = True |
|
437 | 437 | |
|
438 | 438 | # reconstruct the function definition and print it: |
|
439 | 439 | defln = self.__getdef(obj,oname) |
|
440 | 440 | if defln: |
|
441 | 441 | out.write(header('Definition:\t')+self.format(defln)) |
|
442 | 442 | |
|
443 | 443 | # Docstrings only in detail 0 mode, since source contains them (we |
|
444 | 444 | # avoid repetitions). If source fails, we add them back, see below. |
|
445 | 445 | if ds and detail_level == 0: |
|
446 | 446 | out.writeln(header('Docstring:\n') + indent(ds)) |
|
447 | 447 | |
|
448 | 448 | # Original source code for any callable |
|
449 | 449 | if detail_level: |
|
450 | 450 | # Flush the source cache because inspect can return out-of-date |
|
451 | 451 | # source |
|
452 | 452 | linecache.checkcache() |
|
453 | 453 | source_success = False |
|
454 | 454 | try: |
|
455 | 455 | try: |
|
456 | 456 | src = getsource(obj,binary_file) |
|
457 | 457 | except TypeError: |
|
458 | 458 | if hasattr(obj,'__class__'): |
|
459 | 459 | src = getsource(obj.__class__,binary_file) |
|
460 | 460 | if src is not None: |
|
461 | 461 | source = self.format(src) |
|
462 | 462 | out.write(header('Source:\n')+source.rstrip()) |
|
463 | 463 | source_success = True |
|
464 | 464 | except Exception, msg: |
|
465 | 465 | pass |
|
466 | 466 | |
|
467 | 467 | if ds and not source_success: |
|
468 | 468 | out.writeln(header('Docstring [source file open failed]:\n') |
|
469 | 469 | + indent(ds)) |
|
470 | 470 | |
|
471 | 471 | # Constructor docstring for classes |
|
472 | 472 | if inspect.isclass(obj): |
|
473 | 473 | # reconstruct the function definition and print it: |
|
474 | 474 | try: |
|
475 | 475 | obj_init = obj.__init__ |
|
476 | 476 | except AttributeError: |
|
477 | 477 | init_def = init_ds = None |
|
478 | 478 | else: |
|
479 | 479 | init_def = self.__getdef(obj_init,oname) |
|
480 | 480 | init_ds = getdoc(obj_init) |
|
481 | 481 | # Skip Python's auto-generated docstrings |
|
482 | 482 | if init_ds and \ |
|
483 | 483 | init_ds.startswith('x.__init__(...) initializes'): |
|
484 | 484 | init_ds = None |
|
485 | 485 | |
|
486 | 486 | if init_def or init_ds: |
|
487 | 487 | out.writeln(header('\nConstructor information:')) |
|
488 | 488 | if init_def: |
|
489 | 489 | out.write(header('Definition:\t')+ self.format(init_def)) |
|
490 | 490 | if init_ds: |
|
491 | 491 | out.writeln(header('Docstring:\n') + indent(init_ds)) |
|
492 | 492 | # and class docstring for instances: |
|
493 | 493 | elif obj_type is types.InstanceType or \ |
|
494 | 494 | isinstance(obj,object): |
|
495 | 495 | |
|
496 | 496 | # First, check whether the instance docstring is identical to the |
|
497 | 497 | # class one, and print it separately if they don't coincide. In |
|
498 | 498 | # most cases they will, but it's nice to print all the info for |
|
499 | 499 | # objects which use instance-customized docstrings. |
|
500 | 500 | if ds: |
|
501 | 501 | try: |
|
502 | 502 | cls = getattr(obj,'__class__') |
|
503 | 503 | except: |
|
504 | 504 | class_ds = None |
|
505 | 505 | else: |
|
506 | 506 | class_ds = getdoc(cls) |
|
507 | 507 | # Skip Python's auto-generated docstrings |
|
508 | 508 | if class_ds and \ |
|
509 | 509 | (class_ds.startswith('function(code, globals[,') or \ |
|
510 | 510 | class_ds.startswith('instancemethod(function, instance,') or \ |
|
511 | 511 | class_ds.startswith('module(name[,') ): |
|
512 | 512 | class_ds = None |
|
513 | 513 | if class_ds and ds != class_ds: |
|
514 | 514 | out.writeln(header('Class Docstring:\n') + |
|
515 | 515 | indent(class_ds)) |
|
516 | 516 | |
|
517 | 517 | # Next, try to show constructor docstrings |
|
518 | 518 | try: |
|
519 | 519 | init_ds = getdoc(obj.__init__) |
|
520 | 520 | # Skip Python's auto-generated docstrings |
|
521 | 521 | if init_ds and \ |
|
522 | 522 | init_ds.startswith('x.__init__(...) initializes'): |
|
523 | 523 | init_ds = None |
|
524 | 524 | except AttributeError: |
|
525 | 525 | init_ds = None |
|
526 | 526 | if init_ds: |
|
527 | 527 | out.writeln(header('Constructor Docstring:\n') + |
|
528 | 528 | indent(init_ds)) |
|
529 | 529 | |
|
530 | 530 | # Call form docstring for callable instances |
|
531 | 531 | if hasattr(obj,'__call__'): |
|
532 | 532 | #out.writeln(header('Callable:\t')+'Yes') |
|
533 | 533 | call_def = self.__getdef(obj.__call__,oname) |
|
534 | 534 | #if call_def is None: |
|
535 | 535 | # out.writeln(header('Call def:\t')+ |
|
536 | 536 | # 'Calling definition not available.') |
|
537 | 537 | if call_def is not None: |
|
538 | 538 | out.writeln(header('Call def:\t')+self.format(call_def)) |
|
539 | 539 | call_ds = getdoc(obj.__call__) |
|
540 | 540 | # Skip Python's auto-generated docstrings |
|
541 | 541 | if call_ds and call_ds.startswith('x.__call__(...) <==> x(...)'): |
|
542 | 542 | call_ds = None |
|
543 | 543 | if call_ds: |
|
544 | 544 | out.writeln(header('Call docstring:\n') + indent(call_ds)) |
|
545 | 545 | |
|
546 | 546 | # Finally send to printer/pager |
|
547 | 547 | output = out.getvalue() |
|
548 | 548 | if output: |
|
549 | 549 | page(output) |
|
550 | 550 | # end pinfo |
|
551 | 551 | |
|
552 | 552 | def psearch(self,pattern,ns_table,ns_search=[], |
|
553 | 553 | ignore_case=False,show_all=False): |
|
554 | 554 | """Search namespaces with wildcards for objects. |
|
555 | 555 | |
|
556 | 556 | Arguments: |
|
557 | 557 | |
|
558 | 558 | - pattern: string containing shell-like wildcards to use in namespace |
|
559 | 559 | searches and optionally a type specification to narrow the search to |
|
560 | 560 | objects of that type. |
|
561 | 561 | |
|
562 | 562 | - ns_table: dict of name->namespaces for search. |
|
563 | 563 | |
|
564 | 564 | Optional arguments: |
|
565 | 565 | |
|
566 | 566 | - ns_search: list of namespace names to include in search. |
|
567 | 567 | |
|
568 | 568 | - ignore_case(False): make the search case-insensitive. |
|
569 | 569 | |
|
570 | 570 | - show_all(False): show all names, including those starting with |
|
571 | 571 | underscores. |
|
572 | 572 | """ |
|
573 | 573 | #print 'ps pattern:<%r>' % pattern # dbg |
|
574 | 574 | |
|
575 | 575 | # defaults |
|
576 | 576 | type_pattern = 'all' |
|
577 | 577 | filter = '' |
|
578 | 578 | |
|
579 | 579 | cmds = pattern.split() |
|
580 | 580 | len_cmds = len(cmds) |
|
581 | 581 | if len_cmds == 1: |
|
582 | 582 | # Only filter pattern given |
|
583 | 583 | filter = cmds[0] |
|
584 | 584 | elif len_cmds == 2: |
|
585 | 585 | # Both filter and type specified |
|
586 | 586 | filter,type_pattern = cmds |
|
587 | 587 | else: |
|
588 | 588 | raise ValueError('invalid argument string for psearch: <%s>' % |
|
589 | 589 | pattern) |
|
590 | 590 | |
|
591 | 591 | # filter search namespaces |
|
592 | 592 | for name in ns_search: |
|
593 | 593 | if name not in ns_table: |
|
594 | 594 | raise ValueError('invalid namespace <%s>. Valid names: %s' % |
|
595 | 595 | (name,ns_table.keys())) |
|
596 | 596 | |
|
597 | 597 | #print 'type_pattern:',type_pattern # dbg |
|
598 | 598 | search_result = [] |
|
599 | 599 | for ns_name in ns_search: |
|
600 | 600 | ns = ns_table[ns_name] |
|
601 | 601 | tmp_res = list(list_namespace(ns,type_pattern,filter, |
|
602 | 602 | ignore_case=ignore_case, |
|
603 | 603 | show_all=show_all)) |
|
604 | 604 | search_result.extend(tmp_res) |
|
605 | 605 | search_result.sort() |
|
606 | 606 | |
|
607 | 607 | page('\n'.join(search_result)) |
@@ -1,32 +1,35 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | |
|
4 | 4 | def test_import_coloransi(): |
|
5 | 5 | from IPython.utils import coloransi |
|
6 | 6 | |
|
7 | 7 | def test_import_DPyGetOpt(): |
|
8 | 8 | from IPython.utils import DPyGetOpt |
|
9 | 9 | |
|
10 | 10 | def test_import_generics(): |
|
11 | 11 | from IPython.utils import generics |
|
12 | 12 | |
|
13 | 13 | def test_import_genutils(): |
|
14 | 14 | from IPython.utils import genutils |
|
15 | 15 | |
|
16 | 16 | def test_import_ipstruct(): |
|
17 | 17 | from IPython.utils import ipstruct |
|
18 | 18 | |
|
19 | 19 | def test_import_platutils(): |
|
20 | 20 | from IPython.utils import platutils |
|
21 | 21 | |
|
22 | 22 | def test_import_PyColorize(): |
|
23 | 23 | from IPython.utils import PyColorize |
|
24 | 24 | |
|
25 | 25 | def test_import_rlineimpl(): |
|
26 | 26 | from IPython.utils import rlineimpl |
|
27 | 27 | |
|
28 | 28 | def test_import_strdispatch(): |
|
29 | 29 | from IPython.utils import strdispatch |
|
30 | 30 | |
|
31 | 31 | def test_import_upgradedir(): |
|
32 | 32 | from IPython.utils import upgradedir |
|
33 | ||
|
34 | def test_import_wildcard(): | |
|
35 | from IPython.utils import wildcard |
|
1 | NO CONTENT: file renamed from IPython/wildcard.py to IPython/utils/wildcard.py |
@@ -1,270 +1,270 b'' | |||
|
1 | 1 | ============================= |
|
2 | 2 | IPython module reorganization |
|
3 | 3 | ============================= |
|
4 | 4 | |
|
5 | 5 | Currently, IPython has many top-level modules that serve many different purposes. |
|
6 | 6 | The lack of organization make it very difficult for developers to work on IPython |
|
7 | 7 | and understand its design. This document contains notes about how we will reorganize |
|
8 | 8 | the modules into sub-packages. |
|
9 | 9 | |
|
10 | 10 | .. warning:: |
|
11 | 11 | |
|
12 | 12 | This effort will possibly break third party packages that use IPython as |
|
13 | 13 | a library or hack on the IPython internals. |
|
14 | 14 | |
|
15 | 15 | .. warning:: |
|
16 | 16 | |
|
17 | 17 | This effort will result in the removal from IPython of certain modules |
|
18 | 18 | that are not used anymore, don't currently work, are unmaintained, etc. |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | Current subpackges |
|
22 | 22 | ================== |
|
23 | 23 | |
|
24 | 24 | IPython currently has the following sub-packages: |
|
25 | 25 | |
|
26 | 26 | * :mod:`IPython.config` |
|
27 | 27 | |
|
28 | 28 | * :mod:`IPython.Extensions` |
|
29 | 29 | |
|
30 | 30 | * :mod:`IPython.external` |
|
31 | 31 | |
|
32 | 32 | * :mod:`IPython.frontend` |
|
33 | 33 | |
|
34 | 34 | * :mod:`IPython.gui` |
|
35 | 35 | |
|
36 | 36 | * :mod:`IPython.kernel` |
|
37 | 37 | |
|
38 | 38 | * :mod:`IPython.testing` |
|
39 | 39 | |
|
40 | 40 | * :mod:`IPython.tests` |
|
41 | 41 | |
|
42 | 42 | * :mod:`IPython.tools` |
|
43 | 43 | |
|
44 | 44 | * :mod:`IPython.UserConfig` |
|
45 | 45 | |
|
46 | 46 | New Subpackages to be created |
|
47 | 47 | ============================= |
|
48 | 48 | |
|
49 | 49 | We propose to create the following new sub-packages: |
|
50 | 50 | |
|
51 | 51 | * :mod:`IPython.core`. This sub-package will contain the core of the IPython |
|
52 | 52 | interpreter, but none of its extended capabilities. |
|
53 | 53 | |
|
54 | 54 | * :mod:`IPython.lib`. IPython has many extended capabilities that are not part |
|
55 | 55 | of the IPython core. These things will go here. Any better names than |
|
56 | 56 | :mod:`IPython.lib`? |
|
57 | 57 | |
|
58 | 58 | * :mod:`IPython.utils`. This sub-package will contain anything that might |
|
59 | 59 | eventually be found in the Python standard library, like things in |
|
60 | 60 | :mod:`genutils`. Each sub-module in this sub-package should contain |
|
61 | 61 | functions and classes that serve a single purpose. |
|
62 | 62 | |
|
63 | 63 | * :mod:`IPython.deathrow`. This is for code that is untested and/or rotting |
|
64 | 64 | and needs to be removed from IPython. Eventually all this code will either |
|
65 | 65 | i) be revived by someone willing to maintain it with tests and docs and |
|
66 | 66 | re-included into IPython or 2) be removed from IPython proper, but put into |
|
67 | 67 | a separate top-level (not IPython) package that we keep around. No new code |
|
68 | 68 | will be allowed here. |
|
69 | 69 | |
|
70 | 70 | * :mod:`IPython.quarantine`. This is for code that doesn't meet IPython's |
|
71 | 71 | standards, but that we plan on keeping. To be moved out of this sub-package |
|
72 | 72 | a module needs to have a maintainer, tests and documentation. |
|
73 | 73 | |
|
74 | 74 | Prodecure |
|
75 | 75 | ========= |
|
76 | 76 | |
|
77 | 77 | 1. Move the file to its new location with its new name. |
|
78 | 78 | 2. Rename all import statements to reflect the change. |
|
79 | 79 | 3. Run PyFlakes on each changes module. |
|
80 | 80 | 3. Add tests/test_imports.py to test it. |
|
81 | 81 | |
|
82 | 82 | Need to modify iptests to properly skip modules that are no longer top |
|
83 | 83 | level modules. |
|
84 | 84 | |
|
85 | 85 | Need to update the top level IPython/__init__.py file. |
|
86 | 86 | |
|
87 | 87 | Where things will be moved |
|
88 | 88 | ========================== |
|
89 | 89 | |
|
90 | 90 | * :file:`background_jobs.py`. Move to :file:`IPython/lib/backgroundjobs.py`. |
|
91 | 91 | |
|
92 | 92 | * :file:`ColorANSI.py`. Move to :file:`IPython/utils/coloransi.py`. |
|
93 | 93 | |
|
94 | 94 | * :file:`completer.py`. Move to :file:`IPython/core/completer.py`. |
|
95 | 95 | |
|
96 | 96 | * :file:`ConfigLoader.py`. Move to :file:`IPython/config/configloader.py`. |
|
97 | 97 | |
|
98 | 98 | * :file:`CrashHandler.py`. Move to :file:`IPython/core/crashhandler`. |
|
99 | 99 | |
|
100 | 100 | * :file:`Debugger.py`. Move to :file:`IPython/core/debugger.py`. |
|
101 | 101 | |
|
102 | 102 | * :file:`deep_reload.py`. Move to :file:`IPython/lib/deepreload.py`. |
|
103 | 103 | |
|
104 | 104 | * :file:`demo.py`. Move to :file:`IPython/lib/demo.py`. |
|
105 | 105 | |
|
106 | 106 | * :file:`DPyGetOpt.py`. Move to :mod:`IPython.utils` and replace with newer options parser. |
|
107 | 107 | |
|
108 | 108 | * :file:`dtutils.py`. Move to :file:`IPython.deathrow`. |
|
109 | 109 | |
|
110 | 110 | * :file:`excolors.py`. Move to :file:`IPython.core` or :file:`IPython.config`. |
|
111 | 111 | Maybe move to :mod:`IPython.lib` or :mod:`IPython.python`? |
|
112 | 112 | |
|
113 | 113 | * :file:`FakeModule.py`. Move to :file:`IPython/core/fakemodule.py`. |
|
114 | 114 | |
|
115 | 115 | * :file:`generics.py`. Move to :file:`IPython.python`. |
|
116 | 116 | |
|
117 | 117 | * :file:`genutils.py`. Move to :file:`IPython.utils`. |
|
118 | 118 | |
|
119 | 119 | * :file:`Gnuplot2.py`. Move to :file:`IPython.sandbox`. |
|
120 | 120 | |
|
121 | 121 | * :file:`GnuplotInteractive.py`. Move to :file:`IPython.sandbox`. |
|
122 | 122 | |
|
123 | 123 | * :file:`GnuplotRuntime.py`. Move to :file:`IPython.sandbox`. |
|
124 | 124 | |
|
125 | 125 | * :file:`numutils.py`. Move to :file:`IPython.sandbox`. |
|
126 | 126 | |
|
127 | 127 | * :file:`twshell.py`. Move to :file:`IPython.sandbox`. |
|
128 | 128 | |
|
129 | 129 | * :file:`Extensions`. This needs to be gone through separately. Minimally, |
|
130 | 130 | the package should be renamed to :file:`extensions`. |
|
131 | 131 | |
|
132 | 132 | * :file:`history.py`. Move to :file:`IPython.core`. |
|
133 | 133 | |
|
134 | 134 | * :file:`hooks.py`. Move to :file:`IPython.core`. |
|
135 | 135 | |
|
136 | 136 | * :file:`ipapi.py`. Move to :file:`IPython.core`. |
|
137 | 137 | |
|
138 | 138 | * :file:`iplib.py`. Move to :file:`IPython.core`. |
|
139 | 139 | |
|
140 | 140 | * :file:`ipmaker.py`: Move to :file:`IPython.core`. |
|
141 | 141 | |
|
142 | 142 | * :file:`ipstruct.py`. Move to :file:`IPython.python`. |
|
143 | 143 | |
|
144 | 144 | * :file:`irunner.py`. Move to :file:`IPython.scripts`. ??? |
|
145 | 145 | |
|
146 | 146 | * :file:`Itpl.py`. Move to :file:`deathrow/Itpl.py`. Copy already in |
|
147 | 147 | :file:`IPython.external`. |
|
148 | 148 | |
|
149 | 149 | * :file:`Logger.py`. Move to :file:`IPython/core/logger.py`. |
|
150 | 150 | |
|
151 | 151 | * :file:`macro.py`. Move to :file:`IPython.core`. |
|
152 | 152 | |
|
153 | 153 | * :file:`Magic.py`. Move to :file:`IPython/core/magic.py`. |
|
154 | 154 | |
|
155 | 155 | * :file:`OInspect.py`. Move to :file:`IPython/core/oinspect.py`. |
|
156 | 156 | |
|
157 | 157 | * :file:`OutputTrap.py`. Move to :file:`IPython/core/outputtrap.py`. |
|
158 | 158 | |
|
159 | 159 | * :file:`platutils.py`. Move to :file:`IPython.python`. |
|
160 | 160 | |
|
161 | 161 | * :file:`platutils_dummy.py`. Move to :file:`IPython.python`. |
|
162 | 162 | |
|
163 | 163 | * :file:`platutils_posix.py`. Move to :file:`IPython.python`. |
|
164 | 164 | |
|
165 | 165 | * :file:`platutils_win32.py`. Move to :file:`IPython.python`. |
|
166 | 166 | |
|
167 | 167 | * :file:`prefilter.py`: Move to :file:`IPython.core`. |
|
168 | 168 | |
|
169 | 169 | * :file:`Prompts.py`. Move to :file:`IPython/core/prompts.py` or |
|
170 | 170 | :file:`IPython/frontend/prompts.py`. |
|
171 | 171 | |
|
172 | 172 | * :file:`PyColorize.py`. Replace with pygments? If not, move to |
|
173 | 173 | :file:`IPython/core/pycolorize.py`. Maybe move to :mod:`IPython.lib` or |
|
174 | 174 | :mod:`IPython.python`? |
|
175 | 175 | |
|
176 | 176 | * :file:`Release.py`. Move to ??? or remove? |
|
177 | 177 | |
|
178 | 178 | * :file:`rlineimpl.py`. Move to :file:`IPython.core`. |
|
179 | 179 | |
|
180 | 180 | * :file:`shadowns.py`. Move to :file:`IPython.core`. |
|
181 | 181 | |
|
182 | 182 | * :file:`Shell.py`. Move to :file:`IPython.core.shell.py` or |
|
183 | 183 | :file:`IPython/frontend/shell.py`. |
|
184 | 184 | |
|
185 | 185 | * :file:`shellglobals.py`. Move to :file:`IPython.core`. |
|
186 | 186 | |
|
187 | 187 | * :file:`strdispatch.py`. Move to :file:`IPython.python`. |
|
188 | 188 | |
|
189 | 189 | * :file:`testing`. Good where it is. |
|
190 | 190 | |
|
191 | 191 | * :file:`tests`. Good where it is. |
|
192 | 192 | |
|
193 | 193 | * :file:`tools`. Things in here need to be looked at and moved elsewhere like |
|
194 | 194 | :file:`IPython.python`. |
|
195 | 195 | |
|
196 | 196 | * :file:`UserConfig`. Move to a subdirectory of :file:`IPython.config`. |
|
197 | 197 | |
|
198 | 198 | |
|
199 | 199 | |
|
200 | 200 | |
|
201 | 201 | * :file:`config`. Good where it is! |
|
202 | 202 | |
|
203 | 203 | * :file:`external`. Good where it is! |
|
204 | 204 | |
|
205 | 205 | * :file:`frontend`. Good where it is! |
|
206 | 206 | |
|
207 | 207 | |
|
208 | 208 | |
|
209 | 209 | * :file:`gui`. Eventually this should be moved to a subdir of |
|
210 | 210 | :file:`IPython.frontend`. |
|
211 | 211 | |
|
212 | 212 | |
|
213 | 213 | |
|
214 | 214 | |
|
215 | 215 | |
|
216 | 216 | |
|
217 | 217 | |
|
218 | 218 | |
|
219 | 219 | |
|
220 | 220 | |
|
221 | 221 | |
|
222 | 222 | * :file:`kernel`. Good where it is. |
|
223 | 223 | |
|
224 | 224 | |
|
225 | 225 | |
|
226 | 226 | |
|
227 | 227 | |
|
228 | 228 | |
|
229 | 229 | |
|
230 | 230 | |
|
231 | 231 | |
|
232 | 232 | |
|
233 | 233 | |
|
234 | 234 | |
|
235 | 235 | |
|
236 | 236 | |
|
237 | 237 | |
|
238 | 238 | * :file:`twshell.py`. Move to :file:`IPython.sandbox`. |
|
239 | 239 | |
|
240 | 240 | * :file:`ultraTB.py`. Move to :file:`IPython/core/ultratb.py`. |
|
241 | 241 | |
|
242 |
* :file:`upgrade_dir.py`. Move to :file:`IPython/ |
|
|
242 | * :file:`upgrade_dir.py`. Move to :file:`IPython/utils/upgradedir.py`. | |
|
243 | 243 | |
|
244 | 244 | * :file:`usage.py`. Move to :file:`IPython.core`. |
|
245 | 245 | |
|
246 |
* :file:`wildcard.py`. Move to :file:`IPython. |
|
|
246 | * :file:`wildcard.py`. Move to :file:`IPython.utils`. | |
|
247 | 247 | |
|
248 | 248 | * :file:`winconsole.py`. Move to :file:`IPython.lib`. |
|
249 | 249 | |
|
250 | 250 | Other things |
|
251 | 251 | ============ |
|
252 | 252 | |
|
253 | 253 | When these files are moved around, a number of other things will happen at the same time: |
|
254 | 254 | |
|
255 | 255 | 1. Test files will be created for each module in IPython. Minimally, all |
|
256 | 256 | modules will be imported as a part of the test. This will serve as a |
|
257 | 257 | test of the module reorganization. These tests will be put into new |
|
258 | 258 | :file:`tests` subdirectories that each package will have. |
|
259 | 259 | |
|
260 | 260 | 2. PyFlakes and other code checkers will be run to look for problems. |
|
261 | 261 | |
|
262 | 262 | 3. Modules will be renamed to comply with PEP 8 naming conventions: all |
|
263 | 263 | lowercase and no special characters like ``-`` or ``_``. |
|
264 | 264 | |
|
265 | 265 | 4. Existing tests will be moved to the appropriate :file:`tests` |
|
266 | 266 | subdirectories. |
|
267 | 267 | |
|
268 | 268 | |
|
269 | 269 | |
|
270 | 270 |
General Comments 0
You need to be logged in to leave comments.
Login now