from pathlib import Path
import _hashlib


def get_hash(file: Path, first_chunk_only=False, hash_algo=_hashlib.sha1):
    """Returns a hash of the provided Path_Obj.
    Can return full hash or only first 1024 bytes of file.

    Args:
        file (Path_Obj): File to be hashed.
        first_chunk_only (bool, optional): Hash total file?. Defaults to False.
        hash_algo (Hash Function, optional): Hash routine to use. Defaults to _hashlib.sha1.

    Returns: Hash Value
    """
    def chunk_reader(fobj, chunk_size=1024):
        """ Generator that reads a file in chunks of bytes """
        while True:
            chunk = fobj.read(chunk_size)
            if not chunk:
                return
            yield chunk

    hashobj = hash_algo()
    with open(file, "rb") as f:
        if first_chunk_only:
            hashobj.update(f.read(1024))
        else:
            for chunk in chunk_reader(f):
                hashobj.update(chunk)
    return hashobj.digest()