import re
from bm_model import User, KANoUserException


def _get_cmd(text):
    text = re.sub(r"\,\s+", ",", text)
    text = re.sub(r"\s+", " ", text)
    return text.split()


def _get_username_by_id(session, id):
    user = session.query(User).filter(User.id == id).first()
    if user:
        return user.name
    else:
        return None


def _get_id_by_username(session, name):
    user = session.query(User).filter(User.name == name).first()
    if user:
        return user.id
    else:
        raise KANoUserException(name)
