Exemple #1
0
def write_file(filename: str, accounts_dict: dict):
    """ Compresses and writes the accounts_dict to the file at filename.

    """

    json_data = json_dumps(accounts_dict)

    lzma_data = lzma_compress(json_data.encode())

    with open(filename, 'wb') as pass_file:
        pass_file.write(lzma_data)
Exemple #2
0
def write_file(filename: str, accounts_dict: dict, encrypted_key: bytes):
    """ Compresses and writes the accounts_dict to the file at filename.

    """

    # Put the master key into the accounts dict.
    accounts_dict[MASTER_KEY_DIGEST] = encrypted_key.hex()

    json_data = json_dumps(accounts_dict)

    lzma_data = lzma_compress(json_data.encode())

    with pathlib_path(filename) as pass_file:
        pass_file.write_bytes(lzma_data)
def compress_test(data: bytes,
                  output: bool = True) -> Dict[str, Tuple[int, int, int]]:
    """
    Compare compress modules.
    :param data: the data to compress.
    :param output: if this value is True, print the results to console.
    :return: {'module name': (<size of the compressed data>, <time to compress>, <time to decompress>)}
    """
    res: Dict[str, Tuple[int, int, int]] = {}
    try_print('+++++++++++++++++++++++++++++++++++++++++++++++++++++',
              flag=output)
    size = len(data)
    try_print(f'Original size: {round(size/1024/1024), 4} MB', flag=output)
    # gzip
    for i in range(10):
        tmp = gzip_compress(data, compresslevel=i)
        key = f'gzip(compress level {i})'
        res[key] = (len(tmp),
                    check_function_speed(gzip_compress, data, compresslevel=i),
                    check_function_speed(gzip_decompress, tmp))
        __print(res, size, key, output)
    # bz2
    for i in range(1, 10):
        tmp = bz2_compress(data, compresslevel=i)
        key = f'bz2(compress level {i})'
        res[key] = (len(tmp),
                    check_function_speed(bz2_compress, data, compresslevel=i),
                    check_function_speed(bz2_decompress, tmp))
        __print(res, size, key, output)
    # zlib
    for i in range(10):
        tmp = zlib_compress(data, level=i)
        key = f'zlib(compress level {i})'
        res[key] = (len(tmp), check_function_speed(zlib_compress,
                                                   data,
                                                   level=i),
                    check_function_speed(zlib_decompress, tmp))
        __print(res, size, key, output)
    # lzma
    tmp = lzma_compress(data, FORMAT_XZ, CHECK_CRC64)
    res[f'lzma(XZ - CRC64)'] = (len(tmp),
                                check_function_speed(lzma_compress, data,
                                                     FORMAT_XZ, CHECK_CRC64),
                                check_function_speed(lzma_decompress,
                                                     tmp,
                                                     format=FORMAT_XZ))
    __print(res, size, f'lzma(XZ - CRC64)', output)
    tmp = lzma_compress(data, FORMAT_XZ, CHECK_CRC32)
    res[f'lzma(XZ - CRC32)'] = (len(tmp),
                                check_function_speed(lzma_compress, data,
                                                     FORMAT_XZ, CHECK_CRC32),
                                check_function_speed(lzma_decompress,
                                                     tmp,
                                                     format=FORMAT_XZ))
    __print(res, size, f'lzma(XZ - CRC32)', output)
    tmp = lzma_compress(data, FORMAT_XZ, CHECK_NONE)
    res[f'lzma(XZ - NONE)'] = (len(tmp),
                               check_function_speed(lzma_compress, data,
                                                    FORMAT_XZ, CHECK_NONE),
                               check_function_speed(lzma_decompress,
                                                    tmp,
                                                    format=FORMAT_XZ))
    __print(res, size, f'lzma(XZ - NONE)', output)
    tmp = lzma_compress(data, FORMAT_ALONE, CHECK_NONE)
    res[f'lzma(ALONE - NONE)'] = (len(tmp),
                                  check_function_speed(lzma_compress, data,
                                                       FORMAT_ALONE,
                                                       CHECK_NONE),
                                  check_function_speed(lzma_decompress,
                                                       tmp,
                                                       format=FORMAT_ALONE))
    __print(res, size, f'lzma(ALONE - NONE)', output)
    # brotli
    tmp = brotli_compress(data)
    key = 'brotli'
    res[key] = (len(tmp), check_function_speed(brotli_compress, data),
                check_function_speed(brotli_decompress, tmp))
    __print(res, size, key, output)
    try_print('+++++++++++++++++++++++++++++++++++++++++++++++++++++',
              flag=output)
    return res