##// END OF EJS Templates
By default, Magics inherit from Configurable
Matthias BUSSONNIER -
Show More
@@ -477,7 +477,8 b' class MagicsManager(Configurable):'
477 477
478 478 # Key base class that provides the central functionality for magics.
479 479
480 class Magics(object):
480
481 class Magics(Configurable):
481 482 """Base class for implementing magic functions.
482 483
483 484 Shell functions which can be reached as %function_name. All magic
@@ -506,10 +507,23 b' class Magics(object):'
506 507 # Instance of IPython shell
507 508 shell = None
508 509
509 def __init__(self, shell):
510 def __init__(self, shell=None, **kwargs):
511 if shell is not None:
512 kwargs['shell'] = shell
513 kwargs.setdefault('parent', shell)
514 super(Magics, self).__init__(**kwargs)
515
516 def __init__(self, shell=None, **kwargs):
510 517 if not(self.__class__.registered):
511 518 raise ValueError('Magics subclass without registration - '
512 519 'did you forget to apply @magics_class?')
520 if shell is not None:
521 if hasattr( shell, 'configurables'):
522 shell.configurables.append(self)
523 if hasattr( shell, 'config'):
524 kwargs.setdefault('parent', shell)
525 kwargs['shell'] = shell
526
513 527 self.shell = shell
514 528 self.options_table = {}
515 529 # The method decorators are run when the instance doesn't exist yet, so
@@ -530,6 +544,9 b' class Magics(object):'
530 544 else:
531 545 # it's the real thing
532 546 tab[magic_name] = meth_name
547 # Configurable **need** to be initiated at the end or the config
548 # magics get screwed up.
549 super(Magics, self).__init__(**kwargs)
533 550
534 551 def arg_err(self,func):
535 552 """Print docstring if incorrect arguments were passed"""
@@ -639,6 +656,7 b' class Magics(object):'
639 656 error("%s is not a magic function" % fn)
640 657 self.options_table[fn] = optstr
641 658
659
642 660 class MagicAlias(object):
643 661 """An alias to another magic function.
644 662
@@ -71,7 +71,7 b' def script_args(f):'
71 71 return f
72 72
73 73 @magics_class
74 class ScriptMagics(Magics, Configurable):
74 class ScriptMagics(Magics):
75 75 """Magics for talking to scripts
76 76
77 77 This defines a base `%%script` cell magic for running a cell
@@ -116,9 +116,8 b' class ScriptMagics(Magics, Configurable):'
116 116 )
117 117
118 118 def __init__(self, shell=None):
119 Configurable.__init__(self, config=shell.config)
119 super(ScriptMagics, self).__init__(shell=shell)
120 120 self._generate_script_magics()
121 Magics.__init__(self, shell=shell)
122 121 self.job_manager = BackgroundJobManager()
123 122 self.bg_processes = []
124 123 atexit.register(self.kill_bg_processes)
@@ -60,7 +60,6 b' def test_extract_code_ranges():'
60 60 actual = list(code.extract_code_ranges(instr))
61 61 nt.assert_equal(actual, expected)
62 62
63
64 63 def test_extract_symbols():
65 64 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
66 65 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
@@ -82,6 +81,14 b' def test_extract_symbols_raises_exception_with_non_python_code():'
82 81 with nt.assert_raises(SyntaxError):
83 82 code.extract_symbols(source, "hello")
84 83
84 def test_config():
85 """ test that config magic does not raise
86 can happen if Configurable init is moved too early into
87 Magics.__init__ as then a Config object will be registerd as a
88 magic.
89 """
90 ## should not raise.
91 _ip.magic('config')
85 92
86 93 def test_rehashx():
87 94 # clear up everything
@@ -25,7 +25,6 b' To automatically restore stored variables at startup, add this to your'
25 25 import inspect, os, sys, textwrap
26 26
27 27 # Our own
28 from IPython.config.configurable import Configurable
29 28 from IPython.core.error import UsageError
30 29 from IPython.core.magic import Magics, magics_class, line_magic
31 30 from IPython.testing.skipdoctest import skip_doctest
@@ -69,7 +68,7 b' def restore_data(ip):'
69 68
70 69
71 70 @magics_class
72 class StoreMagics(Magics, Configurable):
71 class StoreMagics(Magics):
73 72 """Lightweight persistence for python variables.
74 73
75 74 Provides the %store magic."""
@@ -81,8 +80,7 b' class StoreMagics(Magics, Configurable):'
81 80 )
82 81
83 82 def __init__(self, shell):
84 Configurable.__init__(self, config=shell.config)
85 Magics.__init__(self, shell=shell)
83 super(StoreMagics, self).__init__(shell=shell)
86 84 self.shell.configurables.append(self)
87 85 if self.autorestore:
88 86 restore_data(self.shell)
@@ -33,6 +33,7 b' from IPython.core.hooks import TryNext'
33 33 noop = lambda *a, **kw: None
34 34
35 35 class FakeShell(object):
36
36 37 def __init__(self):
37 38 self.ns = {}
38 39 self.auto_magics = AutoreloadMagics(shell=self)
General Comments 0
You need to be logged in to leave comments. Login now