88def __not_root (path ): return path != _ROOT_SYMBOL
99
1010
11+ def __target_in_path (path , target ): return target and target .startswith (path )
12+
13+
1114def get_node (tree , path , create = False ):
1215 """Retrieve json or value given a path"""
1316
17+ if not path :
18+ raise ValueError ("Cannot accept empty path" )
19+
1420 current = tree
15- if path and __not_root (path ):
21+ if __not_root (path ):
1622 current = __get_node (tree , path .split (_SEPARATOR ), create )
1723
18- elif not path :
19- raise ValueError ("Cannot accept empty path" )
20-
2124 return current
2225
2326
2427def update_node (tree , path , node_str ):
2528 """Update node with json or value in string format given a path"""
2629
27- if path and __not_root (path ):
30+ __check_path_not_empty (path )
31+
32+ if __not_root (path ):
2833 parent = path .split (_SEPARATOR )
2934 to_set = parent .pop ()
3035 if parent :
@@ -37,9 +42,6 @@ def update_node(tree, path, node_str):
3742 else :
3843 current_node [to_set ] = json .loads (node_str )
3944
40- elif not path :
41- raise ValueError ("Cannot accept empty path" )
42-
4345 else :
4446 tree .clear ()
4547 tree .update (json .loads (node_str ))
@@ -50,7 +52,9 @@ def update_node(tree, path, node_str):
5052def pop_node (tree , path ):
5153 """Retrieve and delete json or value given a path"""
5254
53- if path and __not_root (path ):
55+ __check_path_not_empty (path )
56+
57+ if __not_root (path ):
5458 parent = path .split (_SEPARATOR )
5559 to_delete = parent .pop ()
5660 if parent :
@@ -60,9 +64,6 @@ def pop_node(tree, path):
6064
6165 return json .dumps (node .pop (to_delete ))
6266
63- elif not path :
64- raise ValueError ("Cannot accept empty path" )
65-
6667 else :
6768 node = json .dumps (tree )
6869 tree .clear ()
@@ -72,8 +73,10 @@ def pop_node(tree, path):
7273def cleanup_node (tree , path , target ):
7374 """Remove a node if not in target path and no child is found given a path"""
7475
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 ):
7780 parent = path .split (_SEPARATOR )
7881 to_delete = parent .pop ()
7982 if parent :
@@ -84,9 +87,6 @@ def cleanup_node(tree, path, target):
8487 if not node [to_delete ]:
8588 del node [to_delete ]
8689
87- elif not path :
88- raise ValueError ("Cannot accept empty path" )
89-
9090 else :
9191 if not tree :
9292 tree .clear ()
@@ -108,3 +108,8 @@ def __get_node(tree, node_list, create):
108108 current [last_node ] = {}
109109
110110 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