##// END OF EJS Templates
ENH: Allow configurability of the DefaultFormatter and the DisplayHook.
Robert Kern -
Show More
@@ -20,15 +20,14 b' Authors:'
20 20 #-----------------------------------------------------------------------------
21 21
22 22 import __builtin__
23 from pprint import PrettyPrinter
24 pformat = PrettyPrinter().pformat
25 23
26 24 from IPython.config.configurable import Configurable
27 25 from IPython.core import prompts
28 26 import IPython.utils.generics
29 27 import IPython.utils.io
30 from IPython.utils.traitlets import Instance, Int
28 from IPython.utils.traitlets import Instance, List
31 29 from IPython.utils.warn import warn
30 from IPython.core.formatters import DefaultFormatter
32 31
33 32 #-----------------------------------------------------------------------------
34 33 # Main displayhook class
@@ -55,6 +54,16 b' class DisplayHook(Configurable):'
55 54
56 55 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
57 56
57 # The default formatter.
58 default_formatter = Instance('IPython.core.formatters.FormatterABC')
59 def _default_formatter_default(self):
60 # FIXME: backwards compatibility for the InteractiveShell.pprint option?
61 return DefaultFormatter(config=self.config)
62
63 # Any additional FormatterABC instances we use.
64 # FIXME: currently unused.
65 extra_formatters = List(config=True)
66
58 67 # Each call to the In[] prompt raises it by 1, even the first.
59 68 #prompt_count = Int(0)
60 69
@@ -109,7 +118,6 b' class DisplayHook(Configurable):'
109 118 self.output_sep = output_sep
110 119 self.output_sep2 = output_sep2
111 120 self._,self.__,self.___ = '','',''
112 self.pprint_types = map(type,[(),[],{}])
113 121
114 122 # these are deliberately global:
115 123 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
@@ -184,37 +192,23 b' class DisplayHook(Configurable):'
184 192 if self.do_full_cache:
185 193 IPython.utils.io.Term.cout.write(outprompt)
186 194
187 # TODO: Make this method an extension point. The previous implementation
188 # has both a result_display hook as well as a result_display generic
189 # function to customize the repr on a per class basis. We need to rethink
190 # the hooks mechanism before doing this though.
191 195 def compute_result_repr(self, result):
192 196 """Compute and return the repr of the object to be displayed.
193 197
194 198 This method only compute the string form of the repr and should NOT
195 actual print or write that to a stream. This method may also transform
196 the result itself, but the default implementation passes the original
197 through.
199 actual print or write that to a stream.
198 200 """
199 try:
200 if self.shell.pprint:
201 try:
202 result_repr = pformat(result)
203 except:
204 # Work around possible bugs in pformat
205 result_repr = repr(result)
206 if '\n' in result_repr:
207 # So that multi-line strings line up with the left column of
208 # the screen, instead of having the output prompt mess up
209 # their first line.
210 result_repr = '\n' + result_repr
211 else:
212 result_repr = repr(result)
213 except TypeError:
214 # This happens when result.__repr__ doesn't return a string,
215 # such as when it returns None.
216 result_repr = '\n'
217 return result, result_repr
201 result_repr = self.default_formatter(result)
202 if '\n' in result_repr:
203 # So that multi-line strings line up with the left column of
204 # the screen, instead of having the output prompt mess up
205 # their first line.
206 outprompt = str(self.prompt_out)
207 if outprompt and not outprompt.endswith('\n'):
208 # But avoid extraneous empty lines.
209 result_repr = '\n' + result_repr
210
211 return result_repr
218 212
219 213 def write_result_repr(self, result_repr):
220 214 # We want to print because we want to always make sure we have a
@@ -271,7 +265,7 b' class DisplayHook(Configurable):'
271 265 if result is not None and not self.quiet():
272 266 self.start_displayhook()
273 267 self.write_output_prompt()
274 result, result_repr = self.compute_result_repr(result)
268 result_repr = self.compute_result_repr(result)
275 269 self.write_result_repr(result_repr)
276 270 self.update_user_ns(result)
277 271 self.log_output(result)
@@ -25,32 +25,32 b' class DefaultFormatter(Configurable):'
25 25 format = Str('text')
26 26
27 27 # Whether to pretty-print or not.
28 pprint = Bool(True)
28 pprint = Bool(True, config=True)
29 29
30 30 # Whether to be verbose or not.
31 verbose = Bool(False)
31 verbose = Bool(False, config=True)
32 32
33 33 # The maximum width.
34 max_width = Int(79)
34 max_width = Int(79, config=True)
35 35
36 36 # The newline character.
37 newline = Str('\n')
37 newline = Str('\n', config=True)
38 38
39 39 # The singleton prettyprinters.
40 40 # Maps the IDs of the builtin singleton objects to the format functions.
41 singleton_pprinters = Dict()
41 singleton_pprinters = Dict(config=True)
42 42 def _singleton_pprinters_default(self):
43 43 return pretty._singleton_pprinters.copy()
44 44
45 45 # The type-specific prettyprinters.
46 46 # Map type objects to the format functions.
47 type_pprinters = Dict()
47 type_pprinters = Dict(config=True)
48 48 def _type_pprinters_default(self):
49 49 return pretty._type_pprinters.copy()
50 50
51 51 # The deferred-import type-specific prettyprinters.
52 52 # Map (modulename, classname) pairs to the format functions.
53 deferred_pprinters = Dict()
53 deferred_pprinters = Dict(config=True)
54 54 def _deferred_pprinters_default(self):
55 55 return pretty._deferred_type_pprinters.copy()
56 56
@@ -60,11 +60,10 b' class DefaultFormatter(Configurable):'
60 60 """ Format the object.
61 61 """
62 62 if not self.pprint:
63 r = repr(obj)
64 if r is None:
65 # It can happen.
66 r = ''
67 return r
63 try:
64 return repr(obj)
65 except TypeError:
66 return ''
68 67 else:
69 68 stream = StringIO()
70 69 printer = pretty.RepresentationPrinter(stream, self.verbose,
@@ -475,6 +475,7 b' class InteractiveShell(Configurable, Magic):'
475 475 def init_displayhook(self):
476 476 # Initialize displayhook, set in/out prompts and printing system
477 477 self.displayhook = self.displayhook_class(
478 config=self.config,
478 479 shell=self,
479 480 cache_size=self.cache_size,
480 481 input_sep = self.separate_in,
General Comments 0
You need to be logged in to leave comments. Login now