import re


def ckan_legal_name(s: str) -> str:
    """
    CKAN names must be between 2 and 100 characters long and contain only lowercase alphanumeric characters,
    '-' and '_'. This function makes a reasonable best effort to convert an arbitrary input string. Strings
    below 2 characters long will raise a ValueError.

    Longer strings are converted to lower case, stripped of all non-alphanumeric, non underscore or dash, characters,
    then truncated to 100 characters if necessary.

    :param s:
        input string
    :return:
        valid CKAN name, as close as possible to the original
    :raises ValueError:
        if the string is too short
    """
    if s is None or len(s) < 2:
        raise ValueError(f'Unable to convert, input string is too short "{s}"')
    # Convert to lower case and remove all non-word characters except '_' and '-', then truncate to 100 chars
    return re.sub(r'[^\w_-]', '', s.lower())[:100]