forked from extern/httpie-cli
Use the full capability of HttpLexer
This commit is contained in:
parent
2d7f2c65a2
commit
4aa86cb438
@ -1,8 +1,11 @@
|
||||
#!/usr/bin/env python
|
||||
import sys
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
from requests.compat import str
|
||||
|
||||
from . import httpmessage
|
||||
from . import cliparse
|
||||
from . import cli
|
||||
@ -81,13 +84,14 @@ def _get_output(args, stdout_isatty, response):
|
||||
do_output_response = (cliparse.OUT_RESP_HEADERS in args.output_options
|
||||
or cliparse.OUT_RESP_BODY in args.output_options)
|
||||
|
||||
prettifier = pretty.PrettyHttp(args.style) if do_prettify else None
|
||||
formatter = pretty.formatter(args.style) if do_prettify else None
|
||||
output = []
|
||||
|
||||
if do_output_request:
|
||||
output.append(httpmessage.format(
|
||||
message=httpmessage.from_request(response.request),
|
||||
prettifier=prettifier,
|
||||
pretty=do_prettify,
|
||||
formatter=formatter,
|
||||
with_headers=cliparse.OUT_REQ_HEADERS in args.output_options,
|
||||
with_body=cliparse.OUT_REQ_BODY in args.output_options
|
||||
))
|
||||
@ -97,7 +101,8 @@ def _get_output(args, stdout_isatty, response):
|
||||
if do_output_response:
|
||||
output.append(httpmessage.format(
|
||||
message=httpmessage.from_response(response),
|
||||
prettifier=prettifier,
|
||||
pretty=do_prettify,
|
||||
formatter=formatter,
|
||||
with_headers=cliparse.OUT_RESP_HEADERS in args.output_options,
|
||||
with_body=cliparse.OUT_RESP_BODY in args.output_options
|
||||
))
|
||||
|
@ -1,3 +1,7 @@
|
||||
import json
|
||||
|
||||
from pygments import highlight
|
||||
from pygments.lexers import HttpLexer
|
||||
from requests.compat import urlparse
|
||||
|
||||
|
||||
@ -38,29 +42,32 @@ def from_response(response):
|
||||
return HTTPMessage(
|
||||
line='HTTP/{version} {status} {reason}'.format(
|
||||
version='.'.join(str(original.version)),
|
||||
status=original.status, reason=original.reason,),
|
||||
status=original.status, reason=original.reason),
|
||||
headers=str(original.msg),
|
||||
body=response.content.decode(encoding) if response.content else '',
|
||||
content_type=response_headers.get('Content-Type'))
|
||||
|
||||
|
||||
def format(message, prettifier=None,
|
||||
def format(message, formatter=None, pretty=False,
|
||||
with_headers=True, with_body=True):
|
||||
"""Return a `unicode` representation of `message`. """
|
||||
bits = []
|
||||
if with_headers:
|
||||
if prettifier:
|
||||
bits.append(prettifier.headers(message.line))
|
||||
bits.append(prettifier.headers(message.headers))
|
||||
else:
|
||||
bits.append(message.line)
|
||||
bits.append(message.headers)
|
||||
bits.append(message.line)
|
||||
bits.append(message.headers)
|
||||
if with_body and message.body:
|
||||
bits.append('\n')
|
||||
if with_body and message.body:
|
||||
if prettifier and message.content_type:
|
||||
bits.append(prettifier.body(message.body, message.content_type))
|
||||
if (pretty and message.content_type and
|
||||
message.content_type.split(';')[0] == 'application/json'):
|
||||
# Indent and sort the JSON data.
|
||||
bits.append(json.dumps(json.loads(message.body),
|
||||
sort_keys=True, indent=4))
|
||||
else:
|
||||
bits.append(message.body)
|
||||
bits.append('\n')
|
||||
return '\n'.join(bit.strip() for bit in bits)
|
||||
result = '\n'.join(bit.strip() for bit in bits)
|
||||
if pretty:
|
||||
return highlight(result, HttpLexer(), formatter)
|
||||
|
||||
return result
|
||||
|
@ -1,12 +1,15 @@
|
||||
import os
|
||||
import json
|
||||
|
||||
import pygments
|
||||
|
||||
from pygments import token
|
||||
from pygments.util import ClassNotFound
|
||||
from pygments.lexers import get_lexer_for_mimetype, HttpLexer
|
||||
from pygments.formatters.terminal256 import Terminal256Formatter
|
||||
from pygments.formatters.terminal import TerminalFormatter
|
||||
from pygments.styles import get_style_by_name, STYLE_MAP
|
||||
from pygments.lexers import get_lexer_for_mimetype, HttpLexer
|
||||
from pygments.formatters.terminal import TerminalFormatter
|
||||
from pygments.formatters.terminal256 import Terminal256Formatter
|
||||
|
||||
from . import solarized
|
||||
|
||||
|
||||
@ -16,32 +19,12 @@ FORMATTER = (Terminal256Formatter
|
||||
if '256color' in os.environ.get('TERM', '')
|
||||
else TerminalFormatter)
|
||||
|
||||
class PrettyHttp(object):
|
||||
|
||||
def __init__(self, style_name):
|
||||
if style_name == 'solarized':
|
||||
style = solarized.SolarizedStyle
|
||||
else:
|
||||
style = get_style_by_name(style_name)
|
||||
self.formatter = FORMATTER(style=style)
|
||||
def formatter(style_name):
|
||||
if style_name == 'solarized':
|
||||
style = solarized.SolarizedStyle
|
||||
|
||||
def headers(self, content):
|
||||
return pygments.highlight(content, HttpLexer(), self.formatter)
|
||||
else:
|
||||
style = get_style_by_name(style_name)
|
||||
|
||||
def body(self, content, content_type):
|
||||
content_type = content_type.split(';')[0]
|
||||
|
||||
try:
|
||||
lexer = get_lexer_for_mimetype(content_type)
|
||||
except ClassNotFound:
|
||||
return content
|
||||
|
||||
if 'json' in content_type:
|
||||
try:
|
||||
# Indent the JSON data.
|
||||
content = json.dumps(json.loads(content),
|
||||
sort_keys=True, indent=4)
|
||||
except:
|
||||
pass
|
||||
|
||||
return pygments.highlight(content, lexer, self.formatter)
|
||||
return FORMATTER(style=style)
|
||||
|
Loading…
Reference in New Issue
Block a user