-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlindent.py
263 lines (228 loc) · 6.96 KB
/
xmlindent.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python3
#
# quick-n-dirty formatter for SPDX licenses in XML format
#
# Copyright © 2017 Alexios Zavras
# SPDX-License-Identifier: MIT
#
#-----------------------------------------------------------------
# configuration parameters, self-explanatory :-)
# they are simply defaults; can be overwritten by command-line options
INDENT = 2
LINE_LENGTH = 80
BACKUP_EXT = '.backup'
# which tags are inline and which appear on their own lines
TAGS_inline = [
'alt',
'b',
'br',
'copyright',
'url',
'crossRef',
'bullet',
]
TAGS_block = [
'body',
'header',
'li',
'license',
'list',
'notes',
'optional',
'p',
'SPDX',
'title',
'urls',
'SPDXLicenseCollection',
'license',
'crossRefs',
'standardLicenseHeader',
'notes',
'titleText',
'item',
]
# attributes for tags, in the order we want them to appear
ATTRS_SEQ = {
'SPDXLicenseCollection': [
'xmlns',
'prettyprinted',
],
'license': [
'licenseId',
'name',
'isOsiApproved',
],
'alt': [
'name',
'match',
],
}
# namespace for all tags
NAMESPACE='{http://www.spdx.org/license}'
#-----------------------------------------------------------------
VERSION = '1.0'
import argparse
import datetime
import re
import shutil
import sys
import warnings
import xml.etree.ElementTree as et
NL = '\n'
def process(fname):
backup(fname)
tree = et.parse(fname)
root = tree.getroot()
if root.tag == 'spdx':
root.tag = 'SPDX'
warning('changing root element to SPDX (capital letters)')
ts = '{:%Y%m%d%H%M%S%z}'.format(datetime.datetime.now())
root.set('prettyprinted', ts)
blocks = pretty(root, 0)
ser = fmt(blocks)
with open(fname, 'w') as f:
f.write(ser)
def pretty(node, level):
ser = ''
tag = node.tag
if tag.startswith(NAMESPACE):
tag = tag[len(NAMESPACE):]
text = singlespaceline(node.text)
tail = singlespaceline(node.tail)
# print("\t", level, tag, 'text=', text, 'tail=', tail, node.attrib)
start_tag = "<" + tag
if node.attrib:
for a in ATTRS_SEQ[tag]:
if a in node.attrib:
start_tag += ' {}="{}"'.format(a, node.attrib[a])
del node.attrib[a]
if node.attrib:
warning('more attrs remaining in {}: {}'.format(tag, node.attrib.keys()))
start_tag += ">"
end_tag = "</" + tag + ">"
if tag in config['block']:
child_level = level + 1
before = '{0}{1}#{2}{0}{3}#'.format(NL, level, start_tag, child_level)
after = '{0}{1}#{2}{0}'.format(NL, level, end_tag)
elif tag in config['inline']:
child_level = level
before = start_tag
after = '{1}{0}{2}#'.format(NL, end_tag, level)
else:
warning('Tag "{}" neither block nor inline!'.format(tag))
child_level = level
before = start_tag
after = end_tag
ser += before
if text:
ser += text
for child in node:
ser += pretty(child, child_level)
ser += after
if tail:
ser += tail
ser = ser.replace('\n\n', '\n')
return ser
def fmt(blocks):
bregexp = re.compile(r'((?P<level>\d+)#)?(?P<paragraph>.*)')
ser = ''
for line in blocks.split('\n'):
if line == '':
continue
m = bregexp.match(line)
if m.group('level'):
l = int(m.group('level'))
else:
warning('Block without level: "{}"'.format(line))
par = m.group('paragraph')
if par == '':
continue
indent = l * config['lvl_indent']
width = config['max_width'] - indent
for fmtline in to_lines(par, width):
ser += indent * ' ' + fmtline + '\n'
return ser
def to_lines(text, width):
words = text.split()
count = len(words)
last_offset = 0
offsets = [last_offset]
for w in words:
last_offset += len(w)
offsets.append(last_offset)
cost = [0] + [10 ** 20] * count
breaks = [0] + [0] * count
for i in range(count):
j = i + 1
while j <= count:
w = offsets[j] - offsets[i] + j - i - 1
if w > width:
break
penalty = cost[i] + (width - w) ** 2
if penalty < cost[j]:
cost[j] = penalty
breaks[j] = i
j += 1
lines = []
last = count
while last > 0:
first = breaks[last]
lines.append(' '.join(words[first:last]))
last = first
lines.reverse()
return lines
def singlespaceline(txt):
if txt:
txt = txt.strip()
txt = re.sub(r'\s+', ' ', txt)
return txt
def backup(fname):
if config['backup_ext']:
bak_fname = fname + config['backup_ext']
shutil.copy(fname, bak_fname)
def warning(msg, category=None):
warnings.warn(msg, category)
#-----------------------------------------------------------------
# main program
if NAMESPACE:
full_TAGS_inline = list(NAMESPACE+e for e in TAGS_inline)
full_TAGS_block = list(NAMESPACE+e for e in TAGS_block)
full_ATTRS_SEQ = dict((NAMESPACE+k, v) for k,v in ATTRS_SEQ.items())
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Indent XML file(s)')
parser.add_argument('filename', nargs='+',
help='the XML files to process')
parser.add_argument('-w', '--width', action='store', type=int,
default = LINE_LENGTH,
help='the maximum width of the lines in output')
parser.add_argument('-i', '--indent', action='store', type=int,
default = INDENT,
help='the number of spaces each level is indented')
parser.add_argument('-b', '--backup', action='store',
default = BACKUP_EXT,
help='the backup extension')
parser.add_argument('-B', '--nobackup', action='store_true',
help='do not keep a backup of the input file(s)')
parser.add_argument('--inline-tags', action='store',
help='space-separated list of tags to be rendered inline')
parser.add_argument('--block-tags', action='store',
help='space-separated list of tags to be rendered as blocks')
parser.add_argument('-V', '--version', action='version',
version='%(prog)s ' + VERSION,
help='print the program version')
args = parser.parse_args()
config = dict()
config['inline'] = TAGS_inline
config['block'] = TAGS_block
config['max_width'] = args.width
config['lvl_indent'] = args.indent
config['backup_ext'] = args.backup
if args.nobackup:
config['backup_ext'] = None
if args.inline_tags:
config['inline'] = args.inline_tags.split()
if args.block_tags:
config['block'] = args.block_tags.split()
for fname in args.filename:
process(fname)