-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.py
114 lines (97 loc) · 4.44 KB
/
tree.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
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from treelib import Node, Tree
import sys
# ------------------------------------------------------------------------------------------
#
# Read the CSV file exported from Confluence.py
#
# ------------------------------------------------------------------------------------------
df = pd.read_csv("confluence.csv") # Make sure this is in the same directy as this file
# ------------------------------------------------------------------------------------------
#
# Create a tree using treelib and make a base node named 'Confluence'
#
# ------------------------------------------------------------------------------------------
tree = Tree() # Create an empty tree object to populate
tree.create_node("Confluence", "confluence") # Create the first node; there can only be one top level node
# ------------------------------------------------------------------------------------------
#
# Create empty dataframes for later use
#
# ------------------------------------------------------------------------------------------
newDF = pd.DataFrame(columns=['id', 'space', 'parent', 'name', 'url'])
secondTierDF = pd.DataFrame(columns=['id', 'space', 'parent', 'name', 'url'])
thirdTierDF = pd.DataFrame(columns=['id', 'space', 'parent', 'name', 'url'])
fourthTierDF = pd.DataFrame(columns=['id', 'space', 'parent', 'name', 'url'])
# ------------------------------------------------------------------------------------------
#
# Create first level heirarchy from spaces
#
# ------------------------------------------------------------------------------------------
# Create first level of tree made up of confluence spaces
for index, row in df.iterrows():
if row['parent'] == 0:
# Clean up dataframe to remove tildes from space names (generated by user pages)
if not row['space'].find("~"):
row['space'] = row['space'][1:]
spaceName = row[3]
tree.create_node(spaceName, row['id'], parent="confluence")
newDF = newDF.append(row)
df.drop(index) #drop all personal pages
else:
tree.create_node(row['space'], row['id'], parent="confluence")
newDF = newDF.append(row)
# Creating a copy of the original page DF to be used later on
df2 = df
# ------------------------------------------------------------------------------------------
#
# Loop through 3 additional levels of page hierarchies for each space
#
# ------------------------------------------------------------------------------------------
# Create the second level heirarchy by comparing every page ID to every parent ID
for index, row in newDF.iterrows():
parentID = row['id']
for index2, confRow in df.iterrows():
childID = confRow['parent']
if parentID == childID:
pageName = confRow[3]
print("TIER 1", pageName)
tree.create_node(pageName, confRow[0], parent=parentID)
secondTierDF = secondTierDF.append(confRow)
df.drop(df.index[index2])
# Create third level heirarchy
for index, row in secondTierDF.iterrows():
parentID2 = row['id']
for index2, secondTierRow in df2.iterrows():
childID2 = secondTierRow['parent']
if parentID2 == childID2:
pageName2 = secondTierRow[3]
print("TIER 2", pageName2)
tree.create_node(pageName2, secondTierRow[0], parent=parentID2)
thirdTierDF = thirdTierDF.append(secondTierRow)
# Create fourth level heirarchy
for index, row in thirdTierDF.iterrows():
parentID3 = row['id']
for index3, thirdTierRow in df2.iterrows():
childID3 = thirdTierRow['parent']
if parentID3 == childID3:
pageName3 = thirdTierRow[3]
print("TIER 3", pageName3)
tree.create_node(pageName3, thirdTierRow[0], parent=parentID3)
# Create fifth level heirarchy
for index, row in fourthTierDF.iterrows():
parentID4 = row['id']
for index4, fourthTierRow in df2.iterrows():
childID4 = fourthTierRow['parent']
if parentID4 == childID4:
pageName4 = fourthTierRow[3]
print("TIER 4!", pageName4)
tree.create_node(pageName4, fourthTierRow[0], parent=parentID4)
# ------------------------------------------------------------------------------------------
#
# Print the tree generated by treelib
#
# ------------------------------------------------------------------------------------------
print(tree.show()) # Prints the tree in the console