1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-28 08:38:44 +02:00

Refactoring

This commit is contained in:
Jakub Roztocil 2016-02-29 15:00:17 +08:00
parent 5408fb0fb9
commit ed08ab133e
4 changed files with 46 additions and 49 deletions

View File

@ -20,12 +20,13 @@ from pygments import __version__ as pygments_version
from httpie import __version__ as httpie_version, ExitStatus
from httpie.compat import str, bytes, is_py3
from httpie.client import get_response
from httpie.downloads import Download
from httpie.downloads import Downloader
from httpie.context import Environment
from httpie.plugins import plugin_manager
from httpie.output.streams import (
build_output_stream,
write, write_with_colors_win_py3
write_stream,
write_stream_with_colors_win_py3
)
@ -98,21 +99,20 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
if args == ['--debug']:
return exit_status
download = None
downloader = None
try:
args = parser.parse_args(args=args, env=env)
if args.download:
args.follow = True # --download implies --follow.
download = Download(
downloader = Downloader(
output_file=args.output_file,
progress_file=env.stderr,
resume=args.download_resume
)
download.pre_request(args.headers)
downloader.pre_request(args.headers)
last_response = get_response(args, config_dir=env.config.directory)
if args.show_redirects:
responses = last_response.history + [last_response]
else:
@ -120,36 +120,33 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
for response in responses:
if exit_status != ExitStatus.OK:
break
if args.check_status or download:
if args.check_status or downloader:
exit_status = get_exit_status(
http_status=response.status_code,
follow=args.follow
)
if not env.stdout_isatty and exit_status != ExitStatus.OK:
error('HTTP %s %s',
response.raw.status,
response.raw.reason,
level='warning')
write_kwargs = {
'stream': build_output_stream(args, env,
response.request,
response),
# This will in fact be `stderr` with `--download`
write_stream_kwargs = {
'stream': build_output_stream(
args=args,
env=env,
request=response.request,
response=response,
),
# NOTE: `env.stdout` will in fact be `stderr` with `--download`
'outfile': env.stdout,
'flush': env.stdout_isatty or args.stream
}
try:
if env.is_windows and is_py3 and 'colors' in args.prettify:
write_with_colors_win_py3(**write_kwargs)
write_stream_with_colors_win_py3(**write_stream_kwargs)
else:
write(**write_kwargs)
write_stream(**write_stream_kwargs)
except IOError as e:
if not traceback and e.errno == errno.EPIPE:
# Ignore broken pipes unless --traceback.
@ -157,20 +154,20 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
else:
raise
if download and exit_status == ExitStatus.OK:
if downloader and exit_status == ExitStatus.OK:
# Last response body download.
download_stream, download_to = download.start(last_response)
write(
download_stream, download_to = downloader.start(last_response)
write_stream(
stream=download_stream,
outfile=download_to,
flush=False,
)
download.finish()
if download.interrupted:
downloader.finish()
if downloader.interrupted:
exit_status = ExitStatus.ERROR
error('Incomplete download: size=%d; downloaded=%d' % (
download.status.total_size,
download.status.downloaded
downloader.status.total_size,
downloader.status.downloaded
))
except KeyboardInterrupt:
@ -204,7 +201,7 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
exit_status = ExitStatus.ERROR
finally:
if download and not download.finished:
download.failed()
if downloader and not downloader.finished:
downloader.failed()
return exit_status

View File

@ -144,7 +144,7 @@ def get_unique_filename(filename, exists=os.path.exists):
attempt += 1
class Download(object):
class Downloader(object):
def __init__(self, output_file=None,
resume=False, progress_file=sys.stderr):

View File

@ -24,7 +24,7 @@ class BinarySuppressedError(Exception):
message = BINARY_SUPPRESSED_NOTICE
def write(stream, outfile, flush):
def write_stream(stream, outfile, flush):
"""Write the output stream."""
try:
# Writing bytes so we use the buffer interface (Python 3).
@ -38,7 +38,7 @@ def write(stream, outfile, flush):
outfile.flush()
def write_with_colors_win_py3(stream, outfile, flush):
def write_stream_with_colors_win_py3(stream, outfile, flush):
"""Like `write`, but colorized chunks are written as text
directly to `outfile` to ensure it gets processed by colorama.
Applies only to Windows with Python 3 and colorized terminal output.

View File

@ -7,7 +7,7 @@ from requests.structures import CaseInsensitiveDict
from httpie.compat import urlopen
from httpie.downloads import (
parse_content_range, filename_from_content_disposition, filename_from_url,
get_unique_filename, ContentRangeError, Download,
get_unique_filename, ContentRangeError, Downloader,
)
from utils import http, TestEnvironment
@ -106,34 +106,34 @@ class TestDownloads:
def test_download_with_Content_Length(self, httpbin):
devnull = open(os.devnull, 'w')
download = Download(output_file=devnull, progress_file=devnull)
download.start(Response(
downloader = Downloader(output_file=devnull, progress_file=devnull)
downloader.start(Response(
url=httpbin.url + '/',
headers={'Content-Length': 10}
))
time.sleep(1.1)
download.chunk_downloaded(b'12345')
downloader.chunk_downloaded(b'12345')
time.sleep(1.1)
download.chunk_downloaded(b'12345')
download.finish()
assert not download.interrupted
downloader.chunk_downloaded(b'12345')
downloader.finish()
assert not downloader.interrupted
def test_download_no_Content_Length(self, httpbin):
devnull = open(os.devnull, 'w')
download = Download(output_file=devnull, progress_file=devnull)
download.start(Response(url=httpbin.url + '/'))
downloader = Downloader(output_file=devnull, progress_file=devnull)
downloader.start(Response(url=httpbin.url + '/'))
time.sleep(1.1)
download.chunk_downloaded(b'12345')
download.finish()
assert not download.interrupted
downloader.chunk_downloaded(b'12345')
downloader.finish()
assert not downloader.interrupted
def test_download_interrupted(self, httpbin):
devnull = open(os.devnull, 'w')
download = Download(output_file=devnull, progress_file=devnull)
download.start(Response(
downloader = Downloader(output_file=devnull, progress_file=devnull)
downloader.start(Response(
url=httpbin.url + '/',
headers={'Content-Length': 5}
))
download.chunk_downloaded(b'1234')
download.finish()
assert download.interrupted
downloader.chunk_downloaded(b'1234')
downloader.finish()
assert downloader.interrupted