Source code for jii_multispeq.measurement.checksum
"""Checksum handeling and calculation for MultispeQ Measurements."""importreimportzlibimportwarningsfromjii_multispeq.constantsimportREGEX_CHECKSUM
[docs]defget_crc32(data=None):""" Calculate a CRC32 checksum from data and represent as HEX. :param data: Data stream :type data: str :return: HEX CRC32 checksum :rtype: str :raises ValueError: if no data is provided :raises ValueError: if provided data is not a string """ifdataisNone:raiseValueError("Data not provided")ifnotisinstance(data,str):raiseValueError("Provided data needs to be a string")sum=zlib.crc32(data.encode('utf-8'))returnhex(sum)[2:].upper()
[docs]defstrip_crc32(data=None):""" Strip CRC32 checksum from data string and return data and checksum separately. :param data: Data stream :type data: str :return: data string and HEX CRC32 checksum :rtype: str, str :raises ValueError: if no data is provided :raises ValueError: if provided data is not a string """ifdataisNone:raiseValueError("Data not provided")ifnotisinstance(data,str):raiseValueError("Provided data needs to be a string")# Remove trailing spaces or line breaksdata=data.strip()# Check if there is an attached checksum prog=re.compile(REGEX_CHECKSUM,re.I)result=prog.search(data)ifresultisNone:returndata,Nonecrc32=data[-8:]data=data[:-8]ifcrc32!=get_crc32(data):warnings.warn("Checksum missmatch. There might has been an issue during data transfer.")returndata,crc32