annotate toolfactory/galaxyxml/tool/__init__.py @ 35:5d38cb3d9be8 draft

added patched galaxyxml code temporarily until PR accepted
author fubar
date Sat, 08 Aug 2020 19:55:55 -0400
parents
children ce2b1f8ea68d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
1 import copy
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
2 import logging
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
3 from lxml import etree
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
4 from galaxyxml import Util, GalaxyXML
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
5 from galaxyxml.tool.parameters import XMLParam
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
6
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
7 VALID_TOOL_TYPES = ('data_source', 'data_source_async')
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
8 VALID_URL_METHODS = ('get', 'post')
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
9
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
10 logging.basicConfig(level=logging.INFO)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
11 logger = logging.getLogger(__name__)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
12
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
13
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
14 class Tool(GalaxyXML):
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
15
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
16 def __init__(self, name, id, version, description, executable, hidden=False,
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
17 tool_type=None, URL_method=None, workflow_compatible=True,
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
18 interpreter=None, version_command='interpreter filename.exe --version',
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
19 command_line_override=None):
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
20
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
21 self.executable = executable
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
22 self.interpreter = interpreter
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
23 self.command_line_override = command_line_override
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
24 kwargs = {
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
25 'name': name,
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
26 'id': id,
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
27 'version': version,
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
28 'hidden': hidden,
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
29 'workflow_compatible': workflow_compatible,
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
30 }
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
31 self.version_command = version_command
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
32
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
33 # Remove some of the default values to make tools look a bit nicer
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
34 if not hidden:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
35 del kwargs['hidden']
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
36 if workflow_compatible:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
37 del kwargs['workflow_compatible']
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
38
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
39 kwargs = Util.coerce(kwargs)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
40 self.root = etree.Element('tool', **kwargs)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
41
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
42 if tool_type is not None:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
43 if tool_type not in VALID_TOOL_TYPES:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
44 raise Exception("Tool type must be one of %s" %
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
45 ','.join(VALID_TOOL_TYPES))
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
46 else:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
47 kwargs['tool_type'] = tool_type
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
48
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
49 if URL_method is not None:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
50 if URL_method in VALID_URL_METHODS:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
51 kwargs['URL_method'] = URL_method
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
52 else:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
53 raise Exception("URL_method must be one of %s" %
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
54 ','.join(VALID_URL_METHODS))
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
55
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
56 description_node = etree.SubElement(self.root, 'description')
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
57 description_node.text = description
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
58
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
59 def add_comment(self, comment_txt):
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
60 comment = etree.Comment(comment_txt)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
61 self.root.insert(0, comment)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
62
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
63 def append_version_command(self):
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
64 version_command = etree.SubElement(self.root, 'version_command')
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
65 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
66 version_command.text = etree.CDATA(self.version_command)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
67 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
68 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
69
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
70 def append(self, sub_node):
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
71 if issubclass(type(sub_node), XMLParam):
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
72 self.root.append(sub_node.node)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
73 else:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
74 self.root.append(sub_node)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
75
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
76 def clean_command_string(self, command_line):
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
77 clean = []
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
78 for x in command_line:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
79 if x is not [] and x is not ['']:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
80 clean.append(x)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
81
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
82 return '\n'.join(clean)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
83
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
84 def export(self, keep_old_command=False): # noqa
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
85
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
86 export_xml = copy.deepcopy(self)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
87
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
88 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
89 export_xml.append(export_xml.edam_operations)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
90 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
91 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
92
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
93 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
94 export_xml.append(export_xml.edam_topics)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
95 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
96 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
97
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
98 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
99 export_xml.append(export_xml.requirements)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
100 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
101 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
102
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
103 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
104 export_xml.append(export_xml.configfiles)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
105 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
106 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
107
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
108 if self.command_line_override != None:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
109 command_line = self.command_line_override
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
110 else:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
111 command_line = []
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
112 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
113 command_line.append(export_xml.inputs.cli())
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
114 except Exception as e:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
115 logger.warning(str(e))
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
116
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
117 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
118 command_line.append(export_xml.outputs.cli())
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
119 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
120 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
121
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
122 # Add stdio section
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
123 stdio = etree.SubElement(export_xml.root, 'stdio')
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
124 etree.SubElement(stdio, 'exit_code', range='1:', level='fatal')
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
125
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
126 # Append version command
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
127 export_xml.append_version_command()
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
128
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
129 # Steal interpreter from kwargs
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
130 command_kwargs = {}
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
131 if export_xml.interpreter is not None:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
132 command_kwargs['interpreter'] = export_xml.interpreter
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
133
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
134 # Add command section
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
135 command_node = etree.SubElement(export_xml.root, 'command', **command_kwargs)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
136
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
137 if keep_old_command:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
138 if getattr(self, 'command', None):
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
139 command_node.text = etree.CDATA(export_xml.command)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
140 else:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
141 logger.warning('The tool does not have any old command stored. ' +
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
142 'Only the command line is written.')
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
143 command_node.text = export_xml.executable
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
144 else:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
145 actual_cli = "%s %s" % (
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
146 export_xml.executable, export_xml.clean_command_string(command_line))
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
147 command_node.text = etree.CDATA(actual_cli.strip())
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
148
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
149 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
150 export_xml.append(export_xml.inputs)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
151 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
152 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
153
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
154 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
155 export_xml.append(export_xml.outputs)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
156 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
157 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
158
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
159 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
160 export_xml.append(export_xml.tests)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
161 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
162 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
163
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
164 help_element = etree.SubElement(export_xml.root, 'help')
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
165 help_element.text = etree.CDATA(export_xml.help)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
166
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
167 try:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
168 export_xml.append(export_xml.citations)
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
169 except Exception:
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
170 pass
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
171
5d38cb3d9be8 added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff changeset
172 return super(Tool, export_xml).export()