forked from populse/capsul
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_capsul_xml.py
More file actions
233 lines (218 loc) · 9.56 KB
/
convert_capsul_xml.py
File metadata and controls
233 lines (218 loc) · 9.56 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
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
# Temporary script to ease pipeline conversion. It will be removed when
# Capsul XML 2.0 is validated.
import sys
import re
import xmltodict
import subprocess
from StringIO import StringIO
import tempfile
from collections import OrderedDict
import codecs
from ast import literal_eval
type_map = {
'Int': 'int',
'Float': 'float',
'Str': 'string',
'String': 'string',
'Bool': 'bool',
'Any': 'any',
'File': 'file',
'Directory': 'directory',
'List_File': 'list_file',
}
def get_type(type, dict):
if type in type_map:
return type_map[type]
elif type == 'List':
return 'list_%s' % get_type(dict.pop('@content'), None)
elif type.startswith('['):
return '|'.join(get_type(i, {}) for i in literal_eval(type))
else:
raise ValueError('Unsuported parameter type: %s' % type)
def convert_unit(unit):
process = OrderedDict((('@capsul_xml', '2.0'),))
result = {'process': process}
for tag, content in unit['unit'].iteritems():
if not isinstance(content,list):
content = [content]
if tag in ('input', 'output'):
elems = process.setdefault(tag,[])
for c in content:
elem = {'@name': c.pop('@name')}
elems.append(elem)
type = c.pop('@type')
elem['@type'] = get_type(type, c)
optional = c.pop('@optional', None)
if optional is not None:
elem['@optional'] = ('true' if optional == 'True' else 'false')
doc = c.pop('@description', None)
if doc is None:
doc = c.pop('@desc', None)
if doc:
doc = ' '.join(doc.split())
elem['@doc'] = doc
if c:
raise ValueError('Invalid keys in <%s>: %s' % (tag, ', '.join(c.keys())))
else:
raise ValueError('Invalid key in <unit>: %s' % tag)
return result
def convert_pipeline(source_pipeline):
#return source_pipeline
pipeline = OrderedDict((('@capsul_xml', '2.0'),))
result = {'pipeline': pipeline}
for tag, content in source_pipeline['pipeline'].iteritems():
if not isinstance(content,list):
content = [content]
if tag == 'docstring':
if len(content) > 1:
raise ValueError('Only one <%s> allowed' % tag)
pipeline['doc'] = content[0]
elif tag == 'units':
if len(content) > 1:
raise ValueError('Only one <%s> allowed' % tag)
c = content[0]
unit = c.pop('unit')
if not isinstance(unit, list):
unit = [unit]
processes = []
for u in unit:
module = u.pop('module')
if module.endswith('.xml'):
module = module[:-4]
process = OrderedDict((
('@name', u.pop('@name')),
('@module', module)))
processes.append(process)
set = u.pop('set',[])
if not isinstance(set, list):
set = [set]
for s in set:
name = s.pop('@name')
process.setdefault('set',[]).append({'@name': name, '@value': s.pop('@value')})
copyfile = s.pop('@copyfile', None)
usedefault = s.pop('@usedefault', None)
nipype = OrderedDict((('@name', name),))
if copyfile:
if copyfile == 'True':
nipype['@copyfile'] = 'true'
elif copyfile == 'Temp':
nipype['@copyfile'] = 'discard'
else:
raise 'Invalid copyfile value: %s' % copyfile
if usedefault:
if usedefault == 'True':
nipype['@usedefault'] = 'true'
else:
raise 'Invalid usedefault value: %s' % usedefault
if copyfile or usedefault:
process.setdefault('nipype',[]).append(nipype)
if s:
raise ValueError('Invalid keys in <set>: %s' % ', '.join(s.keys()))
iterinput = u.pop('iterinput', [])
if not isinstance(iterinput, list):
iterinput = [iterinput]
iteroutput = u.pop('iteroutput', [])
if not isinstance(iteroutput, list):
iteroutput = [iteroutput]
iteration = [i['@name'] for i in iterinput] + [i['@name'] for i in iteroutput]
if iteration:
process['iterate'] = [{'@name': i} for i in iteration]
if u:
raise ValueError('Invalid keys in <unit>: %s' % ', '.join(u.keys()))
pipeline.setdefault('process',[]).extend(processes)
switch = c.pop('switch', None)
if switch:
if not isinstance(switch, list):
switch = [switch]
selections = pipeline.setdefault('processes_selection',[])
for s in switch:
selection = {'@name': s.pop('@name')}
selections.append(selection)
path = s.pop('path',[])
if not isinstance(path, list):
path = [path]
groups = selection.setdefault('processes_group', [])
for p in path:
group = {'@name': p.pop('@name')}
groups.append(group)
unit = p.pop('unit',[])
if not isinstance(unit, list):
unit = [unit]
processes = group.setdefault('process', [])
for u in unit:
processes.append({'@name': u.pop('@name')})
if u:
raise ValueError('Invalid keys in <unit> (inside <path>): %s' % ', '.join(u.keys()))
if p:
raise ValueError('Invalid keys in <path>: %s' % ', '.join(p.keys()))
if s:
raise ValueError('Invalid keys in <switch>: %s' % ', '.join(c.keys()))
if c:
raise ValueError('Invalid keys in <%s>: %s' % (tag, ', '.join(c.keys())))
elif tag == 'links':
if len(content) > 1:
raise ValueError('Only one <%s> allowed' % tag)
c = content[0]
link = c.pop('link')
if not isinstance(link, list):
link = [link]
for p in link:
p['@dest'] = p.pop('@destination')
pipeline.setdefault('link',[]).extend(link)
if c:
raise ValueError('Invalid keys in <%s>: %s' % (tag, ', '.join(c.keys())))
elif tag == 'positions':
if len(content) > 1:
raise ValueError('Only one <%s> allowed' % tag)
c = content[0]
position = c.pop('position')
if not isinstance(position, list):
position = [position]
for p in position:
p['@name'] = p.pop('@unit')
pipeline.setdefault('gui',{})['position'] = position
if c:
raise ValueError('Invalid keys in <%s>: %s' % (tag, ', '.join(c.keys())))
elif tag =='scale':
if len(content) > 1:
raise ValueError('Only one <%s> allowed' % tag)
c = content[0]
factor = c.pop('@factor')
pipeline.setdefault('gui',{}).setdefault('zoom',{})['@level'] = factor
if c:
raise ValueError('Invalid keys in <%s>: %s' % (tag, ', '.join(c.keys())))
elif tag =='zoom':
if len(content) > 1:
raise ValueError('Only one <%s> allowed' % tag)
c = content[0]
level = c.pop('@level')
pipeline.setdefault('gui',{}).setdefault('zoom',{})['@level'] = level
if c:
raise ValueError('Invalid keys in <%s>: %s' % (tag, ', '.join(c.keys())))
elif tag == '@version':
pass
else:
raise ValueError('Invalid key in <pipeline>: %s' % tag)
return result
def process_file(input_file_name, output_file_name, main_tag, conversion_function, indent=''):
file_content = codecs.open(input_file_name, encoding='utf-8').read()
result = []
last_end = 0
for match in re.finditer(r'<{0}.*?</{0}>'.format(main_tag), file_content, re.DOTALL):
unit = xmltodict.parse(file_content[match.start():match.end()])
xml = xmltodict.unparse(conversion_function(unit))
# Prettify XML with xmllint command
tmp = tempfile.mktemp()
open(tmp,'w').write(xml)
xml = subprocess.check_output(['xmllint', '--pretty', '1', tmp])
xml = xml[xml.find('\n'):].replace('\n', '\n%s'% indent)
result.append(file_content[last_end:match.start()])
result.append(xml)
last_end = match.end()
result.append(file_content[last_end:])
output_file = codecs.open(output_file_name, 'w', encoding='utf-8')
output_file.write(''.join(result))
if sys.argv[1].endswith('.py'):
process_file(sys.argv[1], sys.argv[2], 'unit', convert_unit, indent=' ')
elif sys.argv[1].endswith('.xml'):
process_file(sys.argv[1], sys.argv[2], 'pipeline', convert_pipeline)