diff --git a/soitool/compressor.py b/soitool/compressor.py index 985400edbd993de0865092370d2cc52e582ffce9..bd3e400d0ad39b718084cd6f9ef4c710d6272eaf 100644 --- a/soitool/compressor.py +++ b/soitool/compressor.py @@ -5,46 +5,62 @@ import json import codecs -def compress(soi): +def compress(obj): """ - Compress a soi using the lzma algorithm. + Compress a json compatible object using the lzma algorithm. + + JSON compapitle object are python types that are posible to convert to + JSON types. Se overview in this link: + https://docs.python.org/3/library/json.html#py-to-json-table Parameters ---------- - soi : dict - soi as dict to be compresesed + obj : json compatible object + Object to compress Returns ------- string - The compressed soi as a string + The compressed data casted from bytest to string """ - soi = json.dumps(soi) - soi = soi.encode(encoding="ascii") - return str(lzma.compress(soi)) + obj = json.dumps(obj) + obj = obj.encode(encoding="ascii") + return str(lzma.compress(obj)) -def decompress(file): +def decompress(lzma_compressed_json_string): """ - Decompress to python dict. + Decompress a lzma compressed json string. + + Decompresses a json compatible python object which is casted from bytes to + string. Parameters ---------- - file : string + lzma_compressed_json_string : string The string of the compressed object Returns ------- - dict - The decompressed file as dict + python object + One of these python objects: + https://docs.python.org/3/library/json.html#py-to-json-table """ # Removes extra b' notation - file = file[2:-1].encode(encoding="ascii") + lzma_compressed_json_string = lzma_compressed_json_string[2:-1].encode( + encoding="ascii" + ) # Remove double \\ - file = codecs.escape_decode(file, "hex") + lzma_compressed_json_string = codecs.escape_decode( + lzma_compressed_json_string, "hex" + ) # Decompress - file = lzma.decompress(file[0]) + lzma_compressed_json_string = lzma.decompress( + lzma_compressed_json_string[0] + ) # Cast to ascii string - file = file.decode(encoding="ascii") + lzma_compressed_json_string = lzma_compressed_json_string.decode( + encoding="ascii" + ) # Return as python datastructure - return json.loads(file) + return json.loads(lzma_compressed_json_string)