import yaml
from yaml.loader import SafeLoader

with open('./server/server.yml') as f:
    SERVER = yaml.load(f, Loader=SafeLoader)


def get_server(email):
    for server in SERVER.keys():
        if email in SERVER[server]["black_listed_email_addresses"]:
            continue
        if (email.split('@')[-1]) in [i["email"] for i in SERVER[server]["mail_server"]]:
            return server
        if email in SERVER[server]["white_listed_email_addresses"]:
            return server
    raise NoServerAssociatedError


def get_servers_list():
    return {key: {k: SERVER[key][k] for k in SERVER[key] if k=="mail_server"} for key in SERVER if key != 'Test_Server'}


class NoServerAssociatedError(Exception):
    pass
