From 60cab7315d37b8c11ff30b734ecd5bc35cf58ca0 2010-08-10 21:05:02 From: Brian Granger Date: 2010-08-10 21:05:02 Subject: [PATCH] First take at cleaning up extensions. --- diff --git a/IPython/extensions/__init__.py b/IPython/extensions/__init__.py index 22e892d..db7f79f 100644 --- a/IPython/extensions/__init__.py +++ b/IPython/extensions/__init__.py @@ -1,13 +1,2 @@ # -*- coding: utf-8 -*- -"""This directory is meant for special-purpose extensions to IPython. - -This can include things which alter the syntax processing stage (see -PhysicalQ_Input for an example of how to do this). - -Any file located here can be called with an 'execfile =' option as - - execfile = extensions/filename.py - -since the IPython directory itself is already part of the search path for -files listed as 'execfile ='. -""" +"""This directory is meant for IPython extensions.""" diff --git a/IPython/extensions/parallelmagic.py b/IPython/extensions/parallelmagic.py index cd3b48f..127e212 100755 --- a/IPython/extensions/parallelmagic.py +++ b/IPython/extensions/parallelmagic.py @@ -16,8 +16,8 @@ import new -from IPython.core.component import Component -from IPython.utils.traitlets import Bool, Any +from IPython.config.configurable import Configurable +from IPython.utils.traitlets import Bool, Any, Instance from IPython.utils.autoattr import auto_attr from IPython.testing import decorators as testdec @@ -31,28 +31,20 @@ Use activate() on a MultiEngineClient object to activate it for magics. """ -class ParalleMagicComponent(Component): +class ParalleMagicComponent(Configurable): """A component to manage the %result, %px and %autopx magics.""" active_multiengine_client = Any() verbose = Bool(False, config=True) + shell = Instance('IPython.core.iplib.InteractiveShell') - def __init__(self, parent, name=None, config=None): - super(ParalleMagicComponent, self).__init__(parent, name=name, config=config) + def __init__(self, shell, config=None): + super(ParalleMagicComponent, self).__init__(config=config) + self.shell = shell self._define_magics() # A flag showing if autopx is activated or not self.autopx = False - # Access other components like this rather than by a regular attribute. - # This won't lookup the InteractiveShell object until it is used and - # then it is cached. This is both efficient and couples this class - # more loosely to InteractiveShell. - @auto_attr - def shell(self): - return Component.get_instances( - root=self.root, - klass='IPython.core.iplib.InteractiveShell')[0] - def _define_magics(self): """Define the magic functions.""" self.shell.define_magic('result', self.magic_result) @@ -204,6 +196,6 @@ def load_ipython_extension(ip): """Load the extension in IPython.""" global _loaded if not _loaded: - prd = ParalleMagicComponent(ip, name='parallel_magic') + prd = ParalleMagicComponent(ip, config=ip.config) _loaded = True diff --git a/IPython/extensions/pretty.py b/IPython/extensions/pretty.py index 9484757..52501d1 100644 --- a/IPython/extensions/pretty.py +++ b/IPython/extensions/pretty.py @@ -37,8 +37,8 @@ by doing:: from IPython.core.error import TryNext from IPython.external import pretty -from IPython.core.component import Component -from IPython.utils.traitlets import Bool, List +from IPython.config.configurable import Configurable +from IPython.utils.traitlets import Bool, List, Instance from IPython.utils.io import Term from IPython.utils.autoattr import auto_attr from IPython.utils.importstring import import_item @@ -51,10 +51,11 @@ from IPython.utils.importstring import import_item _loaded = False -class PrettyResultDisplay(Component): +class PrettyResultDisplay(Configurable): """A component for pretty printing on steroids.""" verbose = Bool(False, config=True) + shell = Instance('IPython.core.iplib.InteractiveShell') # A list of (type, func_name), like # [(dict, 'my_dict_printer')] @@ -66,8 +67,9 @@ class PrettyResultDisplay(Component): # The final argument can also be a callable defaults_for_type_by_name = List(default_value=[], config=True) - def __init__(self, parent, name=None, config=None): - super(PrettyResultDisplay, self).__init__(parent, name=name, config=config) + def __init__(self, shell, config=None): + super(PrettyResultDisplay, self).__init__(config=config) + self.shell = shell self._setup_defaults() def _setup_defaults(self): @@ -87,16 +89,6 @@ class PrettyResultDisplay(Component): else: raise TypeError('func_name must be a str or callable, got: %r' % func_name) - # Access other components like this rather than by a regular attribute. - # This won't lookup the InteractiveShell object until it is used and - # then it is cached. This is both efficient and couples this class - # more loosely to InteractiveShell. - @auto_attr - def shell(self): - return Component.get_instances( - root=self.root, - klass='IPython.core.iplib.InteractiveShell')[0] - def __call__(self, otherself, arg): """Uber-pretty-printing display hook. @@ -132,7 +124,7 @@ def load_ipython_extension(ip): """Load the extension in IPython as a hook.""" global _loaded if not _loaded: - prd = PrettyResultDisplay(ip, name='pretty_result_display') + prd = PrettyResultDisplay(ip, config=ip.config) ip.set_hook('result_display', prd, priority=99) _loaded = True return prd