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