diff --git a/soitool/compressor.py b/soitool/compressor.py index 4152087983e42716f9290ce327b29371c9002387..2fd8ff258b989bc287e785794986cc663fcf3c74 100644 --- a/soitool/compressor.py +++ b/soitool/compressor.py @@ -1,48 +1,48 @@ import lzma import json +import codecs -def compress(file): +def compress(soi): """ - Compress a file using lzma. + Compress a soi using lzma. Parameters ---------- - file : string - The file to compress + soi : dict + soi as dict to be compresesed Returns ------- string - The compressed file + The compressed soi as a str """ - file = file.encode(encoding="ascii") - return str(lzma.compress(file)) + soi = json.dumps(soi) + soi = soi.encode(encoding="ascii") + return str(lzma.compress(soi)) -def decompress(file, return_obj=True): +def decompress(file): """ - Decompress to either object or string. + Decompress to python dict. Parameters ---------- file : string The string of the compressed object - return_obj : bool, optional - True if return as python object, by default True Returns ------- - python object or string - The decompressed file as object or string + dict + The decompressed file as dict """ # Removes extra b' notation file = file[2:-1].encode(encoding="ascii") + # Remove double // + file = codecs.escape_decode(file, "hex") + # Compresses file = lzma.decompress(file[0]) # Cast to ascii string file = file.decode(encoding="ascii") - if return_obj: - # Return as python object - return json.loads(file) - - return file + # Return as python datastructure + return json.loads(file)