from typing import Union
from typing import Tuple


def parse_deformation_potential(
    deformation_pot_str: str,
) -> Union[str, float, Tuple[float, ...]]:
    """Parse deformation potential string.

    Args:
        deformation_pot_str: The deformation potential string. Can be a path to a
            deformation.h5 file, a single deformation potential to use for all bands
            or two deformation potentials separated by a comma for valence and
            conduction bands.

    Returns:
        The deformation potential(s) or path to deformation potential file.
    """

    if "h5" in deformation_pot_str:
        return deformation_pot_str

    deformation_pot_str = deformation_pot_str.strip().replace(" ", "")

    try:
        parts = list(map(float, deformation_pot_str.split(",")))

        if len(parts) == 1:
            return parts[0]
        elif len(parts) == 2:
            return tuple(parts)
        else:
            raise ValueError

    except ValueError:
        raise ValueError(
            "ERROR: Unrecognised deformation potential format: "
            "{}".format(deformation_pot_str)
        )