22import logging
33from lxml import etree
44from galaxyxml import Util , GalaxyXML
5- from galaxyxml .tool .parameters import XMLParam
5+ from galaxyxml .tool .parameters import (
6+ Import ,
7+ Inputs ,
8+ Macro ,
9+ Macros ,
10+ Outputs ,
11+ XMLParam
12+ )
613
714VALID_TOOL_TYPES = ('data_source' , 'data_source_async' )
815VALID_URL_METHODS = ('get' , 'post' )
@@ -15,8 +22,9 @@ class Tool(GalaxyXML):
1522
1623 def __init__ (self , name , id , version , description , executable , hidden = False ,
1724 tool_type = None , URL_method = None , workflow_compatible = True ,
18- interpreter = None , version_command = 'interpreter filename.exe --version' ):
19-
25+ interpreter = None , version_command = 'interpreter filename.exe --version' ,
26+ macros = None ):
27+ self .id = id
2028 self .executable = executable
2129 self .interpreter = interpreter
2230 kwargs = {
@@ -53,6 +61,15 @@ def __init__(self, name, id, version, description, executable, hidden=False,
5361
5462 description_node = etree .SubElement (self .root , 'description' )
5563 description_node .text = description
64+ if macros :
65+ self .macros = Macros ()
66+ if isinstance (macros , list ):
67+ for m in macros :
68+ self .macros .append (Import (m ))
69+ else :
70+ self .macros .append (Import (macros ))
71+ self .inputs = Inputs ()
72+ self .outputs = Outputs ()
5673
5774 def add_comment (self , comment_txt ):
5875 comment = etree .Comment (comment_txt )
@@ -80,8 +97,11 @@ def clean_command_string(self, command_line):
8097 return '\n ' .join (clean )
8198
8299 def export (self , keep_old_command = False ): # noqa
83-
84100 export_xml = copy .deepcopy (self )
101+ try :
102+ export_xml .append (export_xml .macros )
103+ except Exception :
104+ pass
85105
86106 try :
87107 export_xml .append (export_xml .edam_operations )
@@ -108,6 +128,7 @@ def export(self, keep_old_command=False): # noqa
108128 command_line .append (export_xml .inputs .cli ())
109129 except Exception as e :
110130 logger .warning (str (e ))
131+ raise
111132
112133 try :
113134 command_line .append (export_xml .outputs .cli ())
@@ -165,3 +186,55 @@ def export(self, keep_old_command=False): # noqa
165186 pass
166187
167188 return super (Tool , export_xml ).export ()
189+
190+
191+ class MacrosTool (Tool ):
192+
193+ def __init__ (self , * args , ** kwargs ):
194+ super (MacrosTool , self ).__init__ (* args , ** kwargs )
195+ self .root = etree .Element ('macros' )
196+ self .inputs = Macro ("%s_inmacro" % self .id )
197+ self .outputs = Macro ("%s_outmacro" % self .id )
198+
199+
200+ def export (self , keep_old_command = False ): # noqa
201+
202+ export_xml = copy .deepcopy (self )
203+
204+ try :
205+ for child in export_xml .macros :
206+ export_xml .append (child )
207+ except Exception :
208+ pass
209+
210+ command_line = []
211+ try :
212+ command_line .append (export_xml .inputs .cli ())
213+ except Exception as e :
214+ logger .warning (str (e ))
215+
216+ # Add command section
217+ command_node = etree .SubElement (export_xml .root , 'token' , {"name" : "%s_INMACRO" % self .id .upper ()})
218+ actual_cli = "%s" % (export_xml .clean_command_string (command_line ))
219+ command_node .text = etree .CDATA (actual_cli .strip ())
220+
221+ command_line = []
222+ try :
223+ command_line .append (export_xml .outputs .cli ())
224+ except Exception :
225+ pass
226+ command_node = etree .SubElement (export_xml .root , 'token' , {"name" : "%s_OUTMACRO" % self .id .upper ()})
227+ actual_cli = "%s" % (export_xml .clean_command_string (command_line ))
228+ command_node .text = etree .CDATA (actual_cli .strip ())
229+
230+ try :
231+ export_xml .append (export_xml .inputs )
232+ except Exception :
233+ pass
234+
235+ try :
236+ export_xml .append (export_xml .outputs )
237+ except Exception :
238+ pass
239+
240+ return super (Tool , export_xml ).export ()
0 commit comments