from pint import Unit

from timeseriesx import ureg


def coerce_unit(unit):
    """
    returns the convenient representation of a unit as a `pint.Unit`-object

    :param str/pint.Unit unit: the unit string to parse or a Unit object
    :return: the coerced unit
    :rtype pint.Unit
    """
    if isinstance(unit, str):
        unit = ureg.parse_units(unit)
    if isinstance(unit, Unit):
        return unit
    else:
        return ValueError(f'{unit} is no valid unit')


def unit_is_valid(unit, exception=True):
    """
    validate a unit object or a unit string

    :param str/pint.Unit unit: the
    :param exception: if True, raise an execption when validation fails
    :return: True, if the validation passes
    :rtype: bool
    """
    try:
        coerce_unit(unit)
    except ValueError as e:
        if exception:
            raise e
        else:
            return False
    return True
