-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rb
82 lines (71 loc) · 1.35 KB
/
utils.rb
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
def getNode(n,child_name)
path_parts = child_name.split(":")
path = path_parts.shift
new_child_name = path_parts.join(":")
n.children.each{|child|
if child.name == path
if child.name == child_name
return child
else
return getNode(child,new_child_name)
end
end
}
return nil
end
def getNodes(n,child_name)
path_parts = child_name.split(":")
path = path_parts.shift
new_child_name = path_parts.join(":")
ret = []
n.children.each{|child|
if child.name == path
if child.name == child_name
ret << child
else
ret << getNode(child,new_child_name)
end
end
}
return ret
end
def getNodeValue(n,child_name,dict=false)
node = getNode(n,child_name)
if node.nil?
return nil
end
if !dict
return node.values
else
ret = {}
node.children.each{|child|
ret[child.name] = child.values
}
return ret
end
end
def printTree(n,tabs)
if n.values.count > 1
valStr = " --> [" + n.values.join(", ") + "]"
elsif n.values.count == 1
valStr = " --> " + n.values[0]
else
valStr = ""
end
#ap n.attributes
if n.attributes.count > 0
nattrs = []
n.attributes.each{|k,v|
nattrs << "#{k}:#{v}"
}
attStr = " @ [" + nattrs.join(", ") + "]"
else
attStr = ""
end
puts tabs + n.namespace + ":" + n.name + valStr + attStr
if n.children.count>0
n.children.each{|child|
printTree(child,tabs+"\t")
}
end
end