Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

135 lines
5.0 KiB
Python
Raw Permalink Normal View History

2011-04-13 22:36:57 +02:00
import os
2011-04-14 15:09:00 +02:00
from collections import defaultdict
2015-07-13 20:13:28 +01:00
from xml.etree import ElementTree as ET
2011-04-13 22:36:57 +02:00
# Todo: ""
2011-04-14 15:09:00 +02:00
# execute from galaxy root dir
tooldict = defaultdict(list)
2011-04-13 22:36:57 +02:00
2015-07-13 20:13:28 +01:00
2011-04-13 22:36:57 +02:00
def main():
doc = ET.parse("tool_conf.xml")
root = doc.getroot()
2015-07-13 20:13:28 +01:00
2011-04-14 15:09:00 +02:00
# index range 1-1000, current sections/tools divided between 250-750
sectionindex = 250
2019-04-23 12:51:09 +01:00
sectionfactor = int(500 / len(root))
2015-07-13 20:13:28 +01:00
2019-04-23 12:51:09 +01:00
for rootchild in root:
2011-04-14 15:09:00 +02:00
currentsectionlabel = ""
2017-08-17 00:33:02 +01:00
if rootchild.tag == "section":
2011-04-14 15:09:00 +02:00
sectionname = rootchild.attrib["name"]
# per section tool index range 1-1000, current labels/tools
# divided between 20 and 750
toolindex = 250
2019-04-23 12:51:09 +01:00
toolfactor = int(500 / len(rootchild))
2011-04-14 15:09:00 +02:00
currentlabel = ""
2019-04-23 12:51:09 +01:00
for sectionchild in rootchild:
2017-08-17 00:33:02 +01:00
if sectionchild.tag == "tool":
2011-04-14 15:09:00 +02:00
addToToolDict(sectionchild, sectionname, sectionindex, toolindex, currentlabel)
toolindex += toolfactor
2017-08-17 00:33:02 +01:00
elif sectionchild.tag == "label":
2015-07-13 20:13:28 +01:00
currentlabel = sectionchild.attrib["text"]
sectionindex += sectionfactor
2017-08-17 00:33:02 +01:00
elif rootchild.tag == "tool":
2011-04-14 15:09:00 +02:00
addToToolDict(rootchild, "", sectionindex, None, currentsectionlabel)
2015-07-13 20:13:28 +01:00
sectionindex += sectionfactor
2017-08-17 00:33:02 +01:00
elif rootchild.tag == "label":
2015-07-13 20:13:28 +01:00
currentsectionlabel = rootchild.attrib["text"]
sectionindex += sectionfactor
2011-04-14 15:09:00 +02:00
# scan galaxy root tools dir for tool-specific xmls
2017-08-17 00:33:02 +01:00
toolconffilelist = getfnl(os.path.join(os.getcwd(), "tools"))
2015-07-13 20:13:28 +01:00
# foreach tool xml:
2011-04-14 15:09:00 +02:00
# check if the tags element exists in the tool xml (as child of <tool>)
# if not, add empty tags element for later use
# if this tool is in the above tooldict, add the toolboxposition element to the tool xml
# if not, then nothing.
for toolconffile in toolconffilelist:
hastags = False
hastoolboxpos = False
2015-07-13 20:13:28 +01:00
# parse tool config file into a document structure as defined by the ElementTree
2011-04-14 15:09:00 +02:00
tooldoc = ET.parse(toolconffile)
# get the root element of the toolconfig file
tooldocroot = tooldoc.getroot()
2015-07-13 20:13:28 +01:00
# check tags element, set flag
2011-04-14 15:09:00 +02:00
tagselement = tooldocroot.find("tags")
if tagselement:
hastags = True
# check if toolboxposition element already exists in this tooconfig file
toolboxposelement = tooldocroot.find("toolboxposition")
2017-08-17 00:33:02 +01:00
if toolboxposelement:
2011-04-14 15:09:00 +02:00
hastoolboxpos = True
2015-07-13 20:13:28 +01:00
2017-08-17 00:33:02 +01:00
if not (hastags and hastoolboxpos):
2020-10-06 18:49:19 +01:00
original = open(toolconffile)
2011-04-14 15:09:00 +02:00
contents = original.readlines()
original.close()
2015-07-13 20:13:28 +01:00
2011-04-14 15:09:00 +02:00
# the new elements will be added directly below the root tool element
addelementsatposition = 1
# but what's on the first line? Root or not?
2017-08-17 00:33:02 +01:00
if contents[0].startswith("<?"):
2011-04-14 15:09:00 +02:00
addelementsatposition = 2
newelements = []
2017-08-17 00:33:02 +01:00
if not hastoolboxpos:
if toolconffile in tooldict:
2015-07-13 20:13:28 +01:00
for attributes in tooldict[toolconffile]:
# create toolboxposition element
sectionelement = ET.Element("toolboxposition")
sectionelement.attrib = attributes
sectionelement.tail = "\n "
2017-08-17 00:33:02 +01:00
newelements.append(ET.tostring(sectionelement, "utf-8"))
2011-04-13 22:36:57 +02:00
2017-08-17 00:33:02 +01:00
if not hastags:
2011-04-14 15:09:00 +02:00
# create empty tags element
2017-08-17 00:33:02 +01:00
newelements.append("<tags/>\n ")
2011-04-14 15:09:00 +02:00
2021-01-16 14:40:01 +00:00
contents = contents[0:addelementsatposition] + newelements + contents[addelementsatposition:]
2015-07-13 20:13:28 +01:00
2011-04-14 15:09:00 +02:00
# add .new for testing/safety purposes :P
2017-08-17 00:33:02 +01:00
newtoolconffile = open(toolconffile, "w")
newtoolconffile.writelines(contents)
2011-04-14 15:09:00 +02:00
newtoolconffile.close()
def addToToolDict(tool, sectionname, sectionindex, toolindex, currentlabel):
2011-04-13 22:36:57 +02:00
toolfile = tool.attrib["file"]
realtoolfile = os.path.join(os.getcwd(), "tools", toolfile)
2015-07-13 20:13:28 +01:00
2011-04-14 15:09:00 +02:00
# define attributes for the toolboxposition xml-tag
attribdict = {}
2017-08-17 00:33:02 +01:00
if sectionname:
attribdict["section"] = sectionname
if currentlabel:
attribdict["label"] = currentlabel
if sectionindex:
attribdict["sectionorder"] = str(sectionindex)
if toolindex:
attribdict["order"] = str(toolindex)
tooldict[realtoolfile].append(attribdict)
2011-04-13 22:36:57 +02:00
2015-07-13 20:13:28 +01:00
2011-04-14 15:09:00 +02:00
# Build a list of all toolconf xml files in the tools directory
def getfnl(startdir):
filenamelist = []
for root, _dirs, files in os.walk(startdir):
2011-04-14 15:09:00 +02:00
for fn in files:
fullfn = os.path.join(root, fn)
if fn.endswith(".xml"):
try:
doc = ET.parse(fullfn)
2017-10-12 17:10:12 +01:00
except Exception as e:
2020-10-06 18:49:19 +01:00
raise Exception(f"Oops, bad XML in '{fullfn}': {e}")
2011-04-14 15:09:00 +02:00
rootelement = doc.getroot()
# here we check if this xml file actually is a tool conf xml!
if rootelement.tag == "tool":
filenamelist.append(fullfn)
return filenamelist
2011-04-13 22:36:57 +02:00
2016-11-14 21:12:14 +00:00
2011-04-13 22:36:57 +02:00
if __name__ == "__main__":
main()