From 1f1d8fa1ae509c8112860526adbabc1727c9c8e1 2012-05-26 03:25:56 From: Fernando Perez Date: 2012-05-26 03:25:56 Subject: [PATCH] Renamed @register_magics to @magics_class to avoid confusion. The main ipython object also has a .register_magics method that must often be used in close proximity to the class decorator, yet does something completely different. Having these two objects with the same name yet different purposes was proving to be quite confusing in my testing usage so far. --- diff --git a/IPython/core/magic.py b/IPython/core/magic.py index d61ea88..8173417 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -38,7 +38,7 @@ from IPython.utils.warn import error, warn # A dict we'll use for each class that has magics, used as temporary storage to # pass information between the @line/cell_magic method decorators and the -# @register_magics class decorator, because the method decorators have no +# @magics_class class decorator, because the method decorators have no # access to the class when they run. See for more details: # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class @@ -82,7 +82,7 @@ def needs_local_scope(func): # Class and method decorators for registering magics #----------------------------------------------------------------------------- -def register_magics(cls): +def magics_class(cls): cls.registered = True cls.magics = dict(line = magics['line'], cell = magics['cell']) @@ -294,7 +294,7 @@ class Magics(object): - Use the method decorators `@line_magic` and `@cell_magic` to decorate individual methods as magic functions, AND - - Use the class decorator `@register_magics` to ensure that the magic + - Use the class decorator `@magics_class` to ensure that the magic methods are properly registered at the instance level upon instance initialization. @@ -312,7 +312,7 @@ class Magics(object): def __init__(self, shell): if not(self.__class__.registered): raise ValueError('Magics subclass without registration - ' - 'did you forget to apply @register_magics?') + 'did you forget to apply @magics_class?') self.shell = shell self.options_table = {} # The method decorators are run when the instance doesn't exist yet, so diff --git a/IPython/core/magics/__init__.py b/IPython/core/magics/__init__.py index fa82e20..23479b5 100644 --- a/IPython/core/magics/__init__.py +++ b/IPython/core/magics/__init__.py @@ -12,7 +12,7 @@ # Imports #----------------------------------------------------------------------------- -from ..magic import Magics, register_magics +from ..magic import Magics, magics_class from .auto import AutoMagics from .basic import BasicMagics from .code import CodeMagics, MacroToEdit @@ -30,7 +30,7 @@ from .pylab import PylabMagics # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class UserMagics(Magics): """Placeholder for user-defined magics to be added at runtime. diff --git a/IPython/core/magics/auto.py b/IPython/core/magics/auto.py index bfa5059..348d982 100644 --- a/IPython/core/magics/auto.py +++ b/IPython/core/magics/auto.py @@ -13,7 +13,7 @@ #----------------------------------------------------------------------------- # Our own packages -from IPython.core.magic import Bunch, Magics, register_magics, line_magic +from IPython.core.magic import Bunch, Magics, magics_class, line_magic from IPython.testing.skipdoctest import skip_doctest from IPython.utils.warn import error @@ -21,7 +21,7 @@ from IPython.utils.warn import error # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class AutoMagics(Magics): """Magics that control various autoX behaviors.""" diff --git a/IPython/core/magics/basic.py b/IPython/core/magics/basic.py index 57b5953..707bbf0 100644 --- a/IPython/core/magics/basic.py +++ b/IPython/core/magics/basic.py @@ -20,7 +20,7 @@ from pprint import pformat # Our own packages from IPython.core.error import UsageError -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.core.prefilter import ESC_MAGIC from IPython.utils.text import format_screen from IPython.core import magic_arguments, page @@ -33,7 +33,7 @@ from IPython.utils.warn import warn, error # Magics class implementation #----------------------------------------------------------------------------- -@register_magics +@magics_class class BasicMagics(Magics): """Magics that provide central IPython functionality. diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 7052d58..9bf5e6c 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -23,7 +23,7 @@ from urllib2 import urlopen # Our own packages from IPython.core.error import TryNext from IPython.core.macro import Macro -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.testing.skipdoctest import skip_doctest from IPython.utils import openpy from IPython.utils import py3compat @@ -39,7 +39,7 @@ from IPython.utils.warn import warn class MacroToEdit(ValueError): pass -@register_magics +@magics_class class CodeMagics(Magics): """Magics related to code management (loading, saving, editing, ...).""" diff --git a/IPython/core/magics/config.py b/IPython/core/magics/config.py index 204e881..5480d12 100644 --- a/IPython/core/magics/config.py +++ b/IPython/core/magics/config.py @@ -17,14 +17,14 @@ import re # Our own packages from IPython.core.error import UsageError -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.utils.warn import error #----------------------------------------------------------------------------- # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class ConfigMagics(Magics): def __init__(self, shell): diff --git a/IPython/core/magics/deprecated.py b/IPython/core/magics/deprecated.py index 4f544c6..254b101 100644 --- a/IPython/core/magics/deprecated.py +++ b/IPython/core/magics/deprecated.py @@ -13,13 +13,13 @@ #----------------------------------------------------------------------------- # Our own packages -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic #----------------------------------------------------------------------------- # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class DeprecatedMagics(Magics): """Magics slated for later removal.""" diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 30a1a8f..7ddf6db 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -36,7 +36,7 @@ from IPython.core import debugger, oinspect from IPython.core import page from IPython.core.error import UsageError from IPython.core.macro import Macro -from IPython.core.magic import (Magics, register_magics, line_magic, +from IPython.core.magic import (Magics, magics_class, line_magic, on_off, needs_local_scope) from IPython.testing.skipdoctest import skip_doctest from IPython.utils import py3compat @@ -50,7 +50,7 @@ from IPython.utils.warn import warn, error # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class ExecutionMagics(Magics): """Magics related to code execution, debugging, profiling, etc. diff --git a/IPython/core/magics/extension.py b/IPython/core/magics/extension.py index 9425eff..37982e5 100644 --- a/IPython/core/magics/extension.py +++ b/IPython/core/magics/extension.py @@ -16,13 +16,13 @@ import os # Our own packages -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic #----------------------------------------------------------------------------- # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class ExtensionMagics(Magics): """Magics to manage the IPython extensions system.""" diff --git a/IPython/core/magics/history.py b/IPython/core/magics/history.py index 4516582..594824d 100644 --- a/IPython/core/magics/history.py +++ b/IPython/core/magics/history.py @@ -19,7 +19,7 @@ from io import open as io_open # Our own packages from IPython.core.error import StdinNotImplementedError -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.testing.skipdoctest import skip_doctest from IPython.utils import io @@ -27,7 +27,7 @@ from IPython.utils import io # Magics class implementation #----------------------------------------------------------------------------- -@register_magics +@magics_class class HistoryMagics(Magics): @skip_doctest diff --git a/IPython/core/magics/logging.py b/IPython/core/magics/logging.py index f8606fb..23b5567 100644 --- a/IPython/core/magics/logging.py +++ b/IPython/core/magics/logging.py @@ -17,14 +17,14 @@ import os import sys # Our own packages -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.utils.warn import warn #----------------------------------------------------------------------------- # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class LoggingMagics(Magics): """Magics related to all logging machinery.""" diff --git a/IPython/core/magics/namespace.py b/IPython/core/magics/namespace.py index e20d0de..08e0dc9 100644 --- a/IPython/core/magics/namespace.py +++ b/IPython/core/magics/namespace.py @@ -20,7 +20,7 @@ import sys # Our own packages from IPython.core import page from IPython.core.error import StdinNotImplementedError -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.testing.skipdoctest import skip_doctest from IPython.utils.encoding import DEFAULT_ENCODING from IPython.utils.path import get_py_filename @@ -29,7 +29,7 @@ from IPython.utils.path import get_py_filename # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class NamespaceMagics(Magics): """Magics to manage various aspects of the user's namespace. diff --git a/IPython/core/magics/osm.py b/IPython/core/magics/osm.py index f4eae8c..0a358b9 100644 --- a/IPython/core/magics/osm.py +++ b/IPython/core/magics/osm.py @@ -25,7 +25,7 @@ from pprint import pformat from IPython.core import oinspect from IPython.core import page from IPython.core.error import UsageError -from IPython.core.magic import (Magics, compress_dhist, register_magics, +from IPython.core.magic import (Magics, compress_dhist, magics_class, line_magic) from IPython.testing.skipdoctest import skip_doctest from IPython.utils.io import file_read, nlprint @@ -35,7 +35,7 @@ from IPython.utils.terminal import set_term_title #----------------------------------------------------------------------------- # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class OSMagics(Magics): """Magics to interact with the underlying OS (shell-type functionality). """ diff --git a/IPython/core/magics/pylab.py b/IPython/core/magics/pylab.py index d64c929..f07b717 100644 --- a/IPython/core/magics/pylab.py +++ b/IPython/core/magics/pylab.py @@ -14,14 +14,14 @@ # Our own packages from IPython.config.application import Application -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.testing.skipdoctest import skip_doctest #----------------------------------------------------------------------------- # Magic implementation classes #----------------------------------------------------------------------------- -@register_magics +@magics_class class PylabMagics(Magics): """Magics related to matplotlib's pylab support""" diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 8fa2153..d664931 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -29,7 +29,7 @@ from IPython.utils.tempdir import TemporaryDirectory # Test functions begin #----------------------------------------------------------------------------- -@magic.register_magics +@magic.magics_class class DummyMagics(magic.Magics): pass def test_rehashx(): diff --git a/IPython/extensions/autoreload.py b/IPython/extensions/autoreload.py index 75b71c6..6ca4635 100644 --- a/IPython/extensions/autoreload.py +++ b/IPython/extensions/autoreload.py @@ -406,10 +406,10 @@ def superreload(module, reload=reload, old_objects={}): #------------------------------------------------------------------------------ from IPython.core.hooks import TryNext -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.core.plugin import Plugin -@register_magics +@magics_class class AutoreloadMagics(Magics): def __init__(self, *a, **kw): super(AutoreloadMagics, self).__init__(*a, **kw) diff --git a/IPython/extensions/parallelmagic.py b/IPython/extensions/parallelmagic.py index f2b910a..abb49fb 100644 --- a/IPython/extensions/parallelmagic.py +++ b/IPython/extensions/parallelmagic.py @@ -37,7 +37,7 @@ Usage import ast import re -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.testing.skipdoctest import skip_doctest #----------------------------------------------------------------------------- @@ -49,7 +49,7 @@ Use activate() on a DirectView object to activate it for magics. """ -@register_magics +@magics_class class ParallelMagics(Magics): """A set of magics useful when controlling a parallel IPython cluster. """ diff --git a/IPython/extensions/storemagic.py b/IPython/extensions/storemagic.py index 607d2ab..20cb84d 100644 --- a/IPython/extensions/storemagic.py +++ b/IPython/extensions/storemagic.py @@ -14,7 +14,7 @@ import inspect, os, sys, textwrap from IPython.core.error import UsageError from IPython.core.fakemodule import FakeModule -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.core.plugin import Plugin from IPython.testing.skipdoctest import skip_doctest from IPython.utils.traitlets import Bool, Instance @@ -53,7 +53,7 @@ def restore_data(ip): restore_dhist(ip) -@register_magics +@magics_class class StoreMagics(Magics): """Lightweight persistence for python variables. diff --git a/IPython/frontend/terminal/embed.py b/IPython/frontend/terminal/embed.py index 5cdead6..f1430a7 100644 --- a/IPython/frontend/terminal/embed.py +++ b/IPython/frontend/terminal/embed.py @@ -29,7 +29,7 @@ from contextlib import nested import warnings from IPython.core import ultratb -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell from IPython.frontend.terminal.ipapp import load_default_config @@ -42,7 +42,7 @@ from IPython.utils.io import ask_yes_no #----------------------------------------------------------------------------- # This is an additional magic that is exposed in embedded shells. -@register_magics +@magics_class class EmbeddedMagics(Magics): @line_magic diff --git a/IPython/frontend/terminal/interactiveshell.py b/IPython/frontend/terminal/interactiveshell.py index 7b45eb1..1086edd 100644 --- a/IPython/frontend/terminal/interactiveshell.py +++ b/IPython/frontend/terminal/interactiveshell.py @@ -25,7 +25,7 @@ from contextlib import nested from IPython.core.error import TryNext, UsageError from IPython.core.usage import interactive_usage, default_banner from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC -from IPython.core.magic import Magics, register_magics, line_magic +from IPython.core.magic import Magics, magics_class, line_magic from IPython.testing.skipdoctest import skip_doctest from IPython.utils.encoding import get_stream_enc from IPython.utils import py3compat @@ -120,7 +120,7 @@ def rerun_pasted(shell, name='pasted_block'): # Terminal-specific magics #------------------------------------------------------------------------ -@register_magics +@magics_class class TerminalMagics(Magics): @line_magic