#!/usr/bin/env python3 # -*- coding: utf-8 -*- from typing import Self, Any from Interfaces.Application.AnPInterface import AnPInterface from Utils.Common import Common class UniqueKeysManager: def __init__(self:Self, anp:AnPInterface) -> None: self.anp:AnPInterface = anp self.__keys:list[str] = [] self.__alphabet:list[str] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" self.__length:int = 13 self.update() def update(self:Self) -> None: self.__alphabet = Common.unique(self.anp.settings.get(("unique_keys_alphabet", "alphabet"), None, self.__alphabet)) self.__length = self.anp.settings.get(("unique_keys_length", "length"), None, self.__length) def reset(self:Self) -> None: self.__keys = [] self.__alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" self.__length = 13 self.update() def get(self:Self) -> str: key:str while True: key = "" while len(key) < self.__length: key += Common.random(self.__alphabet) if key[0] not in "0123456789" and key not in self.__keys: self.__keys.append(key) return key def remove(self:Self, key:str) -> None: if key in self.__keys: self.__keys.remove(key)