##// END OF EJS Templates
Continue restructuring info system, move some standalone code to utils.
Fernando Perez -
Show More
@@ -61,7 +61,7 b' from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError'
61 from IPython.utils.process import system, getoutput
61 from IPython.utils.process import system, getoutput
62 from IPython.utils.strdispatch import StrDispatch
62 from IPython.utils.strdispatch import StrDispatch
63 from IPython.utils.syspathcontext import prepended_to_syspath
63 from IPython.utils.syspathcontext import prepended_to_syspath
64 from IPython.utils.text import num_ini_spaces
64 from IPython.utils.text import num_ini_spaces, format_screen
65 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
65 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
66 List, Unicode, Instance, Type)
66 List, Unicode, Instance, Type)
67 from IPython.utils.warn import warn, error, fatal
67 from IPython.utils.warn import warn, error, fatal
@@ -1036,7 +1036,15 b' class InteractiveShell(Configurable, Magic):'
1036
1036
1037 Has special code to detect magic functions.
1037 Has special code to detect magic functions.
1038 """
1038 """
1039 oname = oname.strip()
1039 #oname = oname.strip()
1040 #print '1- oname: <%r>' % oname # dbg
1041 try:
1042 oname = oname.strip().encode('ascii')
1043 #print '2- oname: <%r>' % oname # dbg
1044 except UnicodeEncodeError:
1045 print 'Python identifiers can only contain ascii characters.'
1046 return dict(found=False)
1047
1040 alias_ns = None
1048 alias_ns = None
1041 if namespaces is None:
1049 if namespaces is None:
1042 # Namespaces to search in:
1050 # Namespaces to search in:
@@ -1105,32 +1113,13 b' class InteractiveShell(Configurable, Magic):'
1105 obj = eval(oname_head)
1113 obj = eval(oname_head)
1106 found = True
1114 found = True
1107 ospace = 'Interactive'
1115 ospace = 'Interactive'
1108
1116
1109 return {'found':found, 'obj':obj, 'namespace':ospace,
1117 return {'found':found, 'obj':obj, 'namespace':ospace,
1110 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1118 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1111
1119
1112 def _inspect(self,meth,oname,namespaces=None,**kw):
1120 def _ofind_property(self, oname, info):
1113 """Generic interface to the inspector system.
1121 """Second part of object finding, to look for property details."""
1114
1115 This function is meant to be called by pdef, pdoc & friends."""
1116
1117 #oname = oname.strip()
1118 #print '1- oname: <%r>' % oname # dbg
1119 try:
1120 oname = oname.strip().encode('ascii')
1121 #print '2- oname: <%r>' % oname # dbg
1122 except UnicodeEncodeError:
1123 print 'Python identifiers can only contain ascii characters.'
1124 return 'not found'
1125
1126 info = Struct(self._ofind(oname, namespaces))
1127
1128 if info.found:
1122 if info.found:
1129 try:
1130 IPython.utils.generics.inspect_object(info.obj)
1131 return
1132 except TryNext:
1133 pass
1134 # Get the docstring of the class property if it exists.
1123 # Get the docstring of the class property if it exists.
1135 path = oname.split('.')
1124 path = oname.split('.')
1136 root = '.'.join(path[:-1])
1125 root = '.'.join(path[:-1])
@@ -1146,18 +1135,36 b' class InteractiveShell(Configurable, Magic):'
1146 info = Struct(self._ofind(oname))
1135 info = Struct(self._ofind(oname))
1147 except AttributeError: pass
1136 except AttributeError: pass
1148 except AttributeError: pass
1137 except AttributeError: pass
1149
1138
1150 pmethod = getattr(self.inspector,meth)
1139 # We return either the new info or the unmodified input if the object
1151 formatter = info.ismagic and self.format_screen or None
1140 # hadn't been found
1141 return info
1142
1143 def _object_find(self, oname, namespaces=None):
1144 """Find an object and return a struct with info about it."""
1145 inf = Struct(self._ofind(oname, namespaces))
1146 return Struct(self._ofind_property(oname, inf))
1147
1148 def _inspect(self, meth, oname, namespaces=None, **kw):
1149 """Generic interface to the inspector system.
1150
1151 This function is meant to be called by pdef, pdoc & friends."""
1152 info = self._object_find(oname)
1153 if info.found:
1154 pmethod = getattr(self.inspector, meth)
1155 formatter = format_screen if info.ismagic else None
1152 if meth == 'pdoc':
1156 if meth == 'pdoc':
1153 pmethod(info.obj,oname,formatter)
1157 pmethod(info.obj, oname, formatter)
1154 elif meth == 'pinfo':
1158 elif meth == 'pinfo':
1155 pmethod(info.obj,oname,formatter,info,**kw)
1159 pmethod(info.obj, oname, formatter, info, **kw)
1156 else:
1160 else:
1157 pmethod(info.obj,oname)
1161 pmethod(info.obj, oname)
1158 else:
1162 else:
1159 print 'Object `%s` not found.' % oname
1163 print 'Object `%s` not found.' % oname
1160 return 'not found' # so callers can take other action
1164 return 'not found' # so callers can take other action
1165
1166 def object_inspect(self, oname):
1167 info = self._object_find(oname)
1161
1168
1162 #-------------------------------------------------------------------------
1169 #-------------------------------------------------------------------------
1163 # Things related to history management
1170 # Things related to history management
@@ -62,7 +62,7 b' import IPython.utils.io'
62 from IPython.utils.path import get_py_filename
62 from IPython.utils.path import get_py_filename
63 from IPython.utils.process import arg_split, abbrev_cwd
63 from IPython.utils.process import arg_split, abbrev_cwd
64 from IPython.utils.terminal import set_term_title
64 from IPython.utils.terminal import set_term_title
65 from IPython.utils.text import LSString, SList, StringTypes
65 from IPython.utils.text import LSString, SList, StringTypes, format_screen
66 from IPython.utils.timing import clock, clock2
66 from IPython.utils.timing import clock, clock2
67 from IPython.utils.warn import warn, error
67 from IPython.utils.warn import warn, error
68 from IPython.utils.ipstruct import Struct
68 from IPython.utils.ipstruct import Struct
@@ -240,15 +240,6 b' python-profiler package from non-free.""")'
240 strng = newline_re.sub(r'\\textbackslash{}n',strng)
240 strng = newline_re.sub(r'\\textbackslash{}n',strng)
241 return strng
241 return strng
242
242
243 def format_screen(self,strng):
244 """Format a string for screen printing.
245
246 This removes some latex-type format codes."""
247 # Paragraph continue
248 par_re = re.compile(r'\\$',re.MULTILINE)
249 strng = par_re.sub('',strng)
250 return strng
251
252 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
243 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
253 """Parse options passed to an argument string.
244 """Parse options passed to an argument string.
254
245
@@ -387,7 +378,7 b' python-profiler package from non-free.""")'
387 print self.format_latex(magic_docs)
378 print self.format_latex(magic_docs)
388 return
379 return
389 else:
380 else:
390 magic_docs = self.format_screen(magic_docs)
381 magic_docs = format_screen(magic_docs)
391 if mode == 'brief':
382 if mode == 'brief':
392 return magic_docs
383 return magic_docs
393
384
@@ -585,8 +576,8 b' Currently the magic system has the following functions:\\n"""'
585 if "*" in oname:
576 if "*" in oname:
586 self.magic_psearch(oname)
577 self.magic_psearch(oname)
587 else:
578 else:
588 self._inspect('pinfo', oname, detail_level=detail_level,
579 self.shell._inspect('pinfo', oname, detail_level=detail_level,
589 namespaces=namespaces)
580 namespaces=namespaces)
590
581
591 def magic_pdef(self, parameter_s='', namespaces=None):
582 def magic_pdef(self, parameter_s='', namespaces=None):
592 """Print the definition header for any callable object.
583 """Print the definition header for any callable object.
@@ -200,7 +200,7 b' class Inspector:'
200 self.str_detail_level = str_detail_level
200 self.str_detail_level = str_detail_level
201 self.set_active_scheme(scheme)
201 self.set_active_scheme(scheme)
202
202
203 def __getdef(self,obj,oname=''):
203 def _getdef(self,obj,oname=''):
204 """Return the definition header for any callable object.
204 """Return the definition header for any callable object.
205
205
206 If any exception is generated, None is returned instead and the
206 If any exception is generated, None is returned instead and the
@@ -245,7 +245,7 b' class Inspector:'
245 elif type(obj) is types.InstanceType:
245 elif type(obj) is types.InstanceType:
246 obj = obj.__call__
246 obj = obj.__call__
247
247
248 output = self.__getdef(obj,oname)
248 output = self._getdef(obj,oname)
249 if output is None:
249 if output is None:
250 self.noinfo('definition header',oname)
250 self.noinfo('definition header',oname)
251 else:
251 else:
@@ -438,7 +438,7 b' class Inspector:'
438 binary_file = True
438 binary_file = True
439
439
440 # reconstruct the function definition and print it:
440 # reconstruct the function definition and print it:
441 defln = self.__getdef(obj,oname)
441 defln = self._getdef(obj,oname)
442 if defln:
442 if defln:
443 out.write(header('Definition:\t')+self.format(defln))
443 out.write(header('Definition:\t')+self.format(defln))
444
444
@@ -478,7 +478,7 b' class Inspector:'
478 except AttributeError:
478 except AttributeError:
479 init_def = init_ds = None
479 init_def = init_ds = None
480 else:
480 else:
481 init_def = self.__getdef(obj_init,oname)
481 init_def = self._getdef(obj_init,oname)
482 init_ds = getdoc(obj_init)
482 init_ds = getdoc(obj_init)
483 # Skip Python's auto-generated docstrings
483 # Skip Python's auto-generated docstrings
484 if init_ds and \
484 if init_ds and \
@@ -532,7 +532,7 b' class Inspector:'
532 # Call form docstring for callable instances
532 # Call form docstring for callable instances
533 if hasattr(obj,'__call__'):
533 if hasattr(obj,'__call__'):
534 #out.writeln(header('Callable:\t')+'Yes')
534 #out.writeln(header('Callable:\t')+'Yes')
535 call_def = self.__getdef(obj.__call__,oname)
535 call_def = self._getdef(obj.__call__,oname)
536 #if call_def is None:
536 #if call_def is None:
537 # out.writeln(header('Call def:\t')+
537 # out.writeln(header('Call def:\t')+
538 # 'Calling definition not available.')
538 # 'Calling definition not available.')
@@ -487,3 +487,13 b' def num_ini_spaces(strng):'
487 else:
487 else:
488 return 0
488 return 0
489
489
490
491 def format_screen(strng):
492 """Format a string for screen printing.
493
494 This removes some latex-type format codes."""
495 # Paragraph continue
496 par_re = re.compile(r'\\$',re.MULTILINE)
497 strng = par_re.sub('',strng)
498 return strng
499
General Comments 0
You need to be logged in to leave comments. Login now