##// END OF EJS Templates
Implement magic registration and listing in magics manager.
Fernando Perez -
Show More
@@ -142,6 +142,10 b" cell_magic = _magic_marker('cell')"
142 142 class MagicManager(Configurable):
143 143 """Object that handles all magic-related functionality for IPython.
144 144 """
145 # Non-configurable class attributes
146 line_magics = Dict
147 cell_magics = Dict
148
145 149 # An instance of the IPython shell we are attached to
146 150 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
147 151
@@ -155,30 +159,28 b' class MagicManager(Configurable):'
155 159
156 160
157 161 def lsmagic(self):
158 """Return a list of currently available magic functions.
159
160 Gives a list of the bare names after mangling (['ls','cd', ...], not
161 ['magic_ls','magic_cd',...]"""
162
163 # FIXME. This needs a cleanup, in the way the magics list is built.
164
165 # magics in class definition
166 class_magic = lambda fn: fn.startswith('magic_') and \
167 callable(Magic.__dict__[fn])
168 # in instance namespace (run-time user additions)
169 inst_magic = lambda fn: fn.startswith('magic_') and \
170 callable(self.__dict__[fn])
171 # and bound magics by user (so they can access self):
172 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
173 callable(self.__class__.__dict__[fn])
174 magics = filter(class_magic, Magic.__dict__.keys()) + \
175 filter(inst_magic, self.__dict__.keys()) + \
176 filter(inst_bound_magic, self.__class__.__dict__.keys())
177 out = []
178 for fn in set(magics):
179 out.append(fn.replace('magic_', '', 1))
180 out.sort()
181 return out
162 """Return a dict of currently available magic functions.
163
164 The return dict has the keys 'line' and 'cell', corresponding to the
165 two types of magics we support. Each value is a list of names.
166 """
167
168 return dict(line = sorted(self.line_magics),
169 cell = sorted(self.cell_magics))
170
171 def register(self, *magics):
172 """Register one or more instances of Magics.
173 """
174 # Start by validating them to ensure they have all had their magic
175 # methods registered at the instance level
176 for m in magics:
177 if not m.registered:
178 raise ValueError("Class of magics %r was constructed without "
179 "the @register_macics class decorator")
180 self.line_magics.update(m.line_magics)
181 self.cell_magics.update(m.cell_magics)
182
183
182 184
183 185 # Key base class that provides the central functionality for magics.
184 186
General Comments 0
You need to be logged in to leave comments. Login now