1
1
"""
2
2
Module is used for analyzing link relationships
3
3
"""
4
- from treelib import Node , Tree , exceptions
4
+ from treelib import Tree , exceptions
5
5
6
6
from .api import get_node
7
7
from .utils import join_local_path
8
- from .log import info , debug
8
+ from .log import debug
9
+
9
10
10
11
def formatNode (n ):
11
12
return f"{ n ['url' ]} { n ['status_code' ]} { n ['status' ]} "
12
13
14
+
13
15
def build_tree_recursive (t , n ):
14
16
17
+ current_id = n ["url" ]
15
18
# this will only be ran on the root node since others will exist before being passed
16
- if not t .contains (n [ "url" ] ):
17
- debug (f"adding root node { n } " )
18
- t .create_node (formatNode (n ), n [ "url" ] )
19
+ if not t .contains (current_id ):
20
+ debug (f"adding id { current_id } " )
21
+ t .create_node (formatNode (n ), current_id )
19
22
23
+ children = n ["children" ]
20
24
# if there are no children, there's nothing to process
21
- if not n [ " children" ] :
25
+ if not children :
22
26
return
23
27
24
- for child in n ["children" ]:
25
- id = child ["url" ]
28
+ for child in children :
29
+
30
+ current_id = child ["url" ]
26
31
parent_id = n ["url" ]
32
+
27
33
try :
28
- debug (f"adding node { child } " )
29
- debug (f"parent_id { parent_id } " )
30
- t .create_node (formatNode (child ), child ["url" ], parent = n ["url" ])
34
+ debug (f"adding child_id { current_id } to parent_id { parent_id } " )
35
+ t .create_node (formatNode (child ), current_id , parent = parent_id )
31
36
except exceptions .DuplicatedNodeIdError :
32
- debug (f"found a duplicate url { child [ 'url' ] } " )
33
- continue # this node has already been processed somewhere else
37
+ debug (f"found a duplicate url { current_id } " )
38
+ continue # this node has already been processed somewhere else
34
39
35
40
build_tree_recursive (t , child )
36
41
@@ -53,21 +58,26 @@ def __build_tree(self, url: str, depth: int = 1):
53
58
Returns:
54
59
tree (ete3.Tree): Built tree.
55
60
"""
56
- debug (f"building tree for { root } at { depth } depth" )
61
+ debug (f"building tree for { url } at { depth } depth" )
57
62
n = get_node (url , depth )
58
63
t = Tree ()
59
64
build_tree_recursive (t , n )
60
- self ._tree = t ;
65
+ self ._tree = t
61
66
debug ("tree built successfully" )
62
67
63
68
def save (self , file_name : str ):
64
69
"""
65
70
Saves LinkTree to file with given file_name
66
- Current file types supported are .png, .pdf, .svg
71
+ Current file types supported are .txt
67
72
"""
68
73
debug (f"saving link tree as { file_name } " )
69
74
file_path = join_local_path (file_name )
70
- self ._tree .save2file (file_path )
75
+ try :
76
+ self ._tree .save2file (file_path )
77
+ except Exception as e :
78
+ debug (f"failed to save link tree to { file_path } " )
79
+ raise e
80
+
71
81
debug (f"file saved successfully to { file_path } " )
72
82
73
83
def show (self ):
0 commit comments