##// END OF EJS Templates
Fix %run
Fernando Perez -
Show More
@@ -1,363 +1,363 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
8 8 # Copyright (C) 2008 The IPython Development Team
9 9
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17 # Stdlib
18 18 import os
19 19 import re
20 20 import sys
21 21 import types
22 22 from getopt import getopt, GetoptError
23 23
24 24 # Our own
25 25 from IPython.config.configurable import Configurable
26 26 from IPython.core import oinspect
27 27 from IPython.core.error import UsageError
28 28 from IPython.core.prefilter import ESC_MAGIC
29 29 from IPython.external.decorator import decorator
30 30 from IPython.utils.ipstruct import Struct
31 31 from IPython.utils.process import arg_split
32 32 from IPython.utils.traitlets import Bool, Dict, Instance
33 33 from IPython.utils.warn import error
34 34
35 35 #-----------------------------------------------------------------------------
36 36 # Globals
37 37 #-----------------------------------------------------------------------------
38 38
39 39 # A dict we'll use for each class that has magics, used as temporary storage to
40 40 # pass information between the @line/cell_magic method decorators and the
41 41 # @register_magics class decorator, because the method decorators have no
42 42 # access to the class when they run. See for more details:
43 43 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
44 44
45 45 magics = dict(line={}, cell={})
46 46
47 47 magic_types = ('line', 'cell')
48 48
49 49 #-----------------------------------------------------------------------------
50 50 # Utility classes and functions
51 51 #-----------------------------------------------------------------------------
52 52
53 53 class Bunch: pass
54 54
55 55
56 56 def on_off(tag):
57 57 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
58 58 return ['OFF','ON'][tag]
59 59
60 60
61 61 def compress_dhist(dh):
62 62 head, tail = dh[:-10], dh[-10:]
63 63
64 64 newhead = []
65 65 done = set()
66 66 for h in head:
67 67 if h in done:
68 68 continue
69 69 newhead.append(h)
70 70 done.add(h)
71 71
72 72 return newhead + tail
73 73
74 74
75 75 def needs_local_scope(func):
76 76 """Decorator to mark magic functions which need to local scope to run."""
77 77 func.needs_local_scope = True
78 78 return func
79 79
80 80 #-----------------------------------------------------------------------------
81 81 # Class and method decorators for registering magics
82 82 #-----------------------------------------------------------------------------
83 83
84 84 def register_magics(cls):
85 85 cls.registered = True
86 86 cls.magics = dict(line = magics['line'],
87 87 cell = magics['cell'])
88 88 magics['line'] = {}
89 89 magics['cell'] = {}
90 90 return cls
91 91
92 92
93 93 def validate_type(magic_type):
94 94 if magic_type not in magic_types:
95 95 raise ValueError('magic_type must be one of %s, %s given' %
96 96 magic_types, magic_type)
97 97
98 98
99 99 def _magic_marker(magic_type):
100 100 validate_type(magic_type)
101 101
102 102 # This is a closure to capture the magic_type. We could also use a class,
103 103 # but it's overkill for just that one bit of state.
104 104 def magic_deco(arg):
105 105 call = lambda f, *a, **k: f(*a, **k)
106 106
107 107 if callable(arg):
108 108 # "Naked" decorator call (just @foo, no args)
109 109 func = arg
110 110 name = func.func_name
111 111 func.magic_name = name
112 112 retval = decorator(call, func)
113 113 magics[magic_type][name] = name
114 114 elif isinstance(arg, basestring):
115 115 # Decorator called with arguments (@foo('bar'))
116 116 name = arg
117 117 def mark(func, *a, **kw):
118 118 func.magic_name = name
119 119 magics[magic_type][name] = func.func_name
120 120 return decorator(call, func)
121 121 retval = mark
122 122 else:
123 123 raise ValueError("Decorator can only be called with "
124 124 "string or function")
125 125
126 126 return retval
127 127
128 128 return magic_deco
129 129
130 130
131 131 line_magic = _magic_marker('line')
132 132 cell_magic = _magic_marker('cell')
133 133
134 134 #-----------------------------------------------------------------------------
135 135 # Core Magic classes
136 136 #-----------------------------------------------------------------------------
137 137
138 138 class MagicsManager(Configurable):
139 139 """Object that handles all magic-related functionality for IPython.
140 140 """
141 141 # Non-configurable class attributes
142 142 magics = Dict
143 143
144 144 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
145 145
146 146 auto_magic = Bool
147 147
148 148 _auto_status = [
149 149 'Automagic is OFF, % prefix IS needed for magic functions.',
150 150 'Automagic is ON, % prefix IS NOT needed for magic functions.']
151 151
152 152 user_magics = Instance('IPython.core.magic_functions.UserMagics')
153 153
154 154 def __init__(self, shell=None, config=None, user_magics=None, **traits):
155 155
156 156 super(MagicsManager, self).__init__(shell=shell, config=config,
157 157 user_magics=user_magics, **traits)
158 158 self.magics = dict(line={}, cell={})
159 159
160 160 def auto_status(self):
161 161 """Return descriptive string with automagic status."""
162 162 return self._auto_status[self.auto_magic]
163 163
164 164 def lsmagic(self):
165 165 """Return a dict of currently available magic functions.
166 166
167 167 The return dict has the keys 'line' and 'cell', corresponding to the
168 168 two types of magics we support. Each value is a list of names.
169 169 """
170 170 return self.magics
171 171
172 172 def register(self, *magic_objects):
173 173 """Register one or more instances of Magics.
174 174 """
175 175 # Start by validating them to ensure they have all had their magic
176 176 # methods registered at the instance level
177 177 for m in magic_objects:
178 178 if not m.registered:
179 179 raise ValueError("Class of magics %r was constructed without "
180 180 "the @register_macics class decorator")
181 181 if type(m) is type:
182 182 # If we're given an uninstantiated class
183 183 m = m(self.shell)
184 184
185 185 for mtype in magic_types:
186 186 self.magics[mtype].update(m.magics[mtype])
187 187
188 188 def define_magic(self, magic_name, func, magic_type='line'):
189 189 """Expose own function as magic function for ipython
190 190
191 191 Example::
192 192
193 193 def foo_impl(self, parameter_s=''):
194 194 'My very own magic!. (Use docstrings, IPython reads them).'
195 195 print 'Magic function. Passed parameter is between < >:'
196 196 print '<%s>' % parameter_s
197 197 print 'The self object is:', self
198 198
199 199 ip.define_magic('foo', foo_impl)
200 200 """
201 201 # Create the new method in the user_magics and register it in the
202 202 # global table
203 203 self.user_magics.new_magic(magic_name, func, magic_type)
204 204 self.magics[magic_type][magic_name] = \
205 205 self.user_magics.magics[magic_type][magic_name]
206 206
207 207 # Key base class that provides the central functionality for magics.
208 208
209 209 class Magics(object):
210 210 """Base class for implementing magic functions.
211 211
212 212 Shell functions which can be reached as %function_name. All magic
213 213 functions should accept a string, which they can parse for their own
214 214 needs. This can make some functions easier to type, eg `%cd ../`
215 215 vs. `%cd("../")`
216 216
217 217 Classes providing magic functions need to subclass this class, and they
218 218 MUST:
219 219
220 220 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
221 221 individual methods as magic functions, AND
222 222
223 223 - Use the class decorator `@register_magics` to ensure that the magic
224 224 methods are properly registered at the instance level upon instance
225 225 initialization.
226 226
227 227 See :mod:`magic_functions` for examples of actual implementation classes.
228 228 """
229 229 # Dict holding all command-line options for each magic.
230 230 options_table = None
231
232 # Non-configurable class attributes
233 magics = Dict
234
231 # Dict for the mapping of magic names to methods, set by class decorator
232 magics = None
233 # Flag to check that the class decorator was properly applied
235 234 registered = False
236 235
237 236 def __init__(self, shell):
238 237 if not(self.__class__.registered):
239 238 raise ValueError('unregistered Magics')
240 239 self.shell = shell
240 self.options_table = {}
241 241 mtab = dict(line={}, cell={})
242 242 for mtype in magic_types:
243 243 tab = mtab[mtype]
244 244 for magic_name, meth_name in self.magics[mtype].iteritems():
245 245 if isinstance(meth_name, basestring):
246 246 tab[magic_name] = getattr(self, meth_name)
247 247 self.magics.update(mtab)
248 248
249 249 def arg_err(self,func):
250 250 """Print docstring if incorrect arguments were passed"""
251 251 print 'Error in arguments:'
252 252 print oinspect.getdoc(func)
253 253
254 def format_latex(self,strng):
254 def format_latex(self, strng):
255 255 """Format a string for latex inclusion."""
256 256
257 257 # Characters that need to be escaped for latex:
258 258 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
259 259 # Magic command names as headers:
260 260 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
261 261 re.MULTILINE)
262 262 # Magic commands
263 263 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
264 264 re.MULTILINE)
265 265 # Paragraph continue
266 266 par_re = re.compile(r'\\$',re.MULTILINE)
267 267
268 268 # The "\n" symbol
269 269 newline_re = re.compile(r'\\n')
270 270
271 271 # Now build the string for output:
272 272 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
273 273 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
274 274 strng)
275 275 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
276 276 strng = par_re.sub(r'\\\\',strng)
277 277 strng = escape_re.sub(r'\\\1',strng)
278 278 strng = newline_re.sub(r'\\textbackslash{}n',strng)
279 279 return strng
280 280
281 281 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
282 282 """Parse options passed to an argument string.
283 283
284 284 The interface is similar to that of getopt(), but it returns back a
285 285 Struct with the options as keys and the stripped argument string still
286 286 as a string.
287 287
288 288 arg_str is quoted as a true sys.argv vector by using shlex.split.
289 289 This allows us to easily expand variables, glob files, quote
290 290 arguments, etc.
291 291
292 292 Options:
293 293 -mode: default 'string'. If given as 'list', the argument string is
294 294 returned as a list (split on whitespace) instead of a string.
295 295
296 296 -list_all: put all option values in lists. Normally only options
297 297 appearing more than once are put in a list.
298 298
299 299 -posix (True): whether to split the input line in POSIX mode or not,
300 300 as per the conventions outlined in the shlex module from the
301 301 standard library."""
302 302
303 303 # inject default options at the beginning of the input line
304 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
304 caller = sys._getframe(1).f_code.co_name
305 305 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
306 306
307 307 mode = kw.get('mode','string')
308 308 if mode not in ['string','list']:
309 309 raise ValueError,'incorrect mode given: %s' % mode
310 310 # Get options
311 311 list_all = kw.get('list_all',0)
312 312 posix = kw.get('posix', os.name == 'posix')
313 313 strict = kw.get('strict', True)
314 314
315 315 # Check if we have more than one argument to warrant extra processing:
316 316 odict = {} # Dictionary with options
317 317 args = arg_str.split()
318 318 if len(args) >= 1:
319 319 # If the list of inputs only has 0 or 1 thing in it, there's no
320 320 # need to look for options
321 321 argv = arg_split(arg_str, posix, strict)
322 322 # Do regular option processing
323 323 try:
324 324 opts,args = getopt(argv,opt_str,*long_opts)
325 325 except GetoptError,e:
326 326 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
327 327 " ".join(long_opts)))
328 328 for o,a in opts:
329 329 if o.startswith('--'):
330 330 o = o[2:]
331 331 else:
332 332 o = o[1:]
333 333 try:
334 334 odict[o].append(a)
335 335 except AttributeError:
336 336 odict[o] = [odict[o],a]
337 337 except KeyError:
338 338 if list_all:
339 339 odict[o] = [a]
340 340 else:
341 341 odict[o] = a
342 342
343 343 # Prepare opts,args for return
344 344 opts = Struct(odict)
345 345 if mode == 'string':
346 346 args = ' '.join(args)
347 347
348 348 return opts,args
349 349
350 350 def default_option(self, fn, optstr):
351 351 """Make an entry in the options_table for fn, with value optstr"""
352 352
353 353 if fn not in self.lsmagic():
354 354 error("%s is not a magic function" % fn)
355 355 self.options_table[fn] = optstr
356 356
357 357 def new_magic(self, magic_name, func, magic_type='line'):
358 358 """TODO
359 359 """
360 360 validate_type(magic_type)
361 361 meth = types.MethodType(func, self)
362 362 setattr(self, magic_name, meth)
363 363 self.magics[magic_type][magic_name] = meth
General Comments 0
You need to be logged in to leave comments. Login now