typeddfs.utils.checksum_models

Models for shasum-like files.

Module Contents

class typeddfs.utils.checksum_models.ChecksumFile
delete(self) None

Deletes the hash file by calling pathlib.Path.unlink(self.hash_path).

Raises

OSError – Accordingly

property file_path(self) pathlib.Path
property hash_value(self) str
load(self) __qualname__

Returns a copy of self read from hash_path.

classmethod new(cls, hash_path: typeddfs.utils._utils.PathLike, file_path: typeddfs.utils._utils.PathLike, hash_value: str) ChecksumFile

Use this as a constructor.

classmethod parse(cls, path: pathlib.Path, *, lines: Optional[Sequence[str]] = None) __qualname__

Reads hash file contents.

Parameters
  • path – The path of the checksum file; required to resolve paths relative to its parent

  • lines – The lines in the checksum file; reads path if None

Returns

A ChecksumFile

rename(self, path: pathlib.Path) __qualname__

Replaces self.file_path with path. This will affect the filename written in a .shasum-like file. No OS operations are performed.

update(self, value: str, overwrite: Optional[bool] = True) __qualname__

Modifies the hash.

Parameters
  • value – The new hex-encoded hash

  • overwrite – If None, requires that the value is the same as before (no operation is performed). If False, this method will always raise an error.

verify(self, computed: str) None

Verifies the checksum.

Parameters

computed – A pre-computed hex-encoded hash

Raises

HashDidNotValidateError – If the hashes are not equal

write(self) None

Writes the hash file.

Raises

OsError – Accordingly

class typeddfs.utils.checksum_models.ChecksumMapping
__add__(self, other: Union[ChecksumMapping, Mapping[typeddfs.utils._utils.PathLike, str], __qualname__]) __qualname__

Performs a symmetric addition.

Raises

ValueError – If other intersects (shares keys) with self

See also

append()

__contains__(self, path: pathlib.Path) bool
__getitem__(self, path: pathlib.Path) str
__len__(self) int
__sub__(self, other: Union[typeddfs.utils._utils.PathLike, Iterable[typeddfs.utils._utils.PathLike], ChecksumMapping]) __qualname__

Removes entries.

See also

remove()

append(self, append: Mapping[typeddfs.utils._utils.PathLike, str], *, overwrite: Optional[bool] = False) __qualname__

Append paths to a dir hash file. Like update() but less flexible and only for adding paths.

property entries(self) Mapping[pathlib.Path, str]
get(self, key: pathlib.Path, default: Optional[str] = None) Optional[str]
items(self) AbstractSet[Tuple[pathlib.Path, str]]
keys(self) AbstractSet[pathlib.Path]
load(self, missing_ok: bool = False) __qualname__

Replaces this map with one read from the hash file.

Parameters

missing_ok – If the hash path does not exist, treat it has having no items

classmethod new(cls, hash_path: typeddfs.utils._utils.PathLike, dct: Mapping[typeddfs.utils._utils.PathLike, str]) ChecksumMapping

Use this as the constructor.

classmethod parse(cls, path: pathlib.Path, *, lines: Optional[Sequence[str]] = None, missing_ok: bool = False, subdirs: bool = False) __qualname__

Reads hash file contents.

Parameters
  • path – The path of the checksum file; required to resolve paths relative to its parent

  • lines – The lines in the checksum file; reads path if None

  • missing_ok – If path does not exist, assume it contains no items

  • subdirs – Permit files within subdirectories specified with / Most tools do not support these.

Returns

A mapping from raw string filenames to their hex hashes. Any node called ./ in the path is stripped.

remove(self, remove: Union[typeddfs.utils._utils.PathLike, Iterable[typeddfs.utils._utils.PathLike]], *, missing_ok: bool = False) __qualname__

Strips paths from this hash collection. Like update() but less flexible and only for removing paths.

Raises

typeddfs.df_errors.PathNotRelativeError – To avoid, try calling resolve first

update(self, update: Union[Callable[[pathlib.Path], Optional[typeddfs.utils._utils.PathLike]], Mapping[typeddfs.utils._utils.PathLike, Optional[typeddfs.utils._utils.PathLike]]], *, missing_ok: bool = True, overwrite: Optional[bool] = True) __qualname__

Returns updated hashes from a dir hash file.

Parameters
  • update – Values to overwrite. May be a function or a dictionary from paths to values. If None is returned, the entry will be removed; otherwise, updates with the returned hex hash.

  • missing_ok – Require that the path is already listed

  • overwrite – Allow overwriting an existing value. If None, only allow if the hash is the same.

values(self) ValuesView[str]
verify(self, path: typeddfs.utils._utils.PathLike, computed: str, *, resolve: bool = False, exist: bool = False) None

Verifies a checksum. The file path must be listed.

Parameters
  • path – The file to look for

  • computed – A pre-computed hex-encoded hash; if set, do not calculate from path

  • resolve – Resolve paths before comparison

  • exist – Require that path exists

Raises
  • FileNotFoundError – If path does not exist

  • HashFileMissingError – If the hash file does not exist

  • HashDidNotValidateError – If the hashes are not equal

  • HashVerificationError` – Superclass of HashDidNotValidateError if the filename is not listed, etc.

write(self, *, sort: Union[bool, Callable[[Sequence[pathlib.Path]], Sequence[pathlib.Path]]] = False, rm_if_empty: bool = False) None

Writes to the hash (.shasum-like) file.

Parameters
  • sort – Sort with this function, or sorted if True

  • rm_if_empty – Delete with pathlib.Path.unlink if this contains no items

Raises

OSError – Accordingly