In older post, I went through ipxactral
(poor man’s RAL generator). It seems only logical to extract the part where i process XML and make dedicated module for that. I hear someone shouting, isn’t XML a tree already?
My answer is I HATE XML
, So I will do whatever to avoid dealing with XML.
Jumping right into it, IPXACTTree
just takes path to ixact file.
t = IPXACTTree(args.ipxactfile)
The heavy lifting is done here to create the tree starting from root and recursively going through the nodes.
class IPXACTTree():
def __init__(self,ipxactfile):
def iterate_tree(xmlnode,node):
for child in xmlnode:
cn = Node(child)
node.add(cn)
iterate_tree(child, cn)
tree = ET.parse(ipxactfile)
xmlroot = tree.getroot()
self.root = Node(xmlroot)
iterate_tree(xmlroot,self.root)
Other than that, most work is delegated to Node
def __iter__(self):
for node in self.root:
yield node
def __str__(self):
return str(self.root)
def findall(self,tag):
all = self.root.findall(tag)
return all
The interesting part is searching for tags with findall
. I do quick DFS of the tree to get the nodes matching the tag.
def __iter__(self):
def dfs_internal(node):
yield node
for child in node.children:
yield from dfs_internal(child)
yield from dfs_internal(self)
def __str__(self):
return "".join([("\t" * node.level)+ f"{node.tag}: {node.text}\n" for node in self])
def findall(self,tag):
all = [node for node in self if tag == node.tag]
return all