This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtcd_browser.py
89 lines (76 loc) · 3.39 KB
/
tcd_browser.py
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
"""Represents _code_browser.tcd"""
import sys
import xml.etree.ElementTree as ET
import xml.dom.minidom as MD
from preferences import Wrapped
TCD_LIST = [
"_code_browser.tcd",
"_debugger.tcd",
"_version _tracking.tcd",
"Version Tracking (DESTINATION TOOL).tool",
"Version Tracking (SOURCE TOOL).tool",
]
class TCDBrowser:
def __init__(self, path: str) -> None:
self.path = path
tree = ET.parse(path)
self.root = tree.getroot()
def _indent(self, elem, level=0):
"""Spacing fixup after adding new things"""
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
self._indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def update(self, preferences):
# List comprehensions instead of a ton of nested statements
tool = [_ for _ in self.root.iter() if _.tag == "TOOL"][0]
tool_options = [_ for _ in tool.iter() if _.tag == "OPTIONS"][0]
categories = [_ for _ in tool_options.iter() if _.tag == "CATEGORY" and "NAME" in _.attrib]
# Add missing category tags
names = [_.get("NAME") for _ in categories]
for category in preferences.keys():
if category not in names:
c = ET.Element("CATEGORY")
c.set("NAME", category)
tool_options.insert(sys.maxsize, c)
# Remake the categories list in case we added new ones
categories = [_ for _ in tool_options.iter() if _.tag == "CATEGORY" and "NAME" in _.attrib]
for category in categories:
for preference, option in preferences.get(category.get("NAME"), {}).items():
element = [_ for _ in category.iter() if _.get("NAME") == preference]
# Check if the preference exists or not
if len(element) == 0:
e = ET.Element(option.tag)
e.set("NAME", preference)
if isinstance(option, Wrapped):
e.set("CLASS", option.classname)
for state in option.states:
s = ET.SubElement(e, state.tag)
s.set("NAME", state.name)
s.set("TYPE", state.type)
s.set("VALUE", state.value)
else:
e.set("TYPE", option.type)
e.set("VALUE", option.value)
category.insert(sys.maxsize, e)
else:
element = element[0]
if isinstance(option, Wrapped):
for state in option.states:
e = [_ for _ in element.iter() if _.get("NAME") == state.name][0]
e.set("VALUE", state.value)
else:
element.set("VALUE", option.value)
self._indent(self.root)
xmlstr = MD.parseString(ET.tostring(self.root)).toprettyxml(indent="", newl="", encoding="UTF-8")
with open(self.path, "wb") as f:
f.write(xmlstr)