Skip to content

Commit

Permalink
Merge pull request #7 from Mastercard/feature/reduce_complexity
Browse files Browse the repository at this point in the history
Reduce code complexity
  • Loading branch information
pqlMC authored Jul 3, 2019
2 parents ad2ec7d + 7137773 commit e269074
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions client_encryption/json_path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@
def __not_root(path): return path != _ROOT_SYMBOL


def __target_in_path(path, target): return target and target.startswith(path)


def get_node(tree, path, create=False):
"""Retrieve json or value given a path"""

if not path:
raise ValueError("Cannot accept empty path")

current = tree
if path and __not_root(path):
if __not_root(path):
current = __get_node(tree, path.split(_SEPARATOR), create)

elif not path:
raise ValueError("Cannot accept empty path")

return current


def update_node(tree, path, node_str):
"""Update node with json or value in string format given a path"""

if path and __not_root(path):
__check_path_not_empty(path)

if __not_root(path):
parent = path.split(_SEPARATOR)
to_set = parent.pop()
if parent:
Expand All @@ -37,9 +42,6 @@ def update_node(tree, path, node_str):
else:
current_node[to_set] = json.loads(node_str)

elif not path:
raise ValueError("Cannot accept empty path")

else:
tree.clear()
tree.update(json.loads(node_str))
Expand All @@ -50,7 +52,9 @@ def update_node(tree, path, node_str):
def pop_node(tree, path):
"""Retrieve and delete json or value given a path"""

if path and __not_root(path):
__check_path_not_empty(path)

if __not_root(path):
parent = path.split(_SEPARATOR)
to_delete = parent.pop()
if parent:
Expand All @@ -60,9 +64,6 @@ def pop_node(tree, path):

return json.dumps(node.pop(to_delete))

elif not path:
raise ValueError("Cannot accept empty path")

else:
node = json.dumps(tree)
tree.clear()
Expand All @@ -72,8 +73,10 @@ def pop_node(tree, path):
def cleanup_node(tree, path, target):
"""Remove a node if not in target path and no child is found given a path"""

if path and __not_root(path):
if not (target and target.startswith(path)):
__check_path_not_empty(path)

if __not_root(path):
if not __target_in_path(path, target):
parent = path.split(_SEPARATOR)
to_delete = parent.pop()
if parent:
Expand All @@ -84,9 +87,6 @@ def cleanup_node(tree, path, target):
if not node[to_delete]:
del node[to_delete]

elif not path:
raise ValueError("Cannot accept empty path")

else:
if not tree:
tree.clear()
Expand All @@ -108,3 +108,8 @@ def __get_node(tree, node_list, create):
current[last_node] = {}

return current[last_node]


def __check_path_not_empty(path):
if not path:
raise ValueError("Cannot accept empty path")

0 comments on commit e269074

Please sign in to comment.