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