1
0
mirror of https://github.com/httpie/cli.git synced 2025-03-31 21:55:16 +02:00

Fallback to media subtype if the type is uknown.

Closes #81.
This commit is contained in:
Jakub Roztocil 2012-08-01 17:35:32 +02:00
parent 90d34ffd0d
commit 00d85a4b97
2 changed files with 43 additions and 25 deletions

View File

@ -7,7 +7,7 @@ import json
import pygments import pygments
from pygments import token, lexer from pygments import token, lexer
from pygments.styles import get_style_by_name, STYLE_MAP from pygments.styles import get_style_by_name, STYLE_MAP
from pygments.lexers import get_lexer_for_mimetype from pygments.lexers import get_lexer_for_mimetype, get_lexer_by_name
from pygments.formatters.terminal import TerminalFormatter from pygments.formatters.terminal import TerminalFormatter
from pygments.formatters.terminal256 import Terminal256Formatter from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.util import ClassNotFound from pygments.util import ClassNotFound
@ -162,24 +162,30 @@ class BaseProcessor(object):
def process_headers(self, headers): def process_headers(self, headers):
return headers return headers
def process_body(self, content, content_type): def process_body(self, content, content_type, subtype):
"""Return processed `content`.
:param content: `str`
:param content_type: full content type, e.g., 'application/atom+xml'
:param subtype: e.g., 'xml'
"""
return content return content
class JSONProcessor(BaseProcessor): class JSONProcessor(BaseProcessor):
def process_body(self, content, content_type): def process_body(self, content, content_type, subtype):
if content_type == 'application/json': if subtype == 'json':
try: try:
# Indent and sort the JSON data. # Indent the JSON data, sort keys by name, and
content = json.dumps( # avoid unicode escapes to improve readability.
json.loads(content), content = json.dumps(json.loads(content),
sort_keys=True, sort_keys=True,
ensure_ascii=False, ensure_ascii=False,
indent=4, indent=4)
)
except ValueError: except ValueError:
# Invalid JSON - we don't care. # Invalid JSON but we don't care.
pass pass
return content return content
@ -209,9 +215,12 @@ class PygmentsProcessor(BaseProcessor):
return pygments.highlight( return pygments.highlight(
headers, HTTPLexer(), self.formatter) headers, HTTPLexer(), self.formatter)
def process_body(self, content, content_type): def process_body(self, content, content_type, subtype):
try: try:
lexer = get_lexer_for_mimetype(content_type) try:
lexer = get_lexer_for_mimetype(content_type)
except ClassNotFound:
lexer = get_lexer_by_name(subtype)
except ClassNotFound: except ClassNotFound:
pass pass
else: else:
@ -235,22 +244,16 @@ class OutputProcessor(object):
def process_headers(self, headers): def process_headers(self, headers):
for processor in self.processors: for processor in self.processors:
headers = processor.process_headers(headers) headers = processor.process_headers(headers)
return headers return headers
def process_body(self, content, content_type): def process_body(self, content, content_type):
# e.g., 'application/atom+xml'
content_type = content_type.split(';')[0] content_type = content_type.split(';')[0]
# e.g., 'xml'
application_match = re.match( subtype = content_type.split('/')[-1].split('+')[-1]
r'application/(.+\+)(json|xml)$',
content_type
)
if application_match:
# Strip vendor and extensions from Content-Type
vendor, extension = application_match.groups()
content_type = content_type.replace(vendor, '')
for processor in self.processors: for processor in self.processors:
content = processor.process_body(content, content_type) content = processor.process_body(content, content_type, subtype)
return content return content

View File

@ -504,6 +504,21 @@ class PrettyFlagTest(BaseTestCase):
) )
self.assertNotIn(TERMINAL_COLOR_PRESENCE_CHECK, r) self.assertNotIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
def test_subtype_based_pygments_lexer_match(self):
"""Test that media subtype is used if type/subtype doesn't
match any lexer.
"""
r = http(
'--print=B',
'--pretty',
httpbin('/post'),
'Content-Type:text/foo+json',
'a=b',
env=Environment()
)
self.assertIn(TERMINAL_COLOR_PRESENCE_CHECK, r)
class VerboseFlagTest(BaseTestCase): class VerboseFlagTest(BaseTestCase):