from .user import user from .user import user_builder from .user import user_factory from .exception import in_collection_exception class users_collection: """ This is responsible for managing users in the system. It could login new users, or load them by ApiKey. """ def __init__(self): """ This create new collection. """ self.__by_apikey = dict() self.__by_nick = dict() def add(self, target: user) -> None: """ This add new user to the collection. When ApiKey or login already exists in the collection, then error had been raised. Parameters: target (user): New user to add """ if target.apikey in self.__by_apikey: error = "User with ApiKey \"" + target.apikey + "\" " error = error + "already in collection." raise in_collection_exception(error) if target.nick in self.__by_nick: error = "User with nick \"" + target.nick + "\" " error = error + "already in collection." raise in_collection_exception(error) self.__by_apikey[target.apikey] = target self.__by_nick[target.nick] = target def login(self, nick: str, password: str) -> user | None: """ This try to login user by nick and password. When nick or password had been wrong, then None had been returned. When nick and password are correct, then return that user. Parameters: nick (str): Nick of the user password (str): Password of the user Returns: (user): User with that nick and password, if exists (None): When user with that nick and password not exists """ if not nick in self.__by_nick: return None target = self.__by_nick[nick] if target.password != password: return None return target def get(self, apikey: str) -> user | None: """ This try to load user by ApiKey. When user with that ApiKey exists then return it. When not, return None. Parameters: apikey (str): ApiKey of the user to load Returns: (user): User with that ApiKey (None): When user with that ApiKey not exists """ if not apikey in self.__by_apikey: return None return self.__by_apikey[apikey]