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