Added support for output redirection with --download (#104).

This commit is contained in:
Jakub Roztocil 2013-04-12 11:04:14 -03:00
parent 464b7a36da
commit 341272db1e
2 changed files with 16 additions and 2 deletions

View File

@ -149,6 +149,7 @@ class Download(object):
:type progress_file: file
"""
assert output_file, output_file
self._output_file = output_file
self._resume = resume
self._resumed_from = 0
@ -208,8 +209,11 @@ class Download(object):
else:
self._resumed_from = 0
self._output_file.seek(0)
self._output_file.truncate()
try:
self._output_file.seek(0)
self._output_file.truncate()
except IOError:
pass # stdout
else:
# TODO: Should the filename be taken from response.history[0].url?
# Output file not specified. Pick a name that doesn't exist yet.

View File

@ -140,13 +140,23 @@ class Parser(ArgumentParser):
Modify `env.stdout` and `env.stdout_isatty` based on args, if needed.
"""
if not self.env.stdout_isatty and self.args.output_file:
self.error('Cannot use --output, -o with redirected output.')
# FIXME: Come up with a cleaner solution.
if self.args.download:
if not self.env.stdout_isatty:
# Use stdout as tge download output file.
self.args.output_file = self.env.stdout
# With `--download`, we write everything that would normally go to
# `stdout` to `stderr` instead. Let's replace the stream so that
# we don't have to use many `if`s throughout the codebase.
# The response body will be treated separately.
self.env.stdout = self.env.stderr
self.env.stdout_isatty = self.env.stderr_isatty
elif self.args.output_file:
# When not `--download`ing, then `--output` simply replaces
# `stdout`. The file is opened for appending, which isn't what