Commit 83d90bfa authored by Matthieu Boileau's avatar Matthieu Boileau

Count number of pages in rubrique tree

parent fedb9ef1
......@@ -403,7 +403,8 @@ class Website:
self.nullified_urls = 0
self.articles = []
self.rubrique_tree = {}
self.rubrique_nodes = {}
self.rubrique_tree = None
config_filename = "config.yml"
with open(config_filename, 'r') as ymlfile:
......@@ -473,38 +474,41 @@ class Website:
return f"{node_id}: {self.labels[node_id]}"
def insert_node(node_id):
"""A recursive function to insert a node in the anytree.Node tree"""
"""A recursive function to insert a node in the anytree.AnyNode tree"""
node_name = get_label(node_id)
if node_name not in self.rubrique_tree:
if node_name not in self.rubrique_nodes:
# Create node only if it does not exist
if node_id == 0:
# This the root node
self.rubrique_tree[node_name] = anytree.Node(node_name)
self.rubrique_nodes[node_name] = anytree.AnyNode(name=node_name, id=node_id, count=0)
else:
parent_id = self.parents[node_id]
parent_name = get_label(parent_id)
parent = self.rubrique_tree.get(parent_name)
parent = self.rubrique_nodes.get(parent_name)
if not parent:
# insert parent if it does not exist
insert_node(parent_id)
parent = self.rubrique_tree[parent_name]
self.rubrique_tree[node_name] = anytree.Node(node_name, parent=parent)
parent = self.rubrique_nodes[parent_name]
self.rubrique_nodes[node_name] = anytree.AnyNode(name=node_name, id=node_id, count=0, parent=parent)
self.labels[0] = "root" # Root node label
for node_id in self.parents:
insert_node(node_id)
self.rubrique_tree = self.rubrique_nodes[get_label(0)] # rubrique_tree is the root node
# Count the number of articles for each rubriques
for article in self.articles:
if article.type != 'rubrique': # Do not rubriques themselves
pass
#rubrique_node = anytree.search.find_by_attr(self.rubrique_root, article.rubrique, name="id")
#rubrique_node.count += 1
#if article.type != 'rubrique': # Do not count rubriques themselves
rubrique_node = anytree.search.find_by_attr(self.rubrique_tree, article.rubrique, name="id")
rubrique_node.count += 1
logger.debug("-------")
logger.debug("Spip rubriques scheme:")
logger.debug("id: title [number of pages]")
# Render the AnyNode object like the bash "tree" command would do
for pre, fill, rubrique_node in anytree.RenderTree(self.rubrique_tree['0: root']):
print(f"{pre}{rubrique_node.name}")
for pre, fill, rubrique_node in anytree.RenderTree(self.rubrique_nodes['0: root']):
print(f"{pre}{rubrique_node.name} [{rubrique_node.count}]")
def _build_doc_index(self):
"""Build the index dictionary: {id_doc: file_path}"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment