diff rgToolFactory2.py @ 2:6a3c292412fa draft

Uploaded
author fubar
date Sun, 11 Jan 2015 21:32:15 -0500
parents c34063ab3735
children d75fd880fce2
line wrap: on
line diff
--- a/rgToolFactory2.py	Sat Jan 10 19:39:33 2015 -0500
+++ b/rgToolFactory2.py	Sun Jan 11 21:32:15 2015 -0500
@@ -121,7 +121,7 @@
 </tool_dependency>
 """
 
-toolhtmldepskel = """<?xml version="1.0"?>
+emptytoolhtmldepskel = """<?xml version="1.0"?>
 <tool_dependency>
         <readme>
            %s
@@ -230,48 +230,56 @@
             self.test1Inputs = ''
         # we always pass path,name pairs in using python optparse append
         # but the command line has to be different
-        self.infile_paths = ','.join([x.split(',')[0] for x in self.opts.input_tab])
-        self.infile_names = ','.join([x.split(',')[1] for x in self.opts.input_tab])
+        self.infile_paths = ''
+        self.infile_names = ''
+        if self.opts.input_tab:
+            self.infile_paths = ','.join([x.split(',')[0].strip() for x in self.opts.input_tab])
+            self.infile_names = ','.join([x.split(',')[1].strip() for x in self.opts.input_tab])
         if self.opts.interpreter == 'python':
             # yes, this is how additional parameters are always passed in python - to the TF itself and to
             # scripts to avoid having unknown parameter names (yes, they can be parsed but...) on the command line
-            a('--INPATHS "%s"' % (self.infile_paths)) 
-            a('--INNAMES "%s"' % (self.infile_names)) 
+            if self.opts.input_tab:
+                a('--INPATHS "%s"' % (self.infile_paths)) 
+                a('--INNAMES "%s"' % (self.infile_names)) 
             if self.opts.output_tab:
                 a('--OUTPATH "%s"' % self.opts.output_tab) 
             for p in opts.additional_parameters:
                 p = p.replace('"','')
-                psplit=p.split(',')
-                param = psplit[0]
-                value = psplit[1]
-                a('--additional_parameters "%s,%s"' % (param,value))
+                ps = p.split(',')
+                a('%s="%s"' % (ps[0],ps[1]))
         if (self.opts.interpreter == 'Rscript'):
             # pass params on command line
-            a('INPATHS "%s"' % self.infile_paths)
-            a('INNAMES "%s"' % self.infile_names)
+            if self.opts.input_tab:
+                a('INPATHS="%s"' % self.infile_paths)
+                a('INNAMES="%s"' % self.infile_names)
             if self.opts.output_tab:
-                a('OUTPATH "%s"' % self.opts.output_tab) 
-            for param in opts.additional_parameters:
-                param, value=param.split(',')
-                a('%s="%s"' % (param,value))
+                a('OUTPATH="%s"' % self.opts.output_tab) 
+            for p in opts.additional_parameters:
+                p = p.replace('"','')
+                ps = p.split(',')
+                a('%s="%s"' % (ps[0],ps[1]))
         if (self.opts.interpreter == 'perl'):
             # pass params on command line
-            a('%s' % self.infile_paths)
-            a('%s' % self.infile_names)
+            if self.opts.input_tab:
+                a('%s' % self.infile_paths)
+                a('%s' % self.infile_names)
             if self.opts.output_tab:
                 a('%s' % self.opts.output_tab)
-            for param in opts.additional_parameters:
-                param, value=param.split(',')
+            for p in opts.additional_parameters:
+                p = p.replace('"','')
+                ps = p.split(',')
+                param = ps[0]
+                value = ps[1]
                 if (value.find(' ') <> -1):
                     a('%s="%s"' % (param,value))
                 else:
                     a('%s=%s' % (param,value))
-                
         if self.opts.interpreter == 'sh' or self.opts.interpreter == 'bash':
               # more is better - now move all params into environment AND drop on to command line.
               self.cl.insert(0,'env')
-              self.cl.insert(1,'INPATHS=%s' % (self.infile_paths))
-              self.cl.insert(2,'INNAMES=%s' % (self.infile_names))
+              if self.opts.input_tab:
+                  self.cl.insert(1,'INPATHS=%s' % (self.infile_paths))
+                  self.cl.insert(2,'INNAMES=%s' % (self.infile_names))
               if self.opts.output_tab:
                   self.cl.insert(3,'OUTPATH=%s' % (self.opts.output_tab))
                   a('OUTPATH=%s' % (self.opts.output_tab))
@@ -415,9 +423,9 @@
         xdict['additionalInputs'] = ''
         if self.opts.additional_parameters:
             if self.opts.edit_additional_parameters: # add to new tool form with default value set to original value
-                xdict['additionalInputs'] = '\n'.join(['<param name="%s" value="%s" label="%s" help="%s" type="%s"/>' % (x.split(',')[0],x.split(',')[1],x.split(',')[2],
-                    x.split(',')[3], x.split(',')[4]) for x in self.opts.additional_parameters])
-            xdict['additionalParams'] = '\n'.join(['<param name="%s" value="%s" />' % (x.split(',')[0],x.split(',')[1]) for x in self.opts.additional_parameters])
+                xdict['additionalInputs'] = '\n'.join(['<param name="%s" value="%s" label="%s" help="%s" type="%s"/>' % (x.split(',')[0],html_escape(x.split(',')[1]),html_escape(x.split(',')[2]),
+                    html_escape(x.split(',')[3]), x.split(',')[4]) for x in self.opts.additional_parameters])
+            xdict['additionalParams'] = '\n'.join(['<param name="%s" value="%s" />' % (x.split(',')[0],html_escape(x.split(',')[1])) for x in self.opts.additional_parameters])
         xdict['requirements'] = ''
         if self.opts.make_HTML:
             if self.opts.include_dependencies == "yes":
@@ -455,10 +463,11 @@
             xdict['tooldesc'] = ''
         xdict['command_outputs'] = '' 
         xdict['outputs'] = '' 
-        if self.opts.input_tab <> 'None':
+        if self.opts.input_tab:
             cins = ['\n',]
+            cins.append('--input_formats %s' % self.opts.input_formats)
             cins.append('#for intab in $input1:')
-            cins.append('--input_tab "$intab,$intab.name"')
+            cins.append('--input_tab "${intab},${intab.name}"')
             cins.append('#end for\n')
             xdict['command_inputs'] = '\n'.join(cins)
             xdict['inputs'] = '''<param name="input_tab" multiple="true"  type="data" format="%s" label="Select one or more %s input files from your history"
@@ -469,12 +478,13 @@
         if (len(self.opts.additional_parameters) > 0):
             cins = ['\n',]
             for params in self.opts.additional_parameters:
-                if self.opts.edit_additional_parameters:
                     psplit = params.split(',') # name,value...
-                    psplit[1] = '$%s' % psplit[0] # replace with form value
-                    cins.append('--additional_parameters "%s"' % ','.join(psplit)) 
-                else:
-                    cins.append('--additional_parameters "%s"' % params)
+                    psplit[3] = html_escape(psplit[3])
+                    if self.opts.edit_additional_parameters:
+                        psplit[1] = '$%s' % psplit[0] # replace with form value
+                    else:
+                        psplit[1] = html_escape(psplit[1]) # leave prespecified value
+                    cins.append('--additional_parameters """%s"""' % ','.join(psplit)) 
             xdict['command_inputs'] = '%s\n%s' % (xdict['command_inputs'],'\n'.join(cins))
         xdict['inputs'] += '<param name="job_name" type="text" size="60" label="Supply a name for the outputs to remind you what they contain" value="%s"/> \n' % self.toolname
         xdict['toolname'] = self.toolname
@@ -520,20 +530,19 @@
         tdir = self.toolname
         os.mkdir(tdir)
         self.makeXML()
-        if self.opts.make_HTML:
-            if self.opts.help_text:
-                hlp = open(self.opts.help_text,'r').read()
-            else:
-                hlp = 'Please ask the tool author for help as none was supplied at tool generation\n'
-            if self.opts.include_dependencies == "yes":
-                tooldepcontent = toolhtmldepskel  % hlp
-            else:
-                tooldepcontent = emptytoolhtmldepskel  % hlp
-            depf = open(os.path.join(tdir,'tool_dependencies.xml'),'w')
-            depf.write(tooldepcontent)
-            depf.write('\n')
-            depf.close()
-        if self.opts.input_tab <> 'None': # no reproducible test otherwise? TODO: maybe..
+        if self.opts.help_text:
+            hlp = open(self.opts.help_text,'r').read()
+        else:
+            hlp = 'Please ask the tool author for help as none was supplied at tool generation\n'
+        if self.opts.include_dependencies == "yes":
+            tooldepcontent = toolhtmldepskel  % hlp
+        else:
+            tooldepcontent = emptytoolhtmldepskel  % hlp
+        depf = open(os.path.join(tdir,'tool_dependencies.xml'),'w')
+        depf.write(tooldepcontent)
+        depf.write('\n')
+        depf.close()
+        if self.opts.input_tab: # no reproducible test otherwise? TODO: maybe..
             testdir = os.path.join(tdir,'test-data')
             os.mkdir(testdir) # make tests directory
             for i,intab in enumerate(self.opts.input_tab):
@@ -544,7 +553,7 @@
                 dest = os.path.join(testdir,os.path.basename(si))
                 if si <> dest:
                     shutil.copyfile(si,dest)
-            if self.opts.output_tab <> None:
+            if self.opts.output_tab:
                 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output))
             if self.opts.make_HTML:
                 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML))