8
8
def __not_root (path ): return path != _ROOT_SYMBOL
9
9
10
10
11
+ def __target_in_path (path , target ): return target and target .startswith (path )
12
+
13
+
11
14
def get_node (tree , path , create = False ):
12
15
"""Retrieve json or value given a path"""
13
16
17
+ if not path :
18
+ raise ValueError ("Cannot accept empty path" )
19
+
14
20
current = tree
15
- if path and __not_root (path ):
21
+ if __not_root (path ):
16
22
current = __get_node (tree , path .split (_SEPARATOR ), create )
17
23
18
- elif not path :
19
- raise ValueError ("Cannot accept empty path" )
20
-
21
24
return current
22
25
23
26
24
27
def update_node (tree , path , node_str ):
25
28
"""Update node with json or value in string format given a path"""
26
29
27
- if path and __not_root (path ):
30
+ __check_path_not_empty (path )
31
+
32
+ if __not_root (path ):
28
33
parent = path .split (_SEPARATOR )
29
34
to_set = parent .pop ()
30
35
if parent :
@@ -37,9 +42,6 @@ def update_node(tree, path, node_str):
37
42
else :
38
43
current_node [to_set ] = json .loads (node_str )
39
44
40
- elif not path :
41
- raise ValueError ("Cannot accept empty path" )
42
-
43
45
else :
44
46
tree .clear ()
45
47
tree .update (json .loads (node_str ))
@@ -50,7 +52,9 @@ def update_node(tree, path, node_str):
50
52
def pop_node (tree , path ):
51
53
"""Retrieve and delete json or value given a path"""
52
54
53
- if path and __not_root (path ):
55
+ __check_path_not_empty (path )
56
+
57
+ if __not_root (path ):
54
58
parent = path .split (_SEPARATOR )
55
59
to_delete = parent .pop ()
56
60
if parent :
@@ -60,9 +64,6 @@ def pop_node(tree, path):
60
64
61
65
return json .dumps (node .pop (to_delete ))
62
66
63
- elif not path :
64
- raise ValueError ("Cannot accept empty path" )
65
-
66
67
else :
67
68
node = json .dumps (tree )
68
69
tree .clear ()
@@ -72,8 +73,10 @@ def pop_node(tree, path):
72
73
def cleanup_node (tree , path , target ):
73
74
"""Remove a node if not in target path and no child is found given a path"""
74
75
75
- if path and __not_root (path ):
76
- if not (target and target .startswith (path )):
76
+ __check_path_not_empty (path )
77
+
78
+ if __not_root (path ):
79
+ if not __target_in_path (path , target ):
77
80
parent = path .split (_SEPARATOR )
78
81
to_delete = parent .pop ()
79
82
if parent :
@@ -84,9 +87,6 @@ def cleanup_node(tree, path, target):
84
87
if not node [to_delete ]:
85
88
del node [to_delete ]
86
89
87
- elif not path :
88
- raise ValueError ("Cannot accept empty path" )
89
-
90
90
else :
91
91
if not tree :
92
92
tree .clear ()
@@ -108,3 +108,8 @@ def __get_node(tree, node_list, create):
108
108
current [last_node ] = {}
109
109
110
110
return current [last_node ]
111
+
112
+
113
+ def __check_path_not_empty (path ):
114
+ if not path :
115
+ raise ValueError ("Cannot accept empty path" )
0 commit comments