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