import grp
import pwd


def string_to_uidgid(user_and_group):
    """ Translate the user_and_group string to uid and gid.
        The userandgroup parameter must be a string of the format '<user>[:[<group>]]'
        or '[<user>]:gid'
        User and group can be strings or integers. If no group is given, -1 will be
        returned for gid.
        :raises ValueError: if user or group could not be resolved
    """
    params = user_and_group.split(':')
    if len(params) > 2:
        raise ValueError(f"User and group {user_and_group} are in wrong format. Expected <user>[:[<group>]] or [<user>]:group")

    gid = None
    if len(params) == 2 and len(params[1]) > 0:
        if params[1].isnumeric():
            gid = int(params[1])
            if gid < 0:
                raise ValueError(f"User and group {user_and_group} contains an illegal value")
        else:
            try:
                gr = grp.getgrnam(params[1])
                gid = gr.gr_gid
            except KeyError as e:
                raise ValueError(f'Could not resolve group {params[1]}: {e}') from e

    uid = None
    if len(params[0]) > 0:
        if params[0].isnumeric():
            uid = int(params[0])
            if uid < 0:
                raise ValueError(f"User and group {user_and_group} contains an illegal value")
        else:
            try:
                passwd = pwd.getpwnam(params[0])
                uid = passwd.pw_uid
            except KeyError as e:
                raise ValueError(f'Could not resolve user {params[0]}: {e}') from e

    if uid is None and gid is None:
        raise ValueError(f"User and group {user_and_group} are in wrong format. Expected <user>[:[<group>]] or [<user>]:group")

    return uid, gid