import hashlib


def file_checksum(path_or_obj, checksum_type):
    """Get the checksum of the given file.

    Args:
      path_or_obj (str): File path to checksum OR an opened file object
      checksum_type (str): Supported values are 'md5', 'sha1', 'sha256'.
    Returns:
      str: Hexadecimal file checksum
    """
    # pylint: disable=redefined-variable-type
    if checksum_type == 'md5':
        hash_obj = hashlib.md5()
    elif checksum_type == 'sha1':
        hash_obj = hashlib.sha1()
    elif checksum_type == 'sha256':
        hash_obj = hashlib.sha256()
    else:
        raise NotImplementedError(
            "No support for generating checksum type {0}"
            .format(checksum_type))

    # Is it a file or do we need to open it?
    try:
        path_or_obj.read(0)
        file_obj = path_or_obj
    except AttributeError:
        file_obj = open(path_or_obj, 'rb')

    blocksize = 65536

    try:
        while True:
            buf = file_obj.read(blocksize)
            if len(buf) == 0:
                break
            hash_obj.update(buf)
    finally:
        if file_obj != path_or_obj:
            file_obj.close()

    return hash_obj.hexdigest()