typeddfs.utils.checksums
ο
Tools for shasum-like files.
Module Contentsο
- class typeddfs.utils.checksums.Checksumsο
- alg :strο
- calc_hash(self, path: typeddfs.utils._utils.PathLike) str ο
Calculates the hash of a file and returns it, hex-encoded.
- classmethod default_algorithm(cls) str ο
- delete_any(self, path: typeddfs.utils._utils.PathLike, *, rm_if_empty: bool = False) None ο
Deletes the filesum and removes
path
from the dirsum. Ignores missing files.
- generate_dirsum(self, directory: typeddfs.utils._utils.PathLike, glob: str = '*') typeddfs.utils.checksum_models.ChecksumMapping ο
Generates a new hash mapping, calculating hashes for extant files.
- Parameters
directory β Base directory
glob β Glob pattern under
directory
(cannot be recursive)
- Returns
A ChecksumMapping; use
.write
to write it
- get_dirsum_of_dir(self, path: typeddfs.utils._utils.PathLike) pathlib.Path ο
Returns the path required for the per-directory hash of
path
.Example
Utils.get_hash_file("my_dir") # Path("my_dir", "my_dir.sha256")
- get_dirsum_of_file(self, path: typeddfs.utils._utils.PathLike) pathlib.Path ο
Returns the path required for the per-directory hash of
path
.Example
Utils.get_hash_file(Path("my_dir, my_file.txt.gz")) # Path("my_dir", "my_dir.sha256")
- get_filesum_of_file(self, path: typeddfs.utils._utils.PathLike) pathlib.Path ο
Returns the path required for the per-file hash of
path
.Example
Utils.get_hash_file("my_file.txt.gz") # Path("my_file.txt.gz.sha256")
- classmethod guess_algorithm(cls, path: typeddfs.utils._utils.PathLike) str ο
Guesses the hashlib algorithm used from a hash file.
- Parameters
path β The hash file (e.g. my-file.sha256)
Example
Utils.guess_algorithm("my_file.sha1") # "sha1"
- load_dirsum_exact(self, path: typeddfs.utils._utils.PathLike, *, missing_ok: bool = True) typeddfs.utils.checksum_models.ChecksumMapping ο
- load_dirsum_of_dir(self, path: typeddfs.utils._utils.PathLike, *, missing_ok: bool = True) typeddfs.utils.checksum_models.ChecksumMapping ο
- load_dirsum_of_file(self, path: typeddfs.utils._utils.PathLike, *, missing_ok: bool = True) typeddfs.utils.checksum_models.ChecksumMapping ο
- load_filesum_exact(self, path: typeddfs.utils._utils.PathLike) typeddfs.utils.checksum_models.ChecksumFile ο
- load_filesum_of_file(self, path: typeddfs.utils._utils.PathLike) typeddfs.utils.checksum_models.ChecksumFile ο
- classmethod resolve_algorithm(cls, alg: str) str ο
Finds a hash algorithm by name in
hashlib
. Converts to lowercase and removes hyphens.- Raises
HashAlgorithmMissingError β If not found
- verify_any(self, path: typeddfs.utils._utils.PathLike, *, file_hash: bool, dir_hash: bool, computed: Optional[str]) Optional[str] ο
- verify_hex(self, path: typeddfs.utils._utils.PathLike, expected: str) Optional[str] ο
Verifies a hash directly from a hex string.
- write_any(self, path: typeddfs.utils._utils.PathLike, *, to_file: bool, to_dir: bool, overwrite: Optional[bool] = True) Optional[str] ο
Adds and/or appends the hex hash of
path
.- Parameters
path β Path to the file to hash
to_file β Whether to save a per-file hash
to_dir β Whether to save a per-dir hash
overwrite β If True, overwrite the file hash and any entry in the dir hash. If False, never overwrite either. If None, never overwrite, but ignore if equal to any existing entries.