|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# See file COPYING distributed with the xnnppx package for the copyright |
| 4 | +# and license. |
| 5 | + |
| 6 | +import sys |
| 7 | +import os |
| 8 | +try: |
| 9 | + import yaml |
| 10 | + have_yaml = True |
| 11 | +except: |
| 12 | + have_yaml = False |
| 13 | +import xml.dom.minidom |
| 14 | + |
| 15 | +def create_simple_element(doc, name, value): |
| 16 | + element = doc.createElement(name) |
| 17 | + element.appendChild(doc.createTextNode(value)) |
| 18 | + return element |
| 19 | + |
| 20 | +base_xml = """<?xml version="1.0" encoding="UTF-8"?> |
| 21 | +<Pipeline xmlns="http://nrg.wustl.edu/pipeline" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://nrg.wustl.edu/pipeline ..\schema\pipeline.xsd" xmlns:fileUtils="http://www.xnat.org/java/org.nrg.imagingtools.utils.FileUtils"> |
| 22 | +</Pipeline> |
| 23 | +""" |
| 24 | + |
| 25 | +progname = os.path.basename(sys.argv.pop(0)) |
| 26 | + |
| 27 | +if len(sys.argv) == 0 or len(sys.argv) > 2: |
| 28 | + print |
| 29 | + print 'usage: %s <source file> [output file]' % progname |
| 30 | + print |
| 31 | + print 'source file may be python or YAML (if available)' |
| 32 | + print |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | +in_fname = sys.argv.pop(0) |
| 36 | + |
| 37 | +try: |
| 38 | + data = open(in_fname).read() |
| 39 | +except IOError, data: |
| 40 | + sys.stderr.write('%s: %s\n' % (progname, str(data))) |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | +try: |
| 44 | + info = eval(data) |
| 45 | +except SyntaxError: |
| 46 | + if have_yaml: |
| 47 | + try: |
| 48 | + info = yaml.load(data) |
| 49 | + except yaml.scanner.ScannerError: |
| 50 | + sys.stderr.write("%s: couldn't interpret data in %s\n" % (progname, in_fname)) |
| 51 | + sys.exit(1) |
| 52 | + else: |
| 53 | + sys.stderr.write("%s: couldn't interpret data in %s\n" % (progname, in_fname)) |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | +doc = xml.dom.minidom.parseString(base_xml) |
| 57 | +pn = doc.getElementsByTagName('Pipeline')[0] |
| 58 | + |
| 59 | +pn.appendChild(create_simple_element(doc, 'name', info['name'])) |
| 60 | +pn.appendChild(create_simple_element(doc, 'location', info['location'])) |
| 61 | +pn.appendChild(create_simple_element(doc, 'description', info['description'])) |
| 62 | + |
| 63 | +documentation = doc.createElement('documentation') |
| 64 | +pn.appendChild(documentation) |
| 65 | + |
| 66 | +if info.has_key('authors'): |
| 67 | + authors = doc.createElement('authors') |
| 68 | + for fullname in info['authors']: |
| 69 | + a = doc.createElement('author') |
| 70 | + (lastname, firstname) = fullname.split(',', 1) |
| 71 | + a.appendChild(create_simple_element(doc, 'lastname', lastname)) |
| 72 | + a.appendChild(create_simple_element(doc, 'firstname', firstname)) |
| 73 | + authors.appendChild(a) |
| 74 | + documentation.appendChild(authors) |
| 75 | + |
| 76 | +if info.has_key('version'): |
| 77 | + documentation.appendChild(create_simple_element(doc, 'version', info['version'])) |
| 78 | + |
| 79 | +if info.has_key('input-parameters'): |
| 80 | + input_parameters = doc.createElement('input-parameters') |
| 81 | + for ip_d in info['input-parameters']: |
| 82 | + ip = doc.createElement('parameter') |
| 83 | + ip.appendChild(create_simple_element(doc, 'name', ip_d['name'])) |
| 84 | + vals = doc.createElement('values') |
| 85 | + if ip_d['values'][0] == 's': |
| 86 | + vals.appendChild(create_simple_element(doc, 'schemalink', ip_d['values'][1])) |
| 87 | + elif ip_d['values'][0] == 'v': |
| 88 | + vals.appendChild(create_simple_element(doc, 'csv', ip_d['values'][1])) |
| 89 | + else: |
| 90 | + sys.stderr.write('%s: bad value type \"%s\"\n' % (progname, ip_d['values'][0])) |
| 91 | + sys.exit(1) |
| 92 | + ip.appendChild(vals) |
| 93 | + ip.appendChild(create_simple_element(doc, 'description', ip_d['description'])) |
| 94 | + input_parameters.appendChild(ip) |
| 95 | + documentation.appendChild(input_parameters) |
| 96 | + |
| 97 | +if info.has_key('appliesTo'): |
| 98 | + applies_to = doc.createElement('xnatInfo') |
| 99 | + applies_to.setAttribute('appliesTo', info['appliesTo']) |
| 100 | + pn.appendChild(applies_to) |
| 101 | + |
| 102 | +parameters = doc.createElement('parameters') |
| 103 | +pn.appendChild(parameters) |
| 104 | +steps = doc.createElement('steps') |
| 105 | +pn.appendChild(steps) |
| 106 | +step = doc.createElement('step') |
| 107 | +step.setAttribute('id', '0') |
| 108 | +step.setAttribute('description', 'bogus step for validation') |
| 109 | +steps.appendChild(step) |
| 110 | + |
| 111 | +if sys.argv: |
| 112 | + try: |
| 113 | + fo = open(sys.argv[0], 'w') |
| 114 | + fo.write(doc.toxml()) |
| 115 | + fo.close() |
| 116 | + except IOError, data: |
| 117 | + sys.stderr.write('%s: %s\n' % (progname, str(data))) |
| 118 | + sys.exit(1) |
| 119 | +else: |
| 120 | + print doc.toxml() |
| 121 | + |
| 122 | +sys.exit(0) |
| 123 | + |
| 124 | +# eof |
0 commit comments