From 7e30006623b723e7b0128b3144c5899fe963e763 Mon Sep 17 00:00:00 2001 From: Vladimir Domnich Date: Wed, 12 Oct 2022 12:22:56 +0400 Subject: [PATCH] Fix code formatting in json transformers Signed-off-by: Vladimir Domnich --- .../lib/python_keywords/json_transformers.py | 59 +++++++------------ 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/robot/resources/lib/python_keywords/json_transformers.py b/robot/resources/lib/python_keywords/json_transformers.py index c1554b3f..317383e1 100644 --- a/robot/resources/lib/python_keywords/json_transformers.py +++ b/robot/resources/lib/python_keywords/json_transformers.py @@ -1,5 +1,3 @@ -#!/usr/bin/python3.9 - """ When doing requests to NeoFS, we get JSON output as an automatically decoded structure from protobuf. Some fields are decoded with boilerplates and binary @@ -14,25 +12,24 @@ import base64 import base58 -def decode_simple_header(data: dict): +def decode_simple_header(data: dict) -> dict: """ This function reencodes Simple Object header and its attributes. """ try: data = decode_common_fields(data) - # object attributes view normalization - ugly_attrs = data["header"]["attributes"] - data["header"]["attributes"] = {} - for attr in ugly_attrs: - data["header"]["attributes"][attr["key"]] = attr["value"] + # Normalize object attributes + data["header"]["attributes"] = { + attr["key"]: attr["value"] for attr in data["header"]["attributes"] + } except Exception as exc: raise ValueError(f"failed to decode JSON output: {exc}") from exc return data -def decode_split_header(data: dict): +def decode_split_header(data: dict) -> dict: """ This function rearranges Complex Object header. The header holds SplitID, a random unique @@ -49,35 +46,25 @@ def decode_split_header(data: dict): return data -def decode_linking_object(data: dict): +def decode_linking_object(data: dict) -> dict: """ This function reencodes Linking Object header. It contains IDs of child Objects and Split Chain data. """ try: data = decode_simple_header(data) - # reencoding Child Object IDs - # { 'value': } -> - for ind, val in enumerate(data["header"]["split"]["children"]): - data["header"]["split"]["children"][ind] = json_reencode(val["value"]) - data["header"]["split"]["splitID"] = json_reencode(data["header"]["split"]["splitID"]) - data["header"]["split"]["previous"] = ( - json_reencode(data["header"]["split"]["previous"]["value"]) - if data["header"]["split"]["previous"] - else None - ) - data["header"]["split"]["parent"] = ( - json_reencode(data["header"]["split"]["parent"]["value"]) - if data["header"]["split"]["parent"] - else None - ) + split = data["header"]["split"] + split["children"] = [json_reencode(item["value"]) for item in split["children"]] + split["splitID"] = json_reencode(split["splitID"]) + split["previous"] = json_reencode(split["previous"]["value"]) if split["previous"] else None + split["parent"] = json_reencode(split["parent"]["value"]) if split["parent"] else None except Exception as exc: raise ValueError(f"failed to decode JSON output: {exc}") from exc return data -def decode_storage_group(data: dict): +def decode_storage_group(data: dict) -> dict: """ This function reencodes Storage Group header. """ @@ -89,7 +76,7 @@ def decode_storage_group(data: dict): return data -def decode_tombstone(data: dict): +def decode_tombstone(data: dict) -> dict: """ This function reencodes Tombstone header. """ @@ -101,29 +88,27 @@ def decode_tombstone(data: dict): return data -def decode_session_token(data: dict): +def decode_session_token(data: dict) -> dict: """ This function reencodes a fragment of header which contains information about session token. """ - data["body"]["object"]["target"]["container"] = json_reencode( - data["body"]["object"]["target"]["container"]["value"] - ) - data["body"]["object"]["target"]["objects"] = [json_reencode(obj["value"]) for obj in data["body"]["object"]["target"]["objects"]] + target = data["body"]["object"]["target"] + target["container"] = json_reencode(target["container"]["value"]) + target["objects"] = [json_reencode(obj["value"]) for obj in target["objects"]] return data -def json_reencode(data: str): +def json_reencode(data: str) -> str: """ According to JSON protocol, binary data (Object/Container/Storage Group IDs, etc) - is converted to string via Base58 encoder. But we usually operate with Base64-encoded - format. + is converted to string via Base58 encoder. But we usually operate with Base64-encoded format. This function reencodes given Base58 string into the Base64 one. """ return base58.b58encode(base64.b64decode(data)).decode("utf-8") -def encode_for_json(data: str): +def encode_for_json(data: str) -> str: """ This function encodes binary data for sending them as protobuf structures. @@ -131,7 +116,7 @@ def encode_for_json(data: str): return base64.b64encode(base58.b58decode(data)).decode("utf-8") -def decode_common_fields(data: dict): +def decode_common_fields(data: dict) -> dict: """ Despite of type (simple/complex Object, Storage Group, etc) every Object header contains several common fields.