Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #88 from t2y/fix-print-on-windows
Browse files Browse the repository at this point in the history
Fixed to output html on Windows avoiding print() function
  • Loading branch information
Lukasa committed Feb 23, 2015
2 parents 700e6b5 + b51870c commit 201086f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
15 changes: 10 additions & 5 deletions hyper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Command line interface for Hyper inspired by Httpie.
"""
import json
import locale
import logging
import sys
from argparse import ArgumentParser, RawTextHelpFormatter
Expand All @@ -15,12 +16,12 @@

from hyper import HTTP20Connection
from hyper import __version__
from hyper.compat import urlencode, urlsplit
from hyper.compat import is_py2, urlencode, urlsplit, write_to_stdout


log = logging.getLogger('hyper')

FILESYSTEM_ENCODING = sys.getfilesystemencoding()
PREFERRED_ENCODING = locale.getpreferredencoding()

# Various separators used in args
SEP_HEADERS = ':'
Expand Down Expand Up @@ -160,13 +161,16 @@ def set_request_data(args):
elif i.sep == SEP_QUERY:
params[i.key] = i.value
elif i.sep == SEP_DATA:
body[i.key] = i.value
value = i.value
if is_py2: # pragma: no cover
value = value.decode(PREFERRED_ENCODING)
body[i.key] = value

if params:
args.url.path += '?' + urlencode(params)

if body:
content_type = 'application/json; charset=%s' % FILESYSTEM_ENCODING
content_type = 'application/json'
headers.setdefault('content-type', content_type)
args.body = json.dumps(body)

Expand Down Expand Up @@ -224,7 +228,8 @@ def request(args):
def main(argv=None):
args = parse_argument(argv)
log.debug('Commandline Argument: %s', args)
print(request(args))
data = request(args)
write_to_stdout(data.encode(PREFERRED_ENCODING, errors='replace'))


if __name__ == '__main__': # pragma: no cover
Expand Down
8 changes: 8 additions & 0 deletions hyper/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def to_byte(char):
def decode_hex(b):
return b.decode('hex')

def write_to_stdout(data):
sys.stdout.write(data + '\n')
sys.stdout.flush()

# The standard zlib.compressobj() accepts only positional arguments.
def zlib_compressobj(level=6, method=zlib.DEFLATED, wbits=15, memlevel=8,
strategy=zlib.Z_DEFAULT_STRATEGY):
Expand All @@ -57,6 +61,10 @@ def to_byte(char):
def decode_hex(b):
return bytes.fromhex(b)

def write_to_stdout(data):
sys.stdout.buffer.write(data + b'\n')
sys.stdout.buffer.flush()

zlib_compressobj = zlib.compressobj

if is_py3_3:
Expand Down
4 changes: 2 additions & 2 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from hyper.cli import FILESYSTEM_ENCODING as FENC, KeyValue
from hyper.cli import KeyValue
from hyper.cli import get_content_type_and_charset, main, parse_argument
from hyper.cli import set_request_data, set_url_info

Expand Down Expand Up @@ -155,7 +155,7 @@ def test_get_content_type_and_charset(response, expected):
KeyValue('data2', 'test2', '=', ''),
]}
),
{'headers': {'content-type': 'application/json; charset=%s' % FENC},
{'headers': {'content-type': 'application/json'},
'method': 'POST',
'body': json.dumps({'data1': 'test1', 'data2': 'test2'}),
}
Expand Down

0 comments on commit 201086f

Please sign in to comment.