##// END OF EJS Templates
First take at cleaning up extensions.
Brian Granger -
Show More
@@ -1,13 +1,2 b''
1 1 # -*- coding: utf-8 -*-
2 """This directory is meant for special-purpose extensions to IPython.
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 """
2 """This directory is meant for IPython extensions."""
@@ -16,8 +16,8 b''
16 16
17 17 import new
18 18
19 from IPython.core.component import Component
20 from IPython.utils.traitlets import Bool, Any
19 from IPython.config.configurable import Configurable
20 from IPython.utils.traitlets import Bool, Any, Instance
21 21 from IPython.utils.autoattr import auto_attr
22 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 35 """A component to manage the %result, %px and %autopx magics."""
36 36
37 37 active_multiengine_client = Any()
38 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 super(ParalleMagicComponent, self).__init__(parent, name=name, config=config)
41 def __init__(self, shell, config=None):
42 super(ParalleMagicComponent, self).__init__(config=config)
43 self.shell = shell
42 44 self._define_magics()
43 45 # A flag showing if autopx is activated or not
44 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 48 def _define_magics(self):
57 49 """Define the magic functions."""
58 50 self.shell.define_magic('result', self.magic_result)
@@ -204,6 +196,6 b' def load_ipython_extension(ip):'
204 196 """Load the extension in IPython."""
205 197 global _loaded
206 198 if not _loaded:
207 prd = ParalleMagicComponent(ip, name='parallel_magic')
199 prd = ParalleMagicComponent(ip, config=ip.config)
208 200 _loaded = True
209 201
@@ -37,8 +37,8 b' by doing::'
37 37
38 38 from IPython.core.error import TryNext
39 39 from IPython.external import pretty
40 from IPython.core.component import Component
41 from IPython.utils.traitlets import Bool, List
40 from IPython.config.configurable import Configurable
41 from IPython.utils.traitlets import Bool, List, Instance
42 42 from IPython.utils.io import Term
43 43 from IPython.utils.autoattr import auto_attr
44 44 from IPython.utils.importstring import import_item
@@ -51,10 +51,11 b' from IPython.utils.importstring import import_item'
51 51 _loaded = False
52 52
53 53
54 class PrettyResultDisplay(Component):
54 class PrettyResultDisplay(Configurable):
55 55 """A component for pretty printing on steroids."""
56 56
57 57 verbose = Bool(False, config=True)
58 shell = Instance('IPython.core.iplib.InteractiveShell')
58 59
59 60 # A list of (type, func_name), like
60 61 # [(dict, 'my_dict_printer')]
@@ -66,8 +67,9 b' class PrettyResultDisplay(Component):'
66 67 # The final argument can also be a callable
67 68 defaults_for_type_by_name = List(default_value=[], config=True)
68 69
69 def __init__(self, parent, name=None, config=None):
70 super(PrettyResultDisplay, self).__init__(parent, name=name, config=config)
70 def __init__(self, shell, config=None):
71 super(PrettyResultDisplay, self).__init__(config=config)
72 self.shell = shell
71 73 self._setup_defaults()
72 74
73 75 def _setup_defaults(self):
@@ -87,16 +89,6 b' class PrettyResultDisplay(Component):'
87 89 else:
88 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 92 def __call__(self, otherself, arg):
101 93 """Uber-pretty-printing display hook.
102 94
@@ -132,7 +124,7 b' def load_ipython_extension(ip):'
132 124 """Load the extension in IPython as a hook."""
133 125 global _loaded
134 126 if not _loaded:
135 prd = PrettyResultDisplay(ip, name='pretty_result_display')
127 prd = PrettyResultDisplay(ip, config=ip.config)
136 128 ip.set_hook('result_display', prd, priority=99)
137 129 _loaded = True
138 130 return prd
General Comments 0
You need to be logged in to leave comments. Login now