-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathformatters.py
More file actions
111 lines (85 loc) · 2.87 KB
/
Copy pathformatters.py
File metadata and controls
111 lines (85 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import json
import xml.dom.minidom
from StringIO import StringIO
class Formatter(object):
def __init__(self, args=None):
self.args = args
def format(text):
pass
class JsonFormatter(Formatter):
def __init__(self, args=None):
super(JsonFormatter, self).__init__(args)
def format(self, text):
formatted = None
try:
o = json.loads(text)
formatted = json.dumps(o, indent=2)
except (TypeError, ValueError):
formatted = text
return formatted
# under Python <= 2.7.2 the minidom output is gnarly, should be fixed in 2.7.3+
# http://bugs.python.org/issue4147
class XmlFormatter(Formatter):
def __init__(self, args=None):
super(XmlFormatter, self).__init__(args)
# for the time being this big time workaround will do it
def format_xml(self, node, writer, indent="", addindent="", newl=""):
# minidom likes to treat whitepace as text nodes
if node.nodeType == xml.dom.minidom.Node.TEXT_NODE and node.data.strip() == "":
return
writer.write(indent + "<" + node.tagName)
attrs = node.attributes
keys = sorted(attrs.keys())
for key in keys:
writer.write(" %s=\"" % key)
writer.write(attrs[key].value)
writer.write("\"")
if node.childNodes:
writer.write(">")
if all(map(lambda n: n.nodeType == xml.dom.minidom.Node.TEXT_NODE,
node.childNodes)):
for child in node.childNodes:
child.writexml(writer, "", "", "")
else:
writer.write(newl)
for child in node.childNodes:
self.format_xml(child, writer, indent + addindent,
addindent, newl)
writer.write(indent)
writer.write("</%s>%s" % (node.tagName, newl))
else:
writer.write("/>%s" % (newl))
def format(self, text):
formatted = None
try:
x = xml.dom.minidom.parseString(text)
writer = StringIO()
self.format_xml(x.childNodes[0], writer, addindent=" ", newl="\n")
formatted = writer.getvalue()
x.unlink()
except:
formatted = text
return formatted
JSONTYPES = (
'application/json',
'application/x-javascript',
'text/javascript',
'text/x-javascript',
'text/x-json')
XMLTYPES = (
'application/xml',
'application/atom+xml',
'application/mathml+xml',
'application/rss+xml',
'application/xhtml+xml',
'text/xml')
def format_by_mimetype(text, mimetype):
formatter = None
if mimetype in JSONTYPES:
formatter = JsonFormatter()
elif mimetype in XMLTYPES:
formatter = XmlFormatter()
if formatter:
return formatter.format(text)
else:
return text