Fix code formatting in json transformers

Signed-off-by: Vladimir Domnich <v.domnich@yadro.com>
This commit is contained in:
Vladimir Domnich 2022-10-12 12:22:56 +04:00 committed by Vladimir
parent 3eadf934e0
commit 7e30006623

View file

@ -1,5 +1,3 @@
#!/usr/bin/python3.9
""" """
When doing requests to NeoFS, we get JSON output as an automatically decoded When doing requests to NeoFS, we get JSON output as an automatically decoded
structure from protobuf. Some fields are decoded with boilerplates and binary structure from protobuf. Some fields are decoded with boilerplates and binary
@ -14,25 +12,24 @@ import base64
import base58 import base58
def decode_simple_header(data: dict): def decode_simple_header(data: dict) -> dict:
""" """
This function reencodes Simple Object header and its attributes. This function reencodes Simple Object header and its attributes.
""" """
try: try:
data = decode_common_fields(data) data = decode_common_fields(data)
# object attributes view normalization # Normalize object attributes
ugly_attrs = data["header"]["attributes"] data["header"]["attributes"] = {
data["header"]["attributes"] = {} attr["key"]: attr["value"] for attr in data["header"]["attributes"]
for attr in ugly_attrs: }
data["header"]["attributes"][attr["key"]] = attr["value"]
except Exception as exc: except Exception as exc:
raise ValueError(f"failed to decode JSON output: {exc}") from exc raise ValueError(f"failed to decode JSON output: {exc}") from exc
return data return data
def decode_split_header(data: dict): def decode_split_header(data: dict) -> dict:
""" """
This function rearranges Complex Object header. This function rearranges Complex Object header.
The header holds SplitID, a random unique The header holds SplitID, a random unique
@ -49,35 +46,25 @@ def decode_split_header(data: dict):
return data return data
def decode_linking_object(data: dict): def decode_linking_object(data: dict) -> dict:
""" """
This function reencodes Linking Object header. This function reencodes Linking Object header.
It contains IDs of child Objects and Split Chain data. It contains IDs of child Objects and Split Chain data.
""" """
try: try:
data = decode_simple_header(data) data = decode_simple_header(data)
# reencoding Child Object IDs split = data["header"]["split"]
# { 'value': <Base58 encoded OID> } -> <Base64 encoded OID> split["children"] = [json_reencode(item["value"]) for item in split["children"]]
for ind, val in enumerate(data["header"]["split"]["children"]): split["splitID"] = json_reencode(split["splitID"])
data["header"]["split"]["children"][ind] = json_reencode(val["value"]) split["previous"] = json_reencode(split["previous"]["value"]) if split["previous"] else None
data["header"]["split"]["splitID"] = json_reencode(data["header"]["split"]["splitID"]) split["parent"] = json_reencode(split["parent"]["value"]) if split["parent"] else None
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
)
except Exception as exc: except Exception as exc:
raise ValueError(f"failed to decode JSON output: {exc}") from exc raise ValueError(f"failed to decode JSON output: {exc}") from exc
return data return data
def decode_storage_group(data: dict): def decode_storage_group(data: dict) -> dict:
""" """
This function reencodes Storage Group header. This function reencodes Storage Group header.
""" """
@ -89,7 +76,7 @@ def decode_storage_group(data: dict):
return data return data
def decode_tombstone(data: dict): def decode_tombstone(data: dict) -> dict:
""" """
This function reencodes Tombstone header. This function reencodes Tombstone header.
""" """
@ -101,29 +88,27 @@ def decode_tombstone(data: dict):
return data return data
def decode_session_token(data: dict): def decode_session_token(data: dict) -> dict:
""" """
This function reencodes a fragment of header which contains This function reencodes a fragment of header which contains
information about session token. information about session token.
""" """
data["body"]["object"]["target"]["container"] = json_reencode( target = data["body"]["object"]["target"]
data["body"]["object"]["target"]["container"]["value"] target["container"] = json_reencode(target["container"]["value"])
) target["objects"] = [json_reencode(obj["value"]) for obj in target["objects"]]
data["body"]["object"]["target"]["objects"] = [json_reencode(obj["value"]) for obj in data["body"]["object"]["target"]["objects"]]
return data 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) 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 is converted to string via Base58 encoder. But we usually operate with Base64-encoded format.
format.
This function reencodes given Base58 string into the Base64 one. This function reencodes given Base58 string into the Base64 one.
""" """
return base58.b58encode(base64.b64decode(data)).decode("utf-8") 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 This function encodes binary data for sending them as protobuf
structures. structures.
@ -131,7 +116,7 @@ def encode_for_json(data: str):
return base64.b64encode(base58.b58decode(data)).decode("utf-8") 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 Despite of type (simple/complex Object, Storage Group, etc) every Object
header contains several common fields. header contains several common fields.