Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print XML #114

Merged
merged 1 commit into from Jun 2, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions httpie/output.py
Expand Up @@ -2,6 +2,7 @@

"""
import json
import xml.dom.minidom
from functools import partial
from itertools import chain

Expand All @@ -20,6 +21,9 @@
OUT_RESP_HEAD, OUT_RESP_BODY)


# The default number of spaces to indent when pretty printing
DEFAULT_INDENT = 4

# Colors on Windows via colorama don't look that
# great and fruity seems to give the best result there.
AVAILABLE_STYLES = set(STYLE_MAP.keys())
Expand Down Expand Up @@ -385,13 +389,28 @@ def process_body(self, content, content_type, subtype):
content = json.dumps(json.loads(content),
sort_keys=True,
ensure_ascii=False,
indent=4)
indent=DEFAULT_INDENT)
except ValueError:
# Invalid JSON but we don't care.
pass
return content


class XMLProcessor(BaseProcessor):
"""XML body processor."""

def process_body(self, content, content_type, subtype):
if subtype == 'xml':
try:
# Pretty print the XML
doc = xml.dom.minidom.parseString(content)
content = doc.toprettyxml(indent=' '*DEFAULT_INDENT)
except xml.parsers.expat.ExpatError:
# Ignore invalid XML errors (skips attempting to pretty print)
pass
return content


class PygmentsProcessor(BaseProcessor):
"""A processor that applies syntax-highlighting using Pygments
to the headers, and to the body as well if its content type is recognized.
Expand Down Expand Up @@ -456,7 +475,8 @@ class OutputProcessor(object):
installed_processors = {
'format': [
HeadersProcessor,
JSONProcessor
JSONProcessor,
XMLProcessor
],
'colors': [
PygmentsProcessor
Expand Down