Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue #5 #30

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 1 in src/xrdsum/checksums/_crc32c.py

View workflow job for this annotation

GitHub Actions / Format

Missing module docstring

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):

Check warning on line 14 in src/xrdsum/checksums/_crc32c.py

View workflow job for this annotation

GitHub Actions / Format

Missing class docstring
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
Loading