-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from BristolComputing/kreczko-issue-5-p2
Fixes issue #5
- Loading branch information
Showing
5 changed files
with
48 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ repos: | |
args: [] | ||
additional_dependencies: | ||
- codetiming | ||
- crc32c | ||
- fasthep_logging | ||
- pyhdfs | ||
- pytest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
"""xrdsum.checksums package""" | ||
|
||
from __future__ import annotations | ||
|
||
from ._base import Checksum | ||
from ._crc32c import CRC32C | ||
from .adler32 import Adler32 | ||
|
||
AVAILABLE_CHECKSUM_TYPES = { | ||
"adler32": Adler32, | ||
"crc32c": CRC32C, | ||
} | ||
__all__ = ["Adler32", "Checksum", "AVAILABLE_CHECKSUM_TYPES"] | ||
__all__ = ["AVAILABLE_CHECKSUM_TYPES", "CRC32C", "Adler32", "Checksum"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,21 @@ | ||
"""Definition of the Checksum protocol.""" | ||
|
||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from collections.abc import Iterable | ||
from typing import Any, Protocol | ||
from typing import Any | ||
|
||
|
||
class Checksum(Protocol): | ||
class Checksum(ABC): | ||
"""Base protocol for checksum implementations.""" | ||
|
||
name: str = "Unknown" | ||
value: str = "N/A" | ||
bytes_read: int = 0 | ||
number_of_buffers_read: int = 0 | ||
|
||
def int_to_hex(self, value: int) -> str: | ||
"""Converts integer to hex representation""" | ||
raise NotImplementedError() | ||
|
||
def hex_to_int(self, value: str) -> int: | ||
"""Converts hex representation to integer""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def calculate(self, file_buffer: Iterable[Any]) -> str: | ||
"""Calculates the checksum""" | ||
raise NotImplementedError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterable | ||
from typing import Any | ||
|
||
import crc32c | ||
|
||
from ..logger import APP_LOGGER_NAME, get_logger | ||
from ._base import Checksum | ||
|
||
log = get_logger(APP_LOGGER_NAME) | ||
|
||
|
||
class CRC32C(Checksum): | ||
name: str = "crc32c" | ||
|
||
def calculate(self, file_buffer: Iterable[Any]) -> str: | ||
value = crc32c.CRC32CHash() | ||
bytes_read = 0 | ||
number_of_buffers_read = 0 | ||
for buffer in file_buffer: | ||
value.update(buffer) | ||
bytes_read += len(buffer) | ||
number_of_buffers_read += 1 | ||
log.trace( | ||
"%s: %s %s %s", | ||
self.name, | ||
value.hexdigest(), | ||
len(buffer), | ||
bytes_read, | ||
) | ||
|
||
self.value = value.hexdigest() | ||
self.bytes_read = bytes_read | ||
self.number_of_buffers_read = number_of_buffers_read | ||
|
||
return self.value |