-
Notifications
You must be signed in to change notification settings - Fork 3
/
xml_generator.py
168 lines (132 loc) · 7.67 KB
/
xml_generator.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
import xml.etree.ElementTree as ET
from xml.dom import minidom
# Return a pretty-printed XML string for the Element.
class XMLGenerator():
def __init__(self, module_name):
self.module_name = module_name
# Read in template xml into string to and remove newlines for proper formatting.
with open('xml_template', "r") as xml_template:
xml_string = xml_template.read().replace("\n", "")
# Build xml tree from formatted string.
self.root = ET.fromstring(xml_string)
# Set module name in xml
self.xml_set_module_name()
def write_xml(self):
INDENT = " "
rough_string = ET.tostring(self.root)
reparsed = minidom.parseString(rough_string)
# prettified_xmlStr is a byte object in order to include the encoding
prettified_xmlStr = reparsed.toprettyxml(indent=INDENT, newl='\n', encoding='utf-8')
# prettified_xmlStr = prettify(my_root)
# print(prettified_xmlStr.decode().replace('\n\n',''))
with open(self.module_name + '.xml', "w") as output_file:
# Change byte object to string
str_xml = prettified_xmlStr.decode()
output_file.write(str_xml)
def xml_set_module_name(self):
self.root.attrib['name'] = self.module_name
# <tag name="inputs">
# <tag name="table_url" type="resource">
# <template>
# <tag name="accepted_type" value="table" />
# <tag name="accepted_type" value="dataset" />
# <tag name="label" value="Table to extract metadata" />
# <tag name="prohibit_upload" value="true" type="boolean" />
# </template>
# </tag>
# <tag name="reducer_url" type="resource">
# <template>
# <tag name="label" value="Select a PyTorch Model" />
# <tag name="accepted_type" value="file" />
# <tag name="prohibit_upload" value="true" />
# <tag name="query" value="filename:*.pt" />
# <tag name="example_query" value="default_reducer:true" />
# <tag name="example_type" value="file" />
# </template>
# </tag>
def add_input(self, type, input_name=None):
# input_var_name = '_'.join(input_name.split()).lower()
for child in self.root:
if child.attrib['name'] == 'inputs': #TODO Format the various input_resource types correctly
if type == 'image':
input_name_tag = ET.SubElement(child, 'tag', attrib={'name': input_name, 'type': 'resource'})
template_tag = ET.SubElement(input_name_tag, 'template')
ET.SubElement(template_tag, 'tag', attrib={'name': 'label', 'value': input_name})
ET.SubElement(template_tag, 'tag', attrib={'name': 'accepted_type', 'value': 'image'})
# ET.SubElement(template_tag, 'tag', attrib={'name': 'accepted_type', 'value': 'dataset'})
ET.SubElement(template_tag, 'tag', attrib={'name': 'prohibit_upload', 'value': 'True'})
elif type == 'file':
input_name_tag = ET.SubElement(child, 'tag', attrib={'name': input_name, 'type': 'resource'})
template_tag = ET.SubElement(input_name_tag, 'template')
ET.SubElement(template_tag, 'tag', attrib={'name': 'label', 'value': input_name})
ET.SubElement(template_tag, 'tag', attrib={'name': 'accepted_type', 'value': 'file'})
ET.SubElement(template_tag, 'tag', attrib={'name': 'prohibit_upload', 'value': 'True'})
elif type == 'table':
input_name_tag = ET.SubElement(child, 'tag', attrib={'name': input_name, 'type': 'resource'})
template_tag = ET.SubElement(input_name_tag, 'template')
ET.SubElement(template_tag, 'tag', attrib={'name': 'label', 'value': input_name})
ET.SubElement(template_tag, 'tag', attrib={'name': 'accepted_type', 'value': 'table'})
# ET.SubElement(template_tag, 'tag', attrib={'name': 'accepted_type', 'value': 'dataset'})
ET.SubElement(template_tag, 'tag', attrib={'name': 'prohibit_upload', 'value': 'True'})
elif type == 'mex':
ET.SubElement(child, 'tag', attrib={'name': 'mex_url', 'type': 'system-input_resource'})
elif type == 'bisque_token':
ET.SubElement(child, 'tag', attrib={'name': 'bisque_token', 'type': 'system-input_resource'})
def add_output(self, output_name, type):
# output_var_name = '_'.join(output_name.split()).lower()
for child in self.root:
if child.attrib['name'] == 'outputs':
if type == 'image':
output_name_tag = ET.SubElement(child, 'tag', attrib={'name': output_name, 'type': 'image'})
template_tag = ET.SubElement(output_name_tag, 'template')
ET.SubElement(template_tag, 'tag', attrib={'name': 'label', 'value': output_name})
else:
template_tag = child.find(".//*[@name='NonImage']/template")
ET.SubElement(template_tag, 'tag', attrib={'name': output_name, 'type':type})
# print(template_tag)
# print('\n\n')
#
# a = child.find(".//*[@name='Metadata']/template")
# print(a)
#
# print('\n\n')
# for c in child:
# if c.attrib['name'] == 'Metadata':
# print(list(c))
#
# # template_tag = c.getchildre()
#
#
# print(dir(c))
# if type == 'table':
# output_name_tag = ET.SubElement(child, 'tag', attrib={'name': output_var_name, 'type': 'table'})
# template_tag = ET.SubElement(output_name_tag, 'template')
# ET.SubElement(template_tag, 'tag', attrib={'name': 'label', 'value': output_name})
# if type == 'file':
# output_name_tag = ET.SubElement(child, 'tag', attrib={'name': output_var_name, 'type': 'resource'})
# template_tag = ET.SubElement(output_name_tag, 'template')
# ET.SubElement(template_tag, 'tag', attrib={'name': 'label', 'value': output_name})
def edit_xml(self, field, value):
for child in self.root:
if field == 'title' and child.attrib['name'] == 'title':
child.attrib['value'] = value
elif field == 'authors' and child.attrib['name'] == 'authors':
child.attrib['value'] = value
elif field == 'description' and child.attrib['name'] == 'description':
child.attrib['value'] = value
if __name__ == '__main__':
module_name = 'outputtagtest'
authors = 'Ivan'
description = 'Edge detector'
BQ_module_xml = XMLGenerator(module_name)
BQ_module_xml.xml_set_module_name()
BQ_module_xml.add_input('Np in', 'file')
# BQ_module_xml.edit_xml('inputs', 'image')
BQ_module_xml.edit_xml('inputs', 'mex')
BQ_module_xml.edit_xml('inputs', 'bisque_token')
# BQ_module_xml.edit_xml('outputs', 'image', out_name='Edge Image')
BQ_module_xml.add_output('Np out', 'file')
BQ_module_xml.edit_xml('title', module_name)
BQ_module_xml.edit_xml('authors', authors)
BQ_module_xml.edit_xml('description', description)
BQ_module_xml.write_xml()