-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathextract_toolbox_sections.py
More file actions
135 lines (114 loc) · 5.01 KB
/
Copy pathextract_toolbox_sections.py
File metadata and controls
135 lines (114 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
from collections import defaultdict
from xml.etree import ElementTree as ET
# Todo: ""
# execute from galaxy root dir
tooldict = defaultdict(list)
def main():
doc = ET.parse("tool_conf.xml")
root = doc.getroot()
# index range 1-1000, current sections/tools divided between 250-750
sectionindex = 250
sectionfactor = int(500 / len(root))
for rootchild in root:
currentsectionlabel = ""
if rootchild.tag == "section":
sectionname = rootchild.attrib["name"]
# per section tool index range 1-1000, current labels/tools
# divided between 20 and 750
toolindex = 250
toolfactor = int(500 / len(rootchild))
currentlabel = ""
for sectionchild in rootchild:
if sectionchild.tag == "tool":
addToToolDict(sectionchild, sectionname, sectionindex, toolindex, currentlabel)
toolindex += toolfactor
elif sectionchild.tag == "label":
currentlabel = sectionchild.attrib["text"]
sectionindex += sectionfactor
elif rootchild.tag == "tool":
addToToolDict(rootchild, "", sectionindex, None, currentsectionlabel)
sectionindex += sectionfactor
elif rootchild.tag == "label":
currentsectionlabel = rootchild.attrib["text"]
sectionindex += sectionfactor
# scan galaxy root tools dir for tool-specific xmls
toolconffilelist = getfnl(os.path.join(os.getcwd(), "tools"))
# foreach tool xml:
# 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
# parse tool config file into a document structure as defined by the ElementTree
tooldoc = ET.parse(toolconffile)
# get the root element of the toolconfig file
tooldocroot = tooldoc.getroot()
# check tags element, set flag
tagselement = tooldocroot.find("tags")
if tagselement:
hastags = True
# check if toolboxposition element already exists in this tooconfig file
toolboxposelement = tooldocroot.find("toolboxposition")
if toolboxposelement:
hastoolboxpos = True
if not (hastags and hastoolboxpos):
original = open(toolconffile)
contents = original.readlines()
original.close()
# the new elements will be added directly below the root tool element
addelementsatposition = 1
# but what's on the first line? Root or not?
if contents[0].startswith("<?"):
addelementsatposition = 2
newelements = []
if not hastoolboxpos:
if toolconffile in tooldict:
for attributes in tooldict[toolconffile]:
# create toolboxposition element
sectionelement = ET.Element("toolboxposition")
sectionelement.attrib = attributes
sectionelement.tail = "\n "
newelements.append(ET.tostring(sectionelement, "utf-8"))
if not hastags:
# create empty tags element
newelements.append("<tags/>\n ")
contents = contents[0:addelementsatposition] + newelements + contents[addelementsatposition:]
# add .new for testing/safety purposes :P
newtoolconffile = open(toolconffile, "w")
newtoolconffile.writelines(contents)
newtoolconffile.close()
def addToToolDict(tool, sectionname, sectionindex, toolindex, currentlabel):
toolfile = tool.attrib["file"]
realtoolfile = os.path.join(os.getcwd(), "tools", toolfile)
# define attributes for the toolboxposition xml-tag
attribdict = {}
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)
# Build a list of all toolconf xml files in the tools directory
def getfnl(startdir):
filenamelist = []
for root, _dirs, files in os.walk(startdir):
for fn in files:
fullfn = os.path.join(root, fn)
if fn.endswith(".xml"):
try:
doc = ET.parse(fullfn)
except Exception as e:
raise Exception(f"Oops, bad XML in '{fullfn}': {e}")
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
if __name__ == "__main__":
main()