habitat.utils.filtertools

Various utilities for filters to call upon.

Classes

UKHASChecksumFixer(protocol, data) A utility to help filters modify data that has been checksummed.
class habitat.utils.filtertools.UKHASChecksumFixer(protocol, data)[source]

A utility to help filters modify data that has been checksummed. It may be used as a context manager or via a class method.

For use as a context manager:

Specify the protocol in use with protocol and pass in the string being modified as data["data"], then use the return value as a dictionary whose data key you can modify as you desire. On exit, the checksum of that string is then updated if the original string’s checksum was valid.

If the original checksum was invalid, the original string is output instead.

>>> data = {"data": "$$hello,world*E408"}
>>> with UKHASChecksumFixer('crc16-ccitt', data) as fixer:
...     fixer["data"] = "$$hi,there,world*E408"
...
>>> fixer["data"]
'$$hi,there,world*39D3'

For direct calling as a class method:

Call UKHASChecksumFixer.fix(protocol, old_data, new_data). The function will either return new_data with a fixed checksum if the original checksum was valid, or it will return the old_data if the original checksum was invalid.

>>> UKHASChecksumFixer.fix('crc16-ccitt',
...                        "$$hello,world*E408", "$$hi,there,world*E408")
'$$hi,there,world*39D3'