Skip to content

Commit

Permalink
Merge pull request #30 from BristolComputing/kreczko-issue-5-p2
Browse files Browse the repository at this point in the history
Fixes issue #5
  • Loading branch information
kreczko authored Feb 19, 2025
2 parents 5dbb6db + 02c0357 commit 43a6ffc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ repos:
args: []
additional_dependencies:
- codetiming
- crc32c
- fasthep_logging
- pyhdfs
- pytest
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dynamic = ["version"]

dependencies = [
"codetiming",
"crc32c",
"fasthep-logging >= 0.1.1",
"typer >= 0.4.0",
"requests",
Expand Down
5 changes: 4 additions & 1 deletion src/xrdsum/checksums/__init__.py
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"]
15 changes: 5 additions & 10 deletions src/xrdsum/checksums/_base.py
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()
37 changes: 37 additions & 0 deletions src/xrdsum/checksums/_crc32c.py
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

0 comments on commit 43a6ffc

Please sign in to comment.