##// END OF EJS Templates
First take at cleaning up extensions.
Brian Granger -
Show More
@@ -1,13 +1,2 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """This directory is meant for special-purpose extensions to IPython.
2 """This directory is meant for IPython extensions."""
3
4 This can include things which alter the syntax processing stage (see
5 PhysicalQ_Input for an example of how to do this).
6
7 Any file located here can be called with an 'execfile =' option as
8
9 execfile = extensions/filename.py
10
11 since the IPython directory itself is already part of the search path for
12 files listed as 'execfile ='.
13 """
@@ -16,8 +16,8 b''
16
16
17 import new
17 import new
18
18
19 from IPython.core.component import Component
19 from IPython.config.configurable import Configurable
20 from IPython.utils.traitlets import Bool, Any
20 from IPython.utils.traitlets import Bool, Any, Instance
21 from IPython.utils.autoattr import auto_attr
21 from IPython.utils.autoattr import auto_attr
22 from IPython.testing import decorators as testdec
22 from IPython.testing import decorators as testdec
23
23
@@ -31,28 +31,20 b' Use activate() on a MultiEngineClient object to activate it for magics.'
31 """
31 """
32
32
33
33
34 class ParalleMagicComponent(Component):
34 class ParalleMagicComponent(Configurable):
35 """A component to manage the %result, %px and %autopx magics."""
35 """A component to manage the %result, %px and %autopx magics."""
36
36
37 active_multiengine_client = Any()
37 active_multiengine_client = Any()
38 verbose = Bool(False, config=True)
38 verbose = Bool(False, config=True)
39 shell = Instance('IPython.core.iplib.InteractiveShell')
39
40
40 def __init__(self, parent, name=None, config=None):
41 def __init__(self, shell, config=None):
41 super(ParalleMagicComponent, self).__init__(parent, name=name, config=config)
42 super(ParalleMagicComponent, self).__init__(config=config)
43 self.shell = shell
42 self._define_magics()
44 self._define_magics()
43 # A flag showing if autopx is activated or not
45 # A flag showing if autopx is activated or not
44 self.autopx = False
46 self.autopx = False
45
47
46 # Access other components like this rather than by a regular attribute.
47 # This won't lookup the InteractiveShell object until it is used and
48 # then it is cached. This is both efficient and couples this class
49 # more loosely to InteractiveShell.
50 @auto_attr
51 def shell(self):
52 return Component.get_instances(
53 root=self.root,
54 klass='IPython.core.iplib.InteractiveShell')[0]
55
56 def _define_magics(self):
48 def _define_magics(self):
57 """Define the magic functions."""
49 """Define the magic functions."""
58 self.shell.define_magic('result', self.magic_result)
50 self.shell.define_magic('result', self.magic_result)
@@ -204,6 +196,6 b' def load_ipython_extension(ip):'
204 """Load the extension in IPython."""
196 """Load the extension in IPython."""
205 global _loaded
197 global _loaded
206 if not _loaded:
198 if not _loaded:
207 prd = ParalleMagicComponent(ip, name='parallel_magic')
199 prd = ParalleMagicComponent(ip, config=ip.config)
208 _loaded = True
200 _loaded = True
209
201
@@ -37,8 +37,8 b' by doing::'
37
37
38 from IPython.core.error import TryNext
38 from IPython.core.error import TryNext
39 from IPython.external import pretty
39 from IPython.external import pretty
40 from IPython.core.component import Component
40 from IPython.config.configurable import Configurable
41 from IPython.utils.traitlets import Bool, List
41 from IPython.utils.traitlets import Bool, List, Instance
42 from IPython.utils.io import Term
42 from IPython.utils.io import Term
43 from IPython.utils.autoattr import auto_attr
43 from IPython.utils.autoattr import auto_attr
44 from IPython.utils.importstring import import_item
44 from IPython.utils.importstring import import_item
@@ -51,10 +51,11 b' from IPython.utils.importstring import import_item'
51 _loaded = False
51 _loaded = False
52
52
53
53
54 class PrettyResultDisplay(Component):
54 class PrettyResultDisplay(Configurable):
55 """A component for pretty printing on steroids."""
55 """A component for pretty printing on steroids."""
56
56
57 verbose = Bool(False, config=True)
57 verbose = Bool(False, config=True)
58 shell = Instance('IPython.core.iplib.InteractiveShell')
58
59
59 # A list of (type, func_name), like
60 # A list of (type, func_name), like
60 # [(dict, 'my_dict_printer')]
61 # [(dict, 'my_dict_printer')]
@@ -66,8 +67,9 b' class PrettyResultDisplay(Component):'
66 # The final argument can also be a callable
67 # The final argument can also be a callable
67 defaults_for_type_by_name = List(default_value=[], config=True)
68 defaults_for_type_by_name = List(default_value=[], config=True)
68
69
69 def __init__(self, parent, name=None, config=None):
70 def __init__(self, shell, config=None):
70 super(PrettyResultDisplay, self).__init__(parent, name=name, config=config)
71 super(PrettyResultDisplay, self).__init__(config=config)
72 self.shell = shell
71 self._setup_defaults()
73 self._setup_defaults()
72
74
73 def _setup_defaults(self):
75 def _setup_defaults(self):
@@ -87,16 +89,6 b' class PrettyResultDisplay(Component):'
87 else:
89 else:
88 raise TypeError('func_name must be a str or callable, got: %r' % func_name)
90 raise TypeError('func_name must be a str or callable, got: %r' % func_name)
89
91
90 # Access other components like this rather than by a regular attribute.
91 # This won't lookup the InteractiveShell object until it is used and
92 # then it is cached. This is both efficient and couples this class
93 # more loosely to InteractiveShell.
94 @auto_attr
95 def shell(self):
96 return Component.get_instances(
97 root=self.root,
98 klass='IPython.core.iplib.InteractiveShell')[0]
99
100 def __call__(self, otherself, arg):
92 def __call__(self, otherself, arg):
101 """Uber-pretty-printing display hook.
93 """Uber-pretty-printing display hook.
102
94
@@ -132,7 +124,7 b' def load_ipython_extension(ip):'
132 """Load the extension in IPython as a hook."""
124 """Load the extension in IPython as a hook."""
133 global _loaded
125 global _loaded
134 if not _loaded:
126 if not _loaded:
135 prd = PrettyResultDisplay(ip, name='pretty_result_display')
127 prd = PrettyResultDisplay(ip, config=ip.config)
136 ip.set_hook('result_display', prd, priority=99)
128 ip.set_hook('result_display', prd, priority=99)
137 _loaded = True
129 _loaded = True
138 return prd
130 return prd
General Comments 0
You need to be logged in to leave comments. Login now