f_lib.utils package

Utilities.

class f_lib.utils.FileHash[source]

Bases: object

Wrapper for hashlib to easily calculate file hashes.

DEFAULT_CHUNK_SIZE

Default chunk size if not defined.

Type:

ClassVar[int]

Note

Does not support algorithms with variable length digests (e.g. SHAKE).

__init__(hash_alg: hashlib._Hash, *, chunk_size: int = DEFAULT_CHUNK_SIZE) None[source]

Instantiate class.

Parameters:
  • hash_alg – Instance of a hashlib algorithm.

  • chunk_size – When reading a file, it will be read this many bytes at a time. Larger values are more time efficient while smaller values or more memory efficient.

add_file(file_path: StrPath) None[source]

Add file contents to the hash.

Parameters:

file_path – Path of the file to add.

add_file_name(file_path: StrPath, *, end_character: str = '\x00', relative_to: StrPath | None = None) None[source]

Add file name to the hash. This includes the path.

Parameters:
  • file_path – Path of the file to add. The full path (or relative) is included when adding it to the hash. This is not resolved prior to use. It is used as-is unless another argument acts up it.

  • end_character – Character that will be added to the end of the file_path. This can be an empty string.

  • relative_to – Optionally, convert the file_path to path relative to this one. It is recommended that both paths be absolute.

add_files(file_paths: Iterable[StrPath], *, relative_to: StrPath | None = None) None[source]

Add files to the hash.

Parameters:
  • file_paths – Paths of the files to add. The full path (or relative) is included when adding it to the hash. This is not resolved prior to use. It is used as-is unless another argument acts up it.

  • relative_to – Optionally, convert the file_path to path relative to this one. It is recommended that both paths be absolute.

property digest: bytes

Digest of the data hashed so far.

Returns:

This is a bytes object of size digest_size which may contain bytes in the whole range from 0 to 255.

property digest_size: int

Size of the resulting hash in bytes.

property hexdigest: str

Digest of the data hashed so far.

Returns:

String object that is double the length of digest and contains only hexadecimal digits.

f_lib.utils.convert_kwargs_to_shell_list(**kwargs: bool | Iterable[pathlib.Path] | Iterable[str] | str | None) list[str][source]

Convert kwargs to a list of shell arguments.

f_lib.utils.convert_list_to_shell_str(split_command: Iterable[str]) str[source]

Combine a list of strings into a string that can be run as a command.

Handles multi-platform differences.

f_lib.utils.convert_to_cli_flag(arg_name: str, *, prefix: str = '--') str[source]

Convert string kwarg name into a CLI flag.