Show More
@@ -1,162 +1,169 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Displayhook formatters. |
|
2 | """Displayhook formatters. | |
3 |
|
3 | |||
4 | The DefaultFormatter is always present and may be configured from the |
|
4 | The DefaultFormatter is always present and may be configured from the | |
5 | ipython_config.py file. For example, to add a pretty-printer for a numpy.dtype |
|
5 | ipython_config.py file. For example, to add a pretty-printer for a numpy.dtype | |
6 | object:: |
|
6 | object:: | |
7 |
|
7 | |||
8 | def dtype_pprinter(obj, p, cycle): |
|
8 | def dtype_pprinter(obj, p, cycle): | |
9 | if cycle: |
|
9 | if cycle: | |
10 | return p.text('dtype(...)') |
|
10 | return p.text('dtype(...)') | |
11 | if hasattr(obj, 'fields'): |
|
11 | if hasattr(obj, 'fields'): | |
12 | if obj.fields is None: |
|
12 | if obj.fields is None: | |
13 | p.text(repr(obj)) |
|
13 | p.text(repr(obj)) | |
14 | else: |
|
14 | else: | |
15 | p.begin_group(7, 'dtype([') |
|
15 | p.begin_group(7, 'dtype([') | |
16 | for i, field in enumerate(obj.descr): |
|
16 | for i, field in enumerate(obj.descr): | |
17 | if i > 0: |
|
17 | if i > 0: | |
18 | p.text(',') |
|
18 | p.text(',') | |
19 | p.breakable() |
|
19 | p.breakable() | |
20 | p.pretty(field) |
|
20 | p.pretty(field) | |
21 | p.end_group(7, '])') |
|
21 | p.end_group(7, '])') | |
22 |
|
22 | |||
23 | c.DefaultFormatter.deferred_pprinters = { |
|
23 | c.DefaultFormatter.deferred_pprinters = { | |
24 | ('numpy', 'dtype'): dtype_pprinter, |
|
24 | ('numpy', 'dtype'): dtype_pprinter, | |
25 | } |
|
25 | } | |
26 |
|
26 | |||
27 | The deferred_pprinters dictionary is the preferred way to configure these |
|
27 | The deferred_pprinters dictionary is the preferred way to configure these | |
28 | pretty-printers. This allows you to define the pretty-printer without needing to |
|
28 | pretty-printers. This allows you to define the pretty-printer without needing to | |
29 | import the type itself. The dictionary maps (modulename, typename) pairs to |
|
29 | import the type itself. The dictionary maps (modulename, typename) pairs to | |
30 | a function. |
|
30 | a function. | |
31 |
|
31 | |||
32 | See the `IPython.external.pretty` documentation for how to write |
|
32 | See the `IPython.external.pretty` documentation for how to write | |
33 | pretty-printer functions. |
|
33 | pretty-printer functions. | |
34 | """ |
|
34 | """ | |
|
35 | #----------------------------------------------------------------------------- | |||
|
36 | # Copyright (c) 2010, IPython Development Team. | |||
|
37 | # | |||
|
38 | # Distributed under the terms of the Modified BSD License. | |||
|
39 | # | |||
|
40 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
41 | #----------------------------------------------------------------------------- | |||
35 |
|
42 | |||
36 | # Stdlib imports |
|
43 | # Stdlib imports | |
37 | import abc |
|
44 | import abc | |
38 | from cStringIO import StringIO |
|
45 | from cStringIO import StringIO | |
39 |
|
46 | |||
40 | # Our own imports |
|
47 | # Our own imports | |
41 | from IPython.config.configurable import Configurable |
|
48 | from IPython.config.configurable import Configurable | |
42 | from IPython.external import pretty |
|
49 | from IPython.external import pretty | |
43 | from IPython.utils.traitlets import Bool, Dict, Int, Str |
|
50 | from IPython.utils.traitlets import Bool, Dict, Int, Str | |
44 |
|
51 | |||
45 |
|
52 | |||
46 | #----------------------------------------------------------------------------- |
|
53 | #----------------------------------------------------------------------------- | |
47 | # Classes and functions |
|
54 | # Classes and functions | |
48 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
49 |
|
56 | |||
50 | class DefaultFormatter(Configurable): |
|
57 | class DefaultFormatter(Configurable): | |
51 | """ The default pretty-printer. |
|
58 | """ The default pretty-printer. | |
52 | """ |
|
59 | """ | |
53 |
|
60 | |||
54 | # The ID of the formatter. |
|
61 | # The ID of the formatter. | |
55 | id = Str('default') |
|
62 | id = Str('default') | |
56 |
|
63 | |||
57 | # The kind of data returned. |
|
64 | # The kind of data returned. | |
58 | # This is often, but not always a MIME type. |
|
65 | # This is often, but not always a MIME type. | |
59 | format = Str('text/plain') |
|
66 | format = Str('text/plain') | |
60 |
|
67 | |||
61 | # Whether to pretty-print or not. |
|
68 | # Whether to pretty-print or not. | |
62 | pprint = Bool(True, config=True) |
|
69 | pprint = Bool(True, config=True) | |
63 |
|
70 | |||
64 | # Whether to be verbose or not. |
|
71 | # Whether to be verbose or not. | |
65 | verbose = Bool(False, config=True) |
|
72 | verbose = Bool(False, config=True) | |
66 |
|
73 | |||
67 | # The maximum width. |
|
74 | # The maximum width. | |
68 | max_width = Int(79, config=True) |
|
75 | max_width = Int(79, config=True) | |
69 |
|
76 | |||
70 | # The newline character. |
|
77 | # The newline character. | |
71 | newline = Str('\n', config=True) |
|
78 | newline = Str('\n', config=True) | |
72 |
|
79 | |||
73 | # The singleton prettyprinters. |
|
80 | # The singleton prettyprinters. | |
74 | # Maps the IDs of the builtin singleton objects to the format functions. |
|
81 | # Maps the IDs of the builtin singleton objects to the format functions. | |
75 | singleton_pprinters = Dict(config=True) |
|
82 | singleton_pprinters = Dict(config=True) | |
76 | def _singleton_pprinters_default(self): |
|
83 | def _singleton_pprinters_default(self): | |
77 | return pretty._singleton_pprinters.copy() |
|
84 | return pretty._singleton_pprinters.copy() | |
78 |
|
85 | |||
79 | # The type-specific prettyprinters. |
|
86 | # The type-specific prettyprinters. | |
80 | # Map type objects to the format functions. |
|
87 | # Map type objects to the format functions. | |
81 | type_pprinters = Dict(config=True) |
|
88 | type_pprinters = Dict(config=True) | |
82 | def _type_pprinters_default(self): |
|
89 | def _type_pprinters_default(self): | |
83 | return pretty._type_pprinters.copy() |
|
90 | return pretty._type_pprinters.copy() | |
84 |
|
91 | |||
85 | # The deferred-import type-specific prettyprinters. |
|
92 | # The deferred-import type-specific prettyprinters. | |
86 | # Map (modulename, classname) pairs to the format functions. |
|
93 | # Map (modulename, classname) pairs to the format functions. | |
87 | deferred_pprinters = Dict(config=True) |
|
94 | deferred_pprinters = Dict(config=True) | |
88 | def _deferred_pprinters_default(self): |
|
95 | def _deferred_pprinters_default(self): | |
89 | return pretty._deferred_type_pprinters.copy() |
|
96 | return pretty._deferred_type_pprinters.copy() | |
90 |
|
97 | |||
91 | #### FormatterABC interface #### |
|
98 | #### FormatterABC interface #### | |
92 |
|
99 | |||
93 | def __call__(self, obj): |
|
100 | def __call__(self, obj): | |
94 | """ Format the object. |
|
101 | """ Format the object. | |
95 | """ |
|
102 | """ | |
96 | if not self.pprint: |
|
103 | if not self.pprint: | |
97 | try: |
|
104 | try: | |
98 | return repr(obj) |
|
105 | return repr(obj) | |
99 | except TypeError: |
|
106 | except TypeError: | |
100 | return '' |
|
107 | return '' | |
101 | else: |
|
108 | else: | |
102 | stream = StringIO() |
|
109 | stream = StringIO() | |
103 | printer = pretty.RepresentationPrinter(stream, self.verbose, |
|
110 | printer = pretty.RepresentationPrinter(stream, self.verbose, | |
104 | self.max_width, self.newline, |
|
111 | self.max_width, self.newline, | |
105 | singleton_pprinters=self.singleton_pprinters, |
|
112 | singleton_pprinters=self.singleton_pprinters, | |
106 | type_pprinters=self.type_pprinters, |
|
113 | type_pprinters=self.type_pprinters, | |
107 | deferred_pprinters=self.deferred_pprinters) |
|
114 | deferred_pprinters=self.deferred_pprinters) | |
108 | printer.pretty(obj) |
|
115 | printer.pretty(obj) | |
109 | printer.flush() |
|
116 | printer.flush() | |
110 | return stream.getvalue() |
|
117 | return stream.getvalue() | |
111 |
|
118 | |||
112 |
|
119 | |||
113 | #### DefaultFormatter interface #### |
|
120 | #### DefaultFormatter interface #### | |
114 |
|
121 | |||
115 | def for_type(self, typ, func): |
|
122 | def for_type(self, typ, func): | |
116 | """ |
|
123 | """ | |
117 | Add a pretty printer for a given type. |
|
124 | Add a pretty printer for a given type. | |
118 | """ |
|
125 | """ | |
119 | oldfunc = self.type_pprinters.get(typ, None) |
|
126 | oldfunc = self.type_pprinters.get(typ, None) | |
120 | if func is not None: |
|
127 | if func is not None: | |
121 | # To support easy restoration of old pprinters, we need to ignore |
|
128 | # To support easy restoration of old pprinters, we need to ignore | |
122 | # Nones. |
|
129 | # Nones. | |
123 | self.type_pprinters[typ] = func |
|
130 | self.type_pprinters[typ] = func | |
124 | return oldfunc |
|
131 | return oldfunc | |
125 |
|
132 | |||
126 | def for_type_by_name(self, type_module, type_name, func): |
|
133 | def for_type_by_name(self, type_module, type_name, func): | |
127 | """ |
|
134 | """ | |
128 | Add a pretty printer for a type specified by the module and name of |
|
135 | Add a pretty printer for a type specified by the module and name of | |
129 | a type rather than the type object itself. |
|
136 | a type rather than the type object itself. | |
130 | """ |
|
137 | """ | |
131 | key = (type_module, type_name) |
|
138 | key = (type_module, type_name) | |
132 | oldfunc = self.deferred_pprinters.get(key, None) |
|
139 | oldfunc = self.deferred_pprinters.get(key, None) | |
133 | if func is not None: |
|
140 | if func is not None: | |
134 | # To support easy restoration of old pprinters, we need to ignore |
|
141 | # To support easy restoration of old pprinters, we need to ignore | |
135 | # Nones. |
|
142 | # Nones. | |
136 | self.deferred_pprinters[key] = func |
|
143 | self.deferred_pprinters[key] = func | |
137 | return oldfunc |
|
144 | return oldfunc | |
138 |
|
145 | |||
139 |
|
146 | |||
140 | class FormatterABC(object): |
|
147 | class FormatterABC(object): | |
141 | """ Abstract base class for Formatters. |
|
148 | """ Abstract base class for Formatters. | |
142 | """ |
|
149 | """ | |
143 | __metaclass__ = abc.ABCMeta |
|
150 | __metaclass__ = abc.ABCMeta | |
144 |
|
151 | |||
145 | # The ID of the formatter. |
|
152 | # The ID of the formatter. | |
146 | id = 'abstract' |
|
153 | id = 'abstract' | |
147 |
|
154 | |||
148 | # The kind of data returned. |
|
155 | # The kind of data returned. | |
149 | format = 'text/plain' |
|
156 | format = 'text/plain' | |
150 |
|
157 | |||
151 | @abc.abstractmethod |
|
158 | @abc.abstractmethod | |
152 | def __call__(self, obj): |
|
159 | def __call__(self, obj): | |
153 | """ Return a JSONable representation of the object. |
|
160 | """ Return a JSONable representation of the object. | |
154 |
|
161 | |||
155 | If the object cannot be formatted by this formatter, then return None |
|
162 | If the object cannot be formatted by this formatter, then return None | |
156 | """ |
|
163 | """ | |
157 | try: |
|
164 | try: | |
158 | return repr(obj) |
|
165 | return repr(obj) | |
159 | except TypeError: |
|
166 | except TypeError: | |
160 | return None |
|
167 | return None | |
161 |
|
168 | |||
162 | FormatterABC.register(DefaultFormatter) |
|
169 | FormatterABC.register(DefaultFormatter) |
General Comments 0
You need to be logged in to leave comments.
Login now