-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavcategory.py
150 lines (127 loc) · 4.81 KB
/
navcategory.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from autogen import Autogen
import xml.etree.ElementTree as Et
from xml.dom import minidom
from tree import Tree
__author__ = 'Yura'
class NavCategory(object):
def __init__(self, nid, hid=0, link=None, primary=False):
self.nid = nid
self.hid = hid
self.link = link if link else {}
self.primary = primary
self.tree = 0
self.children = []
def add_children(self, *nodes):
self.children += list(nodes)
return self
def as_node(self, tree):
return Et.SubElement(tree, 'node', attrib={
'id': str(self.nid),
'primary': str(self.primary),
'hid': str(self.hid)
})
def as_link(self, links):
if not self.link:
return
link = Et.SubElement(links, 'link')
url = Et.SubElement(link, 'url')
params = Et.SubElement(url, 'params')
for (k, v) in self.link.iteritems():
param = Et.SubElement(params, 'param')
Et.SubElement(param, 'name').text = str(k)
Et.SubElement(param, 'value').text = str(v)
class NavTree(Tree):
def _link_parent(self, parent, child):
if child.primary:
if child.hid in self.__primaries:
raise RuntimeError('Hyper category {0} already has primary nid'.format(child.hid))
self.__primaries.add(child.hid)
parent.children.append(child)
@staticmethod
def _get_id(c):
return c.nid
def __init__(self, id=0, **kwargs):
Tree.__init__(self, root=NavCategory(**kwargs))
self.__id = id
self.__primaries = set()
if self._root.primary:
self.__primaries.add(self._root.hid)
def save(self, navigation, links):
categs = [self._root]
root = Et.SubElement(navigation, 'navigation_tree', attrib={'id': str(self.__id)})
xmls = [self._root.as_node(root)]
while categs:
topc = categs.pop()
topx = xmls.pop()
for child in topc.children:
categs.append(child)
xmls.append(child.as_node(topx))
child.as_link(links)
def add_primary(self, hid):
if hid not in self.__primaries:
self.__primaries.add(hid)
self._root.add_children(NavCategory(Autogen.get('nid'), hid=hid, primary=True))
'''
class NavTree(object):
def __init__(self, id=0, **kwargs):
self.id = id
self.root = NavCategory(**kwargs)
self.__primaries = set()
if self.root.primary:
self.__primaries.add(self.root.hid)
def add_children(self, children, index):
self.root.children += [k for k in children.iterkeys()]
candidates = [(k, v) for (k, v) in children.iteritems()]
while candidates:
cat, children = candidates.pop(0)
index[cat.nid] = cat
cat.children = [k for k in children.iterkeys()]
candidates += [(k, v) for (k, v) in children.iteritems()]
if cat.primary:
if cat.hid in self.__primaries:
raise RuntimeError('Hyper category {0} already has primary nid'.format(cat.hid))
self.__primaries.add(cat.hid)
def save(self, navigation, links):
categs = [self.root]
root = Et.SubElement(navigation, 'navigation_tree', attrib={'id': str(self.id)})
xmls = [self.root.as_node(root)]
while categs:
topc = categs.pop()
topx = xmls.pop()
for child in topc.children:
categs.append(child)
xmls.append(child.as_node(topx))
child.as_link(links)
def add_primary(self, hid):
if hid not in self.__primaries:
self.__primaries.add(hid)
self.root.add_children(NavCategory(Autogen.get('nid'), hid=hid, primary=True))
'''
class NavForest(object):
def __init__(self):
self.__trees = []
self.default_tree = NavTree(
id=Autogen.default_navtree,
nid=Autogen.get('nid'),
hid=Autogen.root_hid,
primary=True
)
self.__add_tree(self.default_tree, {})
def __add_tree(self, tree, branches):
self.__trees.append(tree)
tree.add_children(branches)
def add_trees(self, trees):
for (t, b) in trees.iteritems():
self.__add_tree(t, b)
def add_branch_to_default_tree(self, branch):
self.default_tree.add_children(branch)
def save(self, out):
root = Et.Element('navigation')
links = Et.SubElement(root, 'links')
for tree in self.__trees:
tree.save(root, links)
reparsed = minidom.parseString(Et.tostring(root))
out.write(reparsed.toprettyxml(indent=" ") + '\n')
def add_primary_in_all_trees(self, hid):
for tree in self.__trees:
tree.add_primary(hid)