##// END OF EJS Templates
ask_yes_no on %%file overwrite
MinRK -
Show More
@@ -1,713 +1,725 b''
1 """Implementation of magic functions for interaction with the OS.
1 """Implementation of magic functions for interaction with the OS.
2
2
3 Note: this module is named 'osm' instead of 'os' to avoid a collision with the
3 Note: this module is named 'osm' instead of 'os' to avoid a collision with the
4 builtin.
4 builtin.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012 The IPython Development Team.
7 # Copyright (c) 2012 The IPython Development Team.
8 #
8 #
9 # Distributed under the terms of the Modified BSD License.
9 # Distributed under the terms of the Modified BSD License.
10 #
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 # Stdlib
18 # Stdlib
19 import io
19 import os
20 import os
20 import re
21 import re
21 import sys
22 import sys
22 from pprint import pformat
23 from pprint import pformat
23
24
24 # Our own packages
25 # Our own packages
25 from IPython.core import magic_arguments
26 from IPython.core import magic_arguments
26 from IPython.core import oinspect
27 from IPython.core import oinspect
27 from IPython.core import page
28 from IPython.core import page
28 from IPython.core.error import UsageError
29 from IPython.core.error import UsageError, StdinNotImplementedError
29 from IPython.core.magic import (
30 from IPython.core.magic import (
30 Magics, compress_dhist, magics_class, line_magic, cell_magic
31 Magics, compress_dhist, magics_class, line_magic, cell_magic
31 )
32 )
32 from IPython.testing.skipdoctest import skip_doctest
33 from IPython.testing.skipdoctest import skip_doctest
33 from IPython.utils.io import file_read, nlprint
34 from IPython.utils.io import file_read, nlprint
34 from IPython.utils.path import get_py_filename, unquote_filename
35 from IPython.utils.path import get_py_filename, unquote_filename
35 from IPython.utils.process import abbrev_cwd
36 from IPython.utils.process import abbrev_cwd
36 from IPython.utils.terminal import set_term_title
37 from IPython.utils.terminal import set_term_title
37 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
38 # Magic implementation classes
39 # Magic implementation classes
39 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
40 @magics_class
41 @magics_class
41 class OSMagics(Magics):
42 class OSMagics(Magics):
42 """Magics to interact with the underlying OS (shell-type functionality).
43 """Magics to interact with the underlying OS (shell-type functionality).
43 """
44 """
44
45
45 @skip_doctest
46 @skip_doctest
46 @line_magic
47 @line_magic
47 def alias(self, parameter_s=''):
48 def alias(self, parameter_s=''):
48 """Define an alias for a system command.
49 """Define an alias for a system command.
49
50
50 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
51 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
51
52
52 Then, typing 'alias_name params' will execute the system command 'cmd
53 Then, typing 'alias_name params' will execute the system command 'cmd
53 params' (from your underlying operating system).
54 params' (from your underlying operating system).
54
55
55 Aliases have lower precedence than magic functions and Python normal
56 Aliases have lower precedence than magic functions and Python normal
56 variables, so if 'foo' is both a Python variable and an alias, the
57 variables, so if 'foo' is both a Python variable and an alias, the
57 alias can not be executed until 'del foo' removes the Python variable.
58 alias can not be executed until 'del foo' removes the Python variable.
58
59
59 You can use the %l specifier in an alias definition to represent the
60 You can use the %l specifier in an alias definition to represent the
60 whole line when the alias is called. For example::
61 whole line when the alias is called. For example::
61
62
62 In [2]: alias bracket echo "Input in brackets: <%l>"
63 In [2]: alias bracket echo "Input in brackets: <%l>"
63 In [3]: bracket hello world
64 In [3]: bracket hello world
64 Input in brackets: <hello world>
65 Input in brackets: <hello world>
65
66
66 You can also define aliases with parameters using %s specifiers (one
67 You can also define aliases with parameters using %s specifiers (one
67 per parameter)::
68 per parameter)::
68
69
69 In [1]: alias parts echo first %s second %s
70 In [1]: alias parts echo first %s second %s
70 In [2]: %parts A B
71 In [2]: %parts A B
71 first A second B
72 first A second B
72 In [3]: %parts A
73 In [3]: %parts A
73 Incorrect number of arguments: 2 expected.
74 Incorrect number of arguments: 2 expected.
74 parts is an alias to: 'echo first %s second %s'
75 parts is an alias to: 'echo first %s second %s'
75
76
76 Note that %l and %s are mutually exclusive. You can only use one or
77 Note that %l and %s are mutually exclusive. You can only use one or
77 the other in your aliases.
78 the other in your aliases.
78
79
79 Aliases expand Python variables just like system calls using ! or !!
80 Aliases expand Python variables just like system calls using ! or !!
80 do: all expressions prefixed with '$' get expanded. For details of
81 do: all expressions prefixed with '$' get expanded. For details of
81 the semantic rules, see PEP-215:
82 the semantic rules, see PEP-215:
82 http://www.python.org/peps/pep-0215.html. This is the library used by
83 http://www.python.org/peps/pep-0215.html. This is the library used by
83 IPython for variable expansion. If you want to access a true shell
84 IPython for variable expansion. If you want to access a true shell
84 variable, an extra $ is necessary to prevent its expansion by
85 variable, an extra $ is necessary to prevent its expansion by
85 IPython::
86 IPython::
86
87
87 In [6]: alias show echo
88 In [6]: alias show echo
88 In [7]: PATH='A Python string'
89 In [7]: PATH='A Python string'
89 In [8]: show $PATH
90 In [8]: show $PATH
90 A Python string
91 A Python string
91 In [9]: show $$PATH
92 In [9]: show $$PATH
92 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
93 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
93
94
94 You can use the alias facility to acess all of $PATH. See the %rehash
95 You can use the alias facility to acess all of $PATH. See the %rehash
95 and %rehashx functions, which automatically create aliases for the
96 and %rehashx functions, which automatically create aliases for the
96 contents of your $PATH.
97 contents of your $PATH.
97
98
98 If called with no parameters, %alias prints the current alias table."""
99 If called with no parameters, %alias prints the current alias table."""
99
100
100 par = parameter_s.strip()
101 par = parameter_s.strip()
101 if not par:
102 if not par:
102 aliases = sorted(self.shell.alias_manager.aliases)
103 aliases = sorted(self.shell.alias_manager.aliases)
103 # stored = self.shell.db.get('stored_aliases', {} )
104 # stored = self.shell.db.get('stored_aliases', {} )
104 # for k, v in stored:
105 # for k, v in stored:
105 # atab.append(k, v[0])
106 # atab.append(k, v[0])
106
107
107 print "Total number of aliases:", len(aliases)
108 print "Total number of aliases:", len(aliases)
108 sys.stdout.flush()
109 sys.stdout.flush()
109 return aliases
110 return aliases
110
111
111 # Now try to define a new one
112 # Now try to define a new one
112 try:
113 try:
113 alias,cmd = par.split(None, 1)
114 alias,cmd = par.split(None, 1)
114 except:
115 except:
115 print oinspect.getdoc(self.alias)
116 print oinspect.getdoc(self.alias)
116 else:
117 else:
117 self.shell.alias_manager.soft_define_alias(alias, cmd)
118 self.shell.alias_manager.soft_define_alias(alias, cmd)
118 # end magic_alias
119 # end magic_alias
119
120
120 @line_magic
121 @line_magic
121 def unalias(self, parameter_s=''):
122 def unalias(self, parameter_s=''):
122 """Remove an alias"""
123 """Remove an alias"""
123
124
124 aname = parameter_s.strip()
125 aname = parameter_s.strip()
125 self.shell.alias_manager.undefine_alias(aname)
126 self.shell.alias_manager.undefine_alias(aname)
126 stored = self.shell.db.get('stored_aliases', {} )
127 stored = self.shell.db.get('stored_aliases', {} )
127 if aname in stored:
128 if aname in stored:
128 print "Removing %stored alias",aname
129 print "Removing %stored alias",aname
129 del stored[aname]
130 del stored[aname]
130 self.shell.db['stored_aliases'] = stored
131 self.shell.db['stored_aliases'] = stored
131
132
132 @line_magic
133 @line_magic
133 def rehashx(self, parameter_s=''):
134 def rehashx(self, parameter_s=''):
134 """Update the alias table with all executable files in $PATH.
135 """Update the alias table with all executable files in $PATH.
135
136
136 This version explicitly checks that every entry in $PATH is a file
137 This version explicitly checks that every entry in $PATH is a file
137 with execute access (os.X_OK), so it is much slower than %rehash.
138 with execute access (os.X_OK), so it is much slower than %rehash.
138
139
139 Under Windows, it checks executability as a match against a
140 Under Windows, it checks executability as a match against a
140 '|'-separated string of extensions, stored in the IPython config
141 '|'-separated string of extensions, stored in the IPython config
141 variable win_exec_ext. This defaults to 'exe|com|bat'.
142 variable win_exec_ext. This defaults to 'exe|com|bat'.
142
143
143 This function also resets the root module cache of module completer,
144 This function also resets the root module cache of module completer,
144 used on slow filesystems.
145 used on slow filesystems.
145 """
146 """
146 from IPython.core.alias import InvalidAliasError
147 from IPython.core.alias import InvalidAliasError
147
148
148 # for the benefit of module completer in ipy_completers.py
149 # for the benefit of module completer in ipy_completers.py
149 del self.shell.db['rootmodules']
150 del self.shell.db['rootmodules']
150
151
151 path = [os.path.abspath(os.path.expanduser(p)) for p in
152 path = [os.path.abspath(os.path.expanduser(p)) for p in
152 os.environ.get('PATH','').split(os.pathsep)]
153 os.environ.get('PATH','').split(os.pathsep)]
153 path = filter(os.path.isdir,path)
154 path = filter(os.path.isdir,path)
154
155
155 syscmdlist = []
156 syscmdlist = []
156 # Now define isexec in a cross platform manner.
157 # Now define isexec in a cross platform manner.
157 if os.name == 'posix':
158 if os.name == 'posix':
158 isexec = lambda fname:os.path.isfile(fname) and \
159 isexec = lambda fname:os.path.isfile(fname) and \
159 os.access(fname,os.X_OK)
160 os.access(fname,os.X_OK)
160 else:
161 else:
161 try:
162 try:
162 winext = os.environ['pathext'].replace(';','|').replace('.','')
163 winext = os.environ['pathext'].replace(';','|').replace('.','')
163 except KeyError:
164 except KeyError:
164 winext = 'exe|com|bat|py'
165 winext = 'exe|com|bat|py'
165 if 'py' not in winext:
166 if 'py' not in winext:
166 winext += '|py'
167 winext += '|py'
167 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
168 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
168 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
169 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
169 savedir = os.getcwdu()
170 savedir = os.getcwdu()
170
171
171 # Now walk the paths looking for executables to alias.
172 # Now walk the paths looking for executables to alias.
172 try:
173 try:
173 # write the whole loop for posix/Windows so we don't have an if in
174 # write the whole loop for posix/Windows so we don't have an if in
174 # the innermost part
175 # the innermost part
175 if os.name == 'posix':
176 if os.name == 'posix':
176 for pdir in path:
177 for pdir in path:
177 os.chdir(pdir)
178 os.chdir(pdir)
178 for ff in os.listdir(pdir):
179 for ff in os.listdir(pdir):
179 if isexec(ff):
180 if isexec(ff):
180 try:
181 try:
181 # Removes dots from the name since ipython
182 # Removes dots from the name since ipython
182 # will assume names with dots to be python.
183 # will assume names with dots to be python.
183 self.shell.alias_manager.define_alias(
184 self.shell.alias_manager.define_alias(
184 ff.replace('.',''), ff)
185 ff.replace('.',''), ff)
185 except InvalidAliasError:
186 except InvalidAliasError:
186 pass
187 pass
187 else:
188 else:
188 syscmdlist.append(ff)
189 syscmdlist.append(ff)
189 else:
190 else:
190 no_alias = self.shell.alias_manager.no_alias
191 no_alias = self.shell.alias_manager.no_alias
191 for pdir in path:
192 for pdir in path:
192 os.chdir(pdir)
193 os.chdir(pdir)
193 for ff in os.listdir(pdir):
194 for ff in os.listdir(pdir):
194 base, ext = os.path.splitext(ff)
195 base, ext = os.path.splitext(ff)
195 if isexec(ff) and base.lower() not in no_alias:
196 if isexec(ff) and base.lower() not in no_alias:
196 if ext.lower() == '.exe':
197 if ext.lower() == '.exe':
197 ff = base
198 ff = base
198 try:
199 try:
199 # Removes dots from the name since ipython
200 # Removes dots from the name since ipython
200 # will assume names with dots to be python.
201 # will assume names with dots to be python.
201 self.shell.alias_manager.define_alias(
202 self.shell.alias_manager.define_alias(
202 base.lower().replace('.',''), ff)
203 base.lower().replace('.',''), ff)
203 except InvalidAliasError:
204 except InvalidAliasError:
204 pass
205 pass
205 syscmdlist.append(ff)
206 syscmdlist.append(ff)
206 self.shell.db['syscmdlist'] = syscmdlist
207 self.shell.db['syscmdlist'] = syscmdlist
207 finally:
208 finally:
208 os.chdir(savedir)
209 os.chdir(savedir)
209
210
210 @skip_doctest
211 @skip_doctest
211 @line_magic
212 @line_magic
212 def pwd(self, parameter_s=''):
213 def pwd(self, parameter_s=''):
213 """Return the current working directory path.
214 """Return the current working directory path.
214
215
215 Examples
216 Examples
216 --------
217 --------
217 ::
218 ::
218
219
219 In [9]: pwd
220 In [9]: pwd
220 Out[9]: '/home/tsuser/sprint/ipython'
221 Out[9]: '/home/tsuser/sprint/ipython'
221 """
222 """
222 return os.getcwdu()
223 return os.getcwdu()
223
224
224 @skip_doctest
225 @skip_doctest
225 @line_magic
226 @line_magic
226 def cd(self, parameter_s=''):
227 def cd(self, parameter_s=''):
227 """Change the current working directory.
228 """Change the current working directory.
228
229
229 This command automatically maintains an internal list of directories
230 This command automatically maintains an internal list of directories
230 you visit during your IPython session, in the variable _dh. The
231 you visit during your IPython session, in the variable _dh. The
231 command %dhist shows this history nicely formatted. You can also
232 command %dhist shows this history nicely formatted. You can also
232 do 'cd -<tab>' to see directory history conveniently.
233 do 'cd -<tab>' to see directory history conveniently.
233
234
234 Usage:
235 Usage:
235
236
236 cd 'dir': changes to directory 'dir'.
237 cd 'dir': changes to directory 'dir'.
237
238
238 cd -: changes to the last visited directory.
239 cd -: changes to the last visited directory.
239
240
240 cd -<n>: changes to the n-th directory in the directory history.
241 cd -<n>: changes to the n-th directory in the directory history.
241
242
242 cd --foo: change to directory that matches 'foo' in history
243 cd --foo: change to directory that matches 'foo' in history
243
244
244 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
245 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
245 (note: cd <bookmark_name> is enough if there is no
246 (note: cd <bookmark_name> is enough if there is no
246 directory <bookmark_name>, but a bookmark with the name exists.)
247 directory <bookmark_name>, but a bookmark with the name exists.)
247 'cd -b <tab>' allows you to tab-complete bookmark names.
248 'cd -b <tab>' allows you to tab-complete bookmark names.
248
249
249 Options:
250 Options:
250
251
251 -q: quiet. Do not print the working directory after the cd command is
252 -q: quiet. Do not print the working directory after the cd command is
252 executed. By default IPython's cd command does print this directory,
253 executed. By default IPython's cd command does print this directory,
253 since the default prompts do not display path information.
254 since the default prompts do not display path information.
254
255
255 Note that !cd doesn't work for this purpose because the shell where
256 Note that !cd doesn't work for this purpose because the shell where
256 !command runs is immediately discarded after executing 'command'.
257 !command runs is immediately discarded after executing 'command'.
257
258
258 Examples
259 Examples
259 --------
260 --------
260 ::
261 ::
261
262
262 In [10]: cd parent/child
263 In [10]: cd parent/child
263 /home/tsuser/parent/child
264 /home/tsuser/parent/child
264 """
265 """
265
266
266 oldcwd = os.getcwdu()
267 oldcwd = os.getcwdu()
267 numcd = re.match(r'(-)(\d+)$',parameter_s)
268 numcd = re.match(r'(-)(\d+)$',parameter_s)
268 # jump in directory history by number
269 # jump in directory history by number
269 if numcd:
270 if numcd:
270 nn = int(numcd.group(2))
271 nn = int(numcd.group(2))
271 try:
272 try:
272 ps = self.shell.user_ns['_dh'][nn]
273 ps = self.shell.user_ns['_dh'][nn]
273 except IndexError:
274 except IndexError:
274 print 'The requested directory does not exist in history.'
275 print 'The requested directory does not exist in history.'
275 return
276 return
276 else:
277 else:
277 opts = {}
278 opts = {}
278 elif parameter_s.startswith('--'):
279 elif parameter_s.startswith('--'):
279 ps = None
280 ps = None
280 fallback = None
281 fallback = None
281 pat = parameter_s[2:]
282 pat = parameter_s[2:]
282 dh = self.shell.user_ns['_dh']
283 dh = self.shell.user_ns['_dh']
283 # first search only by basename (last component)
284 # first search only by basename (last component)
284 for ent in reversed(dh):
285 for ent in reversed(dh):
285 if pat in os.path.basename(ent) and os.path.isdir(ent):
286 if pat in os.path.basename(ent) and os.path.isdir(ent):
286 ps = ent
287 ps = ent
287 break
288 break
288
289
289 if fallback is None and pat in ent and os.path.isdir(ent):
290 if fallback is None and pat in ent and os.path.isdir(ent):
290 fallback = ent
291 fallback = ent
291
292
292 # if we have no last part match, pick the first full path match
293 # if we have no last part match, pick the first full path match
293 if ps is None:
294 if ps is None:
294 ps = fallback
295 ps = fallback
295
296
296 if ps is None:
297 if ps is None:
297 print "No matching entry in directory history"
298 print "No matching entry in directory history"
298 return
299 return
299 else:
300 else:
300 opts = {}
301 opts = {}
301
302
302
303
303 else:
304 else:
304 #turn all non-space-escaping backslashes to slashes,
305 #turn all non-space-escaping backslashes to slashes,
305 # for c:\windows\directory\names\
306 # for c:\windows\directory\names\
306 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
307 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
307 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
308 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
308 # jump to previous
309 # jump to previous
309 if ps == '-':
310 if ps == '-':
310 try:
311 try:
311 ps = self.shell.user_ns['_dh'][-2]
312 ps = self.shell.user_ns['_dh'][-2]
312 except IndexError:
313 except IndexError:
313 raise UsageError('%cd -: No previous directory to change to.')
314 raise UsageError('%cd -: No previous directory to change to.')
314 # jump to bookmark if needed
315 # jump to bookmark if needed
315 else:
316 else:
316 if not os.path.isdir(ps) or 'b' in opts:
317 if not os.path.isdir(ps) or 'b' in opts:
317 bkms = self.shell.db.get('bookmarks', {})
318 bkms = self.shell.db.get('bookmarks', {})
318
319
319 if ps in bkms:
320 if ps in bkms:
320 target = bkms[ps]
321 target = bkms[ps]
321 print '(bookmark:%s) -> %s' % (ps, target)
322 print '(bookmark:%s) -> %s' % (ps, target)
322 ps = target
323 ps = target
323 else:
324 else:
324 if 'b' in opts:
325 if 'b' in opts:
325 raise UsageError("Bookmark '%s' not found. "
326 raise UsageError("Bookmark '%s' not found. "
326 "Use '%%bookmark -l' to see your bookmarks." % ps)
327 "Use '%%bookmark -l' to see your bookmarks." % ps)
327
328
328 # strip extra quotes on Windows, because os.chdir doesn't like them
329 # strip extra quotes on Windows, because os.chdir doesn't like them
329 ps = unquote_filename(ps)
330 ps = unquote_filename(ps)
330 # at this point ps should point to the target dir
331 # at this point ps should point to the target dir
331 if ps:
332 if ps:
332 try:
333 try:
333 os.chdir(os.path.expanduser(ps))
334 os.chdir(os.path.expanduser(ps))
334 if hasattr(self.shell, 'term_title') and self.shell.term_title:
335 if hasattr(self.shell, 'term_title') and self.shell.term_title:
335 set_term_title('IPython: ' + abbrev_cwd())
336 set_term_title('IPython: ' + abbrev_cwd())
336 except OSError:
337 except OSError:
337 print sys.exc_info()[1]
338 print sys.exc_info()[1]
338 else:
339 else:
339 cwd = os.getcwdu()
340 cwd = os.getcwdu()
340 dhist = self.shell.user_ns['_dh']
341 dhist = self.shell.user_ns['_dh']
341 if oldcwd != cwd:
342 if oldcwd != cwd:
342 dhist.append(cwd)
343 dhist.append(cwd)
343 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
344 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
344
345
345 else:
346 else:
346 os.chdir(self.shell.home_dir)
347 os.chdir(self.shell.home_dir)
347 if hasattr(self.shell, 'term_title') and self.shell.term_title:
348 if hasattr(self.shell, 'term_title') and self.shell.term_title:
348 set_term_title('IPython: ' + '~')
349 set_term_title('IPython: ' + '~')
349 cwd = os.getcwdu()
350 cwd = os.getcwdu()
350 dhist = self.shell.user_ns['_dh']
351 dhist = self.shell.user_ns['_dh']
351
352
352 if oldcwd != cwd:
353 if oldcwd != cwd:
353 dhist.append(cwd)
354 dhist.append(cwd)
354 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
355 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
355 if not 'q' in opts and self.shell.user_ns['_dh']:
356 if not 'q' in opts and self.shell.user_ns['_dh']:
356 print self.shell.user_ns['_dh'][-1]
357 print self.shell.user_ns['_dh'][-1]
357
358
358
359
359 @line_magic
360 @line_magic
360 def env(self, parameter_s=''):
361 def env(self, parameter_s=''):
361 """List environment variables."""
362 """List environment variables."""
362
363
363 return dict(os.environ)
364 return dict(os.environ)
364
365
365 @line_magic
366 @line_magic
366 def pushd(self, parameter_s=''):
367 def pushd(self, parameter_s=''):
367 """Place the current dir on stack and change directory.
368 """Place the current dir on stack and change directory.
368
369
369 Usage:\\
370 Usage:\\
370 %pushd ['dirname']
371 %pushd ['dirname']
371 """
372 """
372
373
373 dir_s = self.shell.dir_stack
374 dir_s = self.shell.dir_stack
374 tgt = os.path.expanduser(unquote_filename(parameter_s))
375 tgt = os.path.expanduser(unquote_filename(parameter_s))
375 cwd = os.getcwdu().replace(self.shell.home_dir,'~')
376 cwd = os.getcwdu().replace(self.shell.home_dir,'~')
376 if tgt:
377 if tgt:
377 self.cd(parameter_s)
378 self.cd(parameter_s)
378 dir_s.insert(0,cwd)
379 dir_s.insert(0,cwd)
379 return self.shell.magic('dirs')
380 return self.shell.magic('dirs')
380
381
381 @line_magic
382 @line_magic
382 def popd(self, parameter_s=''):
383 def popd(self, parameter_s=''):
383 """Change to directory popped off the top of the stack.
384 """Change to directory popped off the top of the stack.
384 """
385 """
385 if not self.shell.dir_stack:
386 if not self.shell.dir_stack:
386 raise UsageError("%popd on empty stack")
387 raise UsageError("%popd on empty stack")
387 top = self.shell.dir_stack.pop(0)
388 top = self.shell.dir_stack.pop(0)
388 self.cd(top)
389 self.cd(top)
389 print "popd ->",top
390 print "popd ->",top
390
391
391 @line_magic
392 @line_magic
392 def dirs(self, parameter_s=''):
393 def dirs(self, parameter_s=''):
393 """Return the current directory stack."""
394 """Return the current directory stack."""
394
395
395 return self.shell.dir_stack
396 return self.shell.dir_stack
396
397
397 @line_magic
398 @line_magic
398 def dhist(self, parameter_s=''):
399 def dhist(self, parameter_s=''):
399 """Print your history of visited directories.
400 """Print your history of visited directories.
400
401
401 %dhist -> print full history\\
402 %dhist -> print full history\\
402 %dhist n -> print last n entries only\\
403 %dhist n -> print last n entries only\\
403 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
404 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
404
405
405 This history is automatically maintained by the %cd command, and
406 This history is automatically maintained by the %cd command, and
406 always available as the global list variable _dh. You can use %cd -<n>
407 always available as the global list variable _dh. You can use %cd -<n>
407 to go to directory number <n>.
408 to go to directory number <n>.
408
409
409 Note that most of time, you should view directory history by entering
410 Note that most of time, you should view directory history by entering
410 cd -<TAB>.
411 cd -<TAB>.
411
412
412 """
413 """
413
414
414 dh = self.shell.user_ns['_dh']
415 dh = self.shell.user_ns['_dh']
415 if parameter_s:
416 if parameter_s:
416 try:
417 try:
417 args = map(int,parameter_s.split())
418 args = map(int,parameter_s.split())
418 except:
419 except:
419 self.arg_err(self.dhist)
420 self.arg_err(self.dhist)
420 return
421 return
421 if len(args) == 1:
422 if len(args) == 1:
422 ini,fin = max(len(dh)-(args[0]),0),len(dh)
423 ini,fin = max(len(dh)-(args[0]),0),len(dh)
423 elif len(args) == 2:
424 elif len(args) == 2:
424 ini,fin = args
425 ini,fin = args
425 else:
426 else:
426 self.arg_err(self.dhist)
427 self.arg_err(self.dhist)
427 return
428 return
428 else:
429 else:
429 ini,fin = 0,len(dh)
430 ini,fin = 0,len(dh)
430 nlprint(dh,
431 nlprint(dh,
431 header = 'Directory history (kept in _dh)',
432 header = 'Directory history (kept in _dh)',
432 start=ini,stop=fin)
433 start=ini,stop=fin)
433
434
434 @skip_doctest
435 @skip_doctest
435 @line_magic
436 @line_magic
436 def sc(self, parameter_s=''):
437 def sc(self, parameter_s=''):
437 """Shell capture - execute a shell command and capture its output.
438 """Shell capture - execute a shell command and capture its output.
438
439
439 DEPRECATED. Suboptimal, retained for backwards compatibility.
440 DEPRECATED. Suboptimal, retained for backwards compatibility.
440
441
441 You should use the form 'var = !command' instead. Example:
442 You should use the form 'var = !command' instead. Example:
442
443
443 "%sc -l myfiles = ls ~" should now be written as
444 "%sc -l myfiles = ls ~" should now be written as
444
445
445 "myfiles = !ls ~"
446 "myfiles = !ls ~"
446
447
447 myfiles.s, myfiles.l and myfiles.n still apply as documented
448 myfiles.s, myfiles.l and myfiles.n still apply as documented
448 below.
449 below.
449
450
450 --
451 --
451 %sc [options] varname=command
452 %sc [options] varname=command
452
453
453 IPython will run the given command using commands.getoutput(), and
454 IPython will run the given command using commands.getoutput(), and
454 will then update the user's interactive namespace with a variable
455 will then update the user's interactive namespace with a variable
455 called varname, containing the value of the call. Your command can
456 called varname, containing the value of the call. Your command can
456 contain shell wildcards, pipes, etc.
457 contain shell wildcards, pipes, etc.
457
458
458 The '=' sign in the syntax is mandatory, and the variable name you
459 The '=' sign in the syntax is mandatory, and the variable name you
459 supply must follow Python's standard conventions for valid names.
460 supply must follow Python's standard conventions for valid names.
460
461
461 (A special format without variable name exists for internal use)
462 (A special format without variable name exists for internal use)
462
463
463 Options:
464 Options:
464
465
465 -l: list output. Split the output on newlines into a list before
466 -l: list output. Split the output on newlines into a list before
466 assigning it to the given variable. By default the output is stored
467 assigning it to the given variable. By default the output is stored
467 as a single string.
468 as a single string.
468
469
469 -v: verbose. Print the contents of the variable.
470 -v: verbose. Print the contents of the variable.
470
471
471 In most cases you should not need to split as a list, because the
472 In most cases you should not need to split as a list, because the
472 returned value is a special type of string which can automatically
473 returned value is a special type of string which can automatically
473 provide its contents either as a list (split on newlines) or as a
474 provide its contents either as a list (split on newlines) or as a
474 space-separated string. These are convenient, respectively, either
475 space-separated string. These are convenient, respectively, either
475 for sequential processing or to be passed to a shell command.
476 for sequential processing or to be passed to a shell command.
476
477
477 For example::
478 For example::
478
479
479 # Capture into variable a
480 # Capture into variable a
480 In [1]: sc a=ls *py
481 In [1]: sc a=ls *py
481
482
482 # a is a string with embedded newlines
483 # a is a string with embedded newlines
483 In [2]: a
484 In [2]: a
484 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
485 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
485
486
486 # which can be seen as a list:
487 # which can be seen as a list:
487 In [3]: a.l
488 In [3]: a.l
488 Out[3]: ['setup.py', 'win32_manual_post_install.py']
489 Out[3]: ['setup.py', 'win32_manual_post_install.py']
489
490
490 # or as a whitespace-separated string:
491 # or as a whitespace-separated string:
491 In [4]: a.s
492 In [4]: a.s
492 Out[4]: 'setup.py win32_manual_post_install.py'
493 Out[4]: 'setup.py win32_manual_post_install.py'
493
494
494 # a.s is useful to pass as a single command line:
495 # a.s is useful to pass as a single command line:
495 In [5]: !wc -l $a.s
496 In [5]: !wc -l $a.s
496 146 setup.py
497 146 setup.py
497 130 win32_manual_post_install.py
498 130 win32_manual_post_install.py
498 276 total
499 276 total
499
500
500 # while the list form is useful to loop over:
501 # while the list form is useful to loop over:
501 In [6]: for f in a.l:
502 In [6]: for f in a.l:
502 ...: !wc -l $f
503 ...: !wc -l $f
503 ...:
504 ...:
504 146 setup.py
505 146 setup.py
505 130 win32_manual_post_install.py
506 130 win32_manual_post_install.py
506
507
507 Similarly, the lists returned by the -l option are also special, in
508 Similarly, the lists returned by the -l option are also special, in
508 the sense that you can equally invoke the .s attribute on them to
509 the sense that you can equally invoke the .s attribute on them to
509 automatically get a whitespace-separated string from their contents::
510 automatically get a whitespace-separated string from their contents::
510
511
511 In [7]: sc -l b=ls *py
512 In [7]: sc -l b=ls *py
512
513
513 In [8]: b
514 In [8]: b
514 Out[8]: ['setup.py', 'win32_manual_post_install.py']
515 Out[8]: ['setup.py', 'win32_manual_post_install.py']
515
516
516 In [9]: b.s
517 In [9]: b.s
517 Out[9]: 'setup.py win32_manual_post_install.py'
518 Out[9]: 'setup.py win32_manual_post_install.py'
518
519
519 In summary, both the lists and strings used for output capture have
520 In summary, both the lists and strings used for output capture have
520 the following special attributes::
521 the following special attributes::
521
522
522 .l (or .list) : value as list.
523 .l (or .list) : value as list.
523 .n (or .nlstr): value as newline-separated string.
524 .n (or .nlstr): value as newline-separated string.
524 .s (or .spstr): value as space-separated string.
525 .s (or .spstr): value as space-separated string.
525 """
526 """
526
527
527 opts,args = self.parse_options(parameter_s, 'lv')
528 opts,args = self.parse_options(parameter_s, 'lv')
528 # Try to get a variable name and command to run
529 # Try to get a variable name and command to run
529 try:
530 try:
530 # the variable name must be obtained from the parse_options
531 # the variable name must be obtained from the parse_options
531 # output, which uses shlex.split to strip options out.
532 # output, which uses shlex.split to strip options out.
532 var,_ = args.split('=', 1)
533 var,_ = args.split('=', 1)
533 var = var.strip()
534 var = var.strip()
534 # But the command has to be extracted from the original input
535 # But the command has to be extracted from the original input
535 # parameter_s, not on what parse_options returns, to avoid the
536 # parameter_s, not on what parse_options returns, to avoid the
536 # quote stripping which shlex.split performs on it.
537 # quote stripping which shlex.split performs on it.
537 _,cmd = parameter_s.split('=', 1)
538 _,cmd = parameter_s.split('=', 1)
538 except ValueError:
539 except ValueError:
539 var,cmd = '',''
540 var,cmd = '',''
540 # If all looks ok, proceed
541 # If all looks ok, proceed
541 split = 'l' in opts
542 split = 'l' in opts
542 out = self.shell.getoutput(cmd, split=split)
543 out = self.shell.getoutput(cmd, split=split)
543 if 'v' in opts:
544 if 'v' in opts:
544 print '%s ==\n%s' % (var, pformat(out))
545 print '%s ==\n%s' % (var, pformat(out))
545 if var:
546 if var:
546 self.shell.user_ns.update({var:out})
547 self.shell.user_ns.update({var:out})
547 else:
548 else:
548 return out
549 return out
549
550
550 @line_magic
551 @line_magic
551 def sx(self, parameter_s=''):
552 def sx(self, parameter_s=''):
552 """Shell execute - run a shell command and capture its output.
553 """Shell execute - run a shell command and capture its output.
553
554
554 %sx command
555 %sx command
555
556
556 IPython will run the given command using commands.getoutput(), and
557 IPython will run the given command using commands.getoutput(), and
557 return the result formatted as a list (split on '\\n'). Since the
558 return the result formatted as a list (split on '\\n'). Since the
558 output is _returned_, it will be stored in ipython's regular output
559 output is _returned_, it will be stored in ipython's regular output
559 cache Out[N] and in the '_N' automatic variables.
560 cache Out[N] and in the '_N' automatic variables.
560
561
561 Notes:
562 Notes:
562
563
563 1) If an input line begins with '!!', then %sx is automatically
564 1) If an input line begins with '!!', then %sx is automatically
564 invoked. That is, while::
565 invoked. That is, while::
565
566
566 !ls
567 !ls
567
568
568 causes ipython to simply issue system('ls'), typing::
569 causes ipython to simply issue system('ls'), typing::
569
570
570 !!ls
571 !!ls
571
572
572 is a shorthand equivalent to::
573 is a shorthand equivalent to::
573
574
574 %sx ls
575 %sx ls
575
576
576 2) %sx differs from %sc in that %sx automatically splits into a list,
577 2) %sx differs from %sc in that %sx automatically splits into a list,
577 like '%sc -l'. The reason for this is to make it as easy as possible
578 like '%sc -l'. The reason for this is to make it as easy as possible
578 to process line-oriented shell output via further python commands.
579 to process line-oriented shell output via further python commands.
579 %sc is meant to provide much finer control, but requires more
580 %sc is meant to provide much finer control, but requires more
580 typing.
581 typing.
581
582
582 3) Just like %sc -l, this is a list with special attributes:
583 3) Just like %sc -l, this is a list with special attributes:
583 ::
584 ::
584
585
585 .l (or .list) : value as list.
586 .l (or .list) : value as list.
586 .n (or .nlstr): value as newline-separated string.
587 .n (or .nlstr): value as newline-separated string.
587 .s (or .spstr): value as whitespace-separated string.
588 .s (or .spstr): value as whitespace-separated string.
588
589
589 This is very useful when trying to use such lists as arguments to
590 This is very useful when trying to use such lists as arguments to
590 system commands."""
591 system commands."""
591
592
592 if parameter_s:
593 if parameter_s:
593 return self.shell.getoutput(parameter_s)
594 return self.shell.getoutput(parameter_s)
594
595
595
596
596 @line_magic
597 @line_magic
597 def bookmark(self, parameter_s=''):
598 def bookmark(self, parameter_s=''):
598 """Manage IPython's bookmark system.
599 """Manage IPython's bookmark system.
599
600
600 %bookmark <name> - set bookmark to current dir
601 %bookmark <name> - set bookmark to current dir
601 %bookmark <name> <dir> - set bookmark to <dir>
602 %bookmark <name> <dir> - set bookmark to <dir>
602 %bookmark -l - list all bookmarks
603 %bookmark -l - list all bookmarks
603 %bookmark -d <name> - remove bookmark
604 %bookmark -d <name> - remove bookmark
604 %bookmark -r - remove all bookmarks
605 %bookmark -r - remove all bookmarks
605
606
606 You can later on access a bookmarked folder with::
607 You can later on access a bookmarked folder with::
607
608
608 %cd -b <name>
609 %cd -b <name>
609
610
610 or simply '%cd <name>' if there is no directory called <name> AND
611 or simply '%cd <name>' if there is no directory called <name> AND
611 there is such a bookmark defined.
612 there is such a bookmark defined.
612
613
613 Your bookmarks persist through IPython sessions, but they are
614 Your bookmarks persist through IPython sessions, but they are
614 associated with each profile."""
615 associated with each profile."""
615
616
616 opts,args = self.parse_options(parameter_s,'drl',mode='list')
617 opts,args = self.parse_options(parameter_s,'drl',mode='list')
617 if len(args) > 2:
618 if len(args) > 2:
618 raise UsageError("%bookmark: too many arguments")
619 raise UsageError("%bookmark: too many arguments")
619
620
620 bkms = self.shell.db.get('bookmarks',{})
621 bkms = self.shell.db.get('bookmarks',{})
621
622
622 if 'd' in opts:
623 if 'd' in opts:
623 try:
624 try:
624 todel = args[0]
625 todel = args[0]
625 except IndexError:
626 except IndexError:
626 raise UsageError(
627 raise UsageError(
627 "%bookmark -d: must provide a bookmark to delete")
628 "%bookmark -d: must provide a bookmark to delete")
628 else:
629 else:
629 try:
630 try:
630 del bkms[todel]
631 del bkms[todel]
631 except KeyError:
632 except KeyError:
632 raise UsageError(
633 raise UsageError(
633 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
634 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
634
635
635 elif 'r' in opts:
636 elif 'r' in opts:
636 bkms = {}
637 bkms = {}
637 elif 'l' in opts:
638 elif 'l' in opts:
638 bks = bkms.keys()
639 bks = bkms.keys()
639 bks.sort()
640 bks.sort()
640 if bks:
641 if bks:
641 size = max(map(len, bks))
642 size = max(map(len, bks))
642 else:
643 else:
643 size = 0
644 size = 0
644 fmt = '%-'+str(size)+'s -> %s'
645 fmt = '%-'+str(size)+'s -> %s'
645 print 'Current bookmarks:'
646 print 'Current bookmarks:'
646 for bk in bks:
647 for bk in bks:
647 print fmt % (bk, bkms[bk])
648 print fmt % (bk, bkms[bk])
648 else:
649 else:
649 if not args:
650 if not args:
650 raise UsageError("%bookmark: You must specify the bookmark name")
651 raise UsageError("%bookmark: You must specify the bookmark name")
651 elif len(args)==1:
652 elif len(args)==1:
652 bkms[args[0]] = os.getcwdu()
653 bkms[args[0]] = os.getcwdu()
653 elif len(args)==2:
654 elif len(args)==2:
654 bkms[args[0]] = args[1]
655 bkms[args[0]] = args[1]
655 self.shell.db['bookmarks'] = bkms
656 self.shell.db['bookmarks'] = bkms
656
657
657 @line_magic
658 @line_magic
658 def pycat(self, parameter_s=''):
659 def pycat(self, parameter_s=''):
659 """Show a syntax-highlighted file through a pager.
660 """Show a syntax-highlighted file through a pager.
660
661
661 This magic is similar to the cat utility, but it will assume the file
662 This magic is similar to the cat utility, but it will assume the file
662 to be Python source and will show it with syntax highlighting.
663 to be Python source and will show it with syntax highlighting.
663
664
664 This magic command can either take a local filename, an url,
665 This magic command can either take a local filename, an url,
665 an history range (see %history) or a macro as argument ::
666 an history range (see %history) or a macro as argument ::
666
667
667 %pycat myscript.py
668 %pycat myscript.py
668 %pycat 7-27
669 %pycat 7-27
669 %pycat myMacro
670 %pycat myMacro
670 %pycat http://www.example.com/myscript.py
671 %pycat http://www.example.com/myscript.py
671 """
672 """
672
673
673 try :
674 try :
674 cont = self.shell.find_user_code(parameter_s)
675 cont = self.shell.find_user_code(parameter_s)
675 except ValueError, IOError:
676 except ValueError, IOError:
676 print "Error: no such file, variable, URL, history range or macro"
677 print "Error: no such file, variable, URL, history range or macro"
677 return
678 return
678
679
679 page.page(self.shell.pycolorize(cont))
680 page.page(self.shell.pycolorize(cont))
680
681
681 @magic_arguments.magic_arguments()
682 @magic_arguments.magic_arguments()
682 @magic_arguments.argument(
683 @magic_arguments.argument(
683 '-f', '--force', action='store_true', default=False,
684 '-f', '--force', action='store_true', default=False,
684 help='Whether to overwrite a file if it already exists'
685 help='Whether to overwrite a file if it already exists'
685 )
686 )
686 @magic_arguments.argument(
687 @magic_arguments.argument(
687 'filename', type=unicode,
688 'filename', type=unicode,
688 help='file to write'
689 help='file to write'
689 )
690 )
690 @cell_magic
691 @cell_magic
691 def file(self, line, cell):
692 def file(self, line, cell):
692 """Write a cell to a file
693 """Write a cell to a file
693
694
694 %%file [-f] filename
695 %%file [-f] filename
695
696
696 Writes the contents of the cell to a file.
697 Writes the contents of the cell to a file.
697
698
698 Will not overwrite existing files unless `-f` is specified.
699 Will not overwrite existing files unless `-f` is specified.
699 """
700 """
700 args = magic_arguments.parse_argstring(self.file, line)
701 args = magic_arguments.parse_argstring(self.file, line)
701 args.filename = unquote_filename(args.filename)
702 args.filename = unquote_filename(args.filename)
703 force = args.force
702
704
703 if os.path.exists(args.filename):
705 if os.path.exists(args.filename):
704 if args.force:
706 if not force:
705 print("Overwriting %s" % args.filename)
707 try:
706 else:
708 force = self.shell.ask_yes_no(
707 error("%s already exists! Force overwrite with %%%%file -f\n" % args.filename)
709 "%s exists, overwrite (y/[n])?",
710 default='n'
711 )
712 except StdinNotImplementedError:
713 force = True
714
715 if not force:
716 # prompted, but still no
717 print "Force overwrite with %%file -f " + args.filename
708 return
718 return
719
720 print("Overwriting %s" % args.filename)
709 else:
721 else:
710 print("Writing %s" % args.filename)
722 print("Writing %s" % args.filename)
711
723
712 with io.open(args.filename, 'w', encoding='utf-8') as f:
724 with io.open(args.filename, 'w', encoding='utf-8') as f:
713 f.write(cell)
725 f.write(cell)
General Comments 0
You need to be logged in to leave comments. Login now