-
Notifications
You must be signed in to change notification settings - Fork 3
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 #74 from E-ARK-Software/feat/validating-files
Validation of file entries
- Loading branch information
Showing
13 changed files
with
2,561 additions
and
2,422 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: utf-8 -*- | ||
from pathlib import Path | ||
|
||
from eark_validator.const import NO_PATH, NOT_FILE | ||
from eark_validator.model import Checksum, ChecksumAlg | ||
|
||
class Checksummer: | ||
def __init__(self, algorithm: ChecksumAlg | str): | ||
if isinstance(algorithm, ChecksumAlg): | ||
self._algorithm: ChecksumAlg = algorithm | ||
else: | ||
self._algorithm: ChecksumAlg = ChecksumAlg.from_string(algorithm) | ||
|
||
@property | ||
def algorithm(self) -> ChecksumAlg: | ||
"""Return the checksum algorithm used by this checksummer.""" | ||
return self._algorithm | ||
|
||
def hash_file(self, path: Path) -> 'Checksum': | ||
"""Calculate the checksum of a file. | ||
Args: | ||
path (Path): A path to a file to checksum. | ||
Raises: | ||
FileNotFoundError: If the path parameter is found. | ||
ValueError: If the path parameter resolves to a directory. | ||
Returns: | ||
Checksum: A Checksum object containing the Hexadecimal digest of the file. | ||
""" | ||
if not path.exists(): | ||
raise FileNotFoundError(NO_PATH.format(path)) | ||
if not path.is_file(): | ||
raise ValueError(NOT_FILE.format(path)) | ||
implemenation: ChecksumAlg = ChecksumAlg.get_implementation(self._algorithm) | ||
with open(path, 'rb') as file: | ||
for chunk in iter(lambda: file.read(4096), b''): | ||
implemenation.update(chunk) | ||
return Checksum.model_validate({ | ||
'algorithm': self._algorithm, | ||
'value': implemenation.hexdigest() | ||
}, strict=True | ||
) | ||
|
||
@classmethod | ||
def from_file(cls, path: Path, algorithm: 'ChecksumAlg') -> 'Checksum': | ||
"""Create a Checksum from an etree element.""" | ||
# Get the child flocat element and grab the href attribute. | ||
return Checksummer(algorithm).hash_file(path) |
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
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
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
Oops, something went wrong.