comparison toolfactory/rgToolFactory2.py @ 108:e878bfc6b80c draft

Uploaded
author fubar
date Sun, 29 Nov 2020 04:32:22 +0000
parents 18e616939a91
children 0116bfe0fa5e
comparison
equal deleted inserted replaced
107:18e616939a91 108:e878bfc6b80c
70 IFMTPOS = 2 70 IFMTPOS = 2
71 ILABPOS = 3 71 ILABPOS = 3
72 IHELPOS = 4 72 IHELPOS = 4
73 IOCLPOS = 5 73 IOCLPOS = 5
74 74
75 # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.CL~~~otab.history_test 75 # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.history_CL~~~$otab.history_test"
76 ONAMEPOS = 0 76 ONAMEPOS = 0
77 OFMTPOS = 1 77 OFMTPOS = 1
78 OCLPOS = 2 78 OCLPOS = 2
79 OTESTPOS = 3 79 OTESTPOS = 3
80 OOCLPOS = 4 80 OOCLPOS = 4
113 return s 113 return s
114 except ValueError: 114 except ValueError:
115 return '"%s"' % s 115 return '"%s"' % s
116 116
117 117
118 html_escape_table = {"&": "&amp;", ">": "&gt;", "<": "&lt;", "$": r"\$"} 118 html_escape_table = {"&": "&amp;", ">": "&gt;", "<": "&lt;", "$": r"\$","#":"&#35;", "$":"&#36;"}
119 119 cheetah_escape_table = {"$": "\$","#":"\#"}
120 120
121 def html_escape(text): 121 def html_escape(text):
122 """Produce entities within text.""" 122 """Produce entities within text."""
123 return "".join(html_escape_table.get(c, c) for c in text) 123 return "".join(html_escape_table.get(c, c) for c in text)
124
125 def cheetah_escape(text):
126 """Produce entities within text."""
127 return "".join(cheetah_escape_table.get(c, c) for c in text)
124 128
125 129
126 def html_unescape(text): 130 def html_unescape(text):
127 """Revert entities within text. Multiple character targets so use replace""" 131 """Revert entities within text. Multiple character targets so use replace"""
128 t = text.replace("&amp;", "&") 132 t = text.replace("&amp;", "&")
129 t = t.replace("&gt;", ">") 133 t = t.replace("&gt;", ">")
130 t = t.replace("&lt;", "<") 134 t = t.replace("&lt;", "<")
131 t = t.replace("\\$", "$") 135 t = t.replace("\\$", "$")
136 t = t.replace("&#36;","$")
137 t = t.replace("&#35;","#")
132 return t 138 return t
133 139
134 140
135 def parse_citations(citations_text): 141 def parse_citations(citations_text):
136 """""" 142 """"""
287 prefix=self.tool_name, suffix="_%s" % (self.executeme) 293 prefix=self.tool_name, suffix="_%s" % (self.executeme)
288 ) 294 )
289 tscript = open(self.sfile, "w") 295 tscript = open(self.sfile, "w")
290 tscript.write(self.script) 296 tscript.write(self.script)
291 tscript.close() 297 tscript.close()
292 self.indentedScript = " %s" % "\n".join([" %s" % html_escape(x) for x in rx]) 298 self.indentedScript = " %s" % "\n".join([" %s" % cheetah_escape(x) for x in rx])
293 self.escapedScript = "%s" % "\n".join([" %s" % html_escape(x) for x in rx]) 299 self.escapedScript = "%s" % "\n".join([" %s" % cheetah_escape(x) for x in rx])
294 art = "%s.%s" % (self.tool_name, self.executeme) 300 art = "%s.%s" % (self.tool_name, self.executeme)
295 artifact = open(art, "wb") 301 artifact = open(art, "wb")
296 artifact.write(bytes(self.script, "utf8")) 302 artifact.write(bytes(self.escapedScript, "utf8"))
297 artifact.close() 303 artifact.close()
298 304
299 def cleanuppar(self): 305 def cleanuppar(self):
300 """ positional parameters are complicated by their numeric ordinal""" 306 """ positional parameters are complicated by their numeric ordinal"""
301 for i, p in enumerate(self.infiles): 307 for i, p in enumerate(self.infiles):
406 412
407 def doXMLparam(self): 413 def doXMLparam(self):
408 """flake8 made me do this...""" 414 """flake8 made me do this..."""
409 for p in self.outfiles: 415 for p in self.outfiles:
410 newname, newfmt, newcl, test, oldcl = p 416 newname, newfmt, newcl, test, oldcl = p
417 test = test.strip()
411 ndash = self.getNdash(newcl) 418 ndash = self.getNdash(newcl)
412 aparm = gxtp.OutputData(newcl, format=newfmt, num_dashes=ndash) 419 aparm = gxtp.OutputData(newcl, format=newfmt, num_dashes=ndash)
413 aparm.positional = self.is_positional 420 aparm.positional = self.is_positional
414 if self.is_positional: 421 if self.is_positional:
415 if oldcl == "STDOUT": 422 if oldcl == "STDOUT":
417 aparm.command_line_override = "> $%s" % newcl 424 aparm.command_line_override = "> $%s" % newcl
418 else: 425 else:
419 aparm.positional = int(oldcl) 426 aparm.positional = int(oldcl)
420 aparm.command_line_override = "$%s" % newcl 427 aparm.command_line_override = "$%s" % newcl
421 self.toutputs.append(aparm) 428 self.toutputs.append(aparm)
422 usetest = None
423 ld = None 429 ld = None
424 if test > "": 430 if test.strip() > "":
425 if test.startswith("diff"): 431 if test.startswith("diff"):
426 usetest = "diff" 432 c = "diff"
433 ld = 0
427 if test.split(":")[1].isdigit: 434 if test.split(":")[1].isdigit:
428 ld = int(test.split(":")[1]) 435 ld = int(test.split(":")[1])
429 else: 436 tp = gxtp.TestOutput(
430 usetest = test 437 name=newcl,
431 tp = gxtp.TestOutput( 438 value="%s_sample" % newcl,
432 name=newcl, 439 format=newfmt,
433 value="%s_sample" % newcl, 440 compare= c,
434 format=newfmt, 441 lines_diff=ld,
435 compare=usetest, 442 )
436 lines_diff=ld, 443 elif test.startswith("sim_size"):
437 delta=None, 444 c = "sim_size"
438 ) 445 tn = test.split(":")[1].strip()
439 self.testparam.append(tp) 446 if tn > '':
447 if '.' in tn:
448 delta = None
449 delta_frac = min(1.0,float(tn))
450 else:
451 delta = int(tn)
452 delta_frac = None
453 tp = gxtp.TestOutput(
454 name=newcl,
455 value="%s_sample" % newcl,
456 format=newfmt,
457 compare= c,
458 delta = delta,
459 delta_frac = delta_frac
460 )
461 self.testparam.append(tp)
440 for p in self.infiles: 462 for p in self.infiles:
441 newname = p[ICLPOS] 463 newname = p[ICLPOS]
442 newfmt = p[IFMTPOS] 464 newfmt = p[IFMTPOS]
443 ndash = self.getNdash(newname) 465 ndash = self.getNdash(newname)
444 if not len(p[ILABPOS]) > 0: 466 if not len(p[ILABPOS]) > 0:
548 self.newtool.command_override = self.command_override # config file 570 self.newtool.command_override = self.command_override # config file
549 else: 571 else:
550 self.newtool.command_override = self.xmlcl 572 self.newtool.command_override = self.xmlcl
551 if self.args.help_text: 573 if self.args.help_text:
552 helptext = open(self.args.help_text, "r").readlines() 574 helptext = open(self.args.help_text, "r").readlines()
553 safertext = [html_escape(x) for x in helptext] 575 safertext = "\n".join([cheetah_escape(x) for x in helptext])
554 if False and self.args.script_path: 576 if self.args.script_path:
555 scrp = self.script.split("\n") 577 scr = self.script.split("\n")
556 scrpt = [" %s" % x for x in scrp] # try to stop templating 578 scrp = [cheetah_escape(x) for x in scr]
557 scrpt.insert(0, "```\n") 579 scrpt = [' %s' % x for x in scrpt if x.strip() > ''] # indent
580 scrpt.insert(0,'------\n\nScript::\n')
558 if len(scrpt) > 300: 581 if len(scrpt) > 300:
559 safertext = ( 582 safertext = (
560 safertext 583 safertext
561 + scrpt[:100] 584 + scrpt[:100]
562 + [">500 lines - stuff deleted", "......"] 585 + [">500 lines - stuff deleted", "......"]
563 + scrpt[-100:] 586 + scrpt[-100:]
564 ) 587 )
565 else: 588 else:
566 safertext = safertext + scrpt 589 safertext = safertext + "\n".join(scrpt)
567 safertext.append("\n```") 590 self.newtool.help = safertext
568 self.newtool.help = "\n".join([x for x in safertext])
569 else: 591 else:
570 self.newtool.help = ( 592 self.newtool.help = (
571 "Please ask the tool author (%s) for help \ 593 "Please ask the tool author (%s) for help \
572 as none was supplied at tool generation\n" 594 as none was supplied at tool generation\n"
573 % (self.args.user_email) 595 % (self.args.user_email)
591 self.doXMLparam() 613 self.doXMLparam()
592 self.newtool.outputs = self.toutputs 614 self.newtool.outputs = self.toutputs
593 self.newtool.inputs = self.tinputs 615 self.newtool.inputs = self.tinputs
594 if self.args.script_path: 616 if self.args.script_path:
595 configfiles = gxtp.Configfiles() 617 configfiles = gxtp.Configfiles()
596 configfiles.append(gxtp.Configfile(name="runme", text=self.script)) 618 configfiles.append(gxtp.Configfile(name="runme", text=self.escapedScript))
597 self.newtool.configfiles = configfiles 619 self.newtool.configfiles = configfiles
598 tests = gxtp.Tests() 620 tests = gxtp.Tests()
599 test_a = gxtp.Test() 621 test_a = gxtp.Test()
600 for tp in self.testparam: 622 for tp in self.testparam:
601 test_a.append(tp) 623 test_a.append(tp)
605 "Created by %s at %s using the Galaxy Tool Factory." 627 "Created by %s at %s using the Galaxy Tool Factory."
606 % (self.args.user_email, timenow()) 628 % (self.args.user_email, timenow())
607 ) 629 )
608 self.newtool.add_comment("Source in git at: %s" % (toolFactoryURL)) 630 self.newtool.add_comment("Source in git at: %s" % (toolFactoryURL))
609 self.newtool.add_comment( 631 self.newtool.add_comment(
610 "Cite: Creating re-usable tools from scripts doi: \ 632 "Cite: Creating re-usable tools from scripts doi:10.1093/bioinformatics/bts573"
611 10.1093/bioinformatics/bts573"
612 ) 633 )
613 exml0 = self.newtool.export() 634 exml0 = self.newtool.export()
614 exml = exml0.replace(FAKEEXE, "") # temporary work around until PR accepted 635 exml = exml0.replace(FAKEEXE, "") # temporary work around until PR accepted
615 if ( 636 if (
616 self.test_override 637 self.test_override
804 url=self.args.toolshed_url, key=self.args.toolshed_api_key, verify=False 825 url=self.args.toolshed_url, key=self.args.toolshed_api_key, verify=False
805 ) 826 )
806 repos = ts.repositories.get_repositories() 827 repos = ts.repositories.get_repositories()
807 rnames = [x.get("name", "?") for x in repos] 828 rnames = [x.get("name", "?") for x in repos]
808 rids = [x.get("id", "?") for x in repos] 829 rids = [x.get("id", "?") for x in repos]
809 sto.write(f"############names={rnames} rids={rids}\n")
810 sto.write(f"############names={repos}\n")
811 tfcat = "ToolFactory generated tools" 830 tfcat = "ToolFactory generated tools"
812 if self.tool_name not in rnames: 831 if self.tool_name not in rnames:
813 tscat = ts.categories.get_categories() 832 tscat = ts.categories.get_categories()
814 cnames = [x.get("name", "?").strip() for x in tscat] 833 cnames = [x.get("name", "?").strip() for x in tscat]
815 cids = [x.get("id", "?") for x in tscat] 834 cids = [x.get("id", "?") for x in tscat]
825 remote_repository_url=self.args.toolshed_url, 844 remote_repository_url=self.args.toolshed_url,
826 homepage_url=None, 845 homepage_url=None,
827 category_ids=catID, 846 category_ids=catID,
828 ) 847 )
829 tid = res.get("id", None) 848 tid = res.get("id", None)
830 sto.write(f"##########create res={res}\n")
831 else: 849 else:
832 i = rnames.index(self.tool_name) 850 i = rnames.index(self.tool_name)
833 tid = rids[i] 851 tid = rids[i]
834 try: 852 try:
835 res = ts.repositories.update_repository( 853 res = ts.repositories.update_repository(