class Admin(AdminBase): FILE = os.path.join(os.path.dirname(__file__), "../modules/resources/admin.txt") def __init__(self): self.lines = FileList(self.FILE) def is_admin(self, user): return self.__get_id(user) in self.lines def add(self, current_user, user): if not self.is_admin(current_user): raise NotAdminException() self.lines.add(self.__get_id(user)) def remove(self, current_user, user): if not self.is_admin(current_user): raise NotAdminException() self.lines.remove(self.__get_id(user)) @classmethod def __get_id(cls, user): if isinstance(user, int): return str(user) if isinstance(user, str): return user elif isinstance(user, dict): return cls.__get_id(user["id"]) else: print("%s is not valid user %s" % (user, type(user)))
class Bad: def __init__(self, path): self.lines = FileList(path) def bad(self, text): bads = [] for word in re.findall("\w{2,}", text.lower(), re.UNICODE): if word in self.lines: bads.append(word) return bads @classmethod def __invalid(cls, word): return " " in word or len(word) <= 2 def add(self, word): if self.__invalid(word): return word = word.lower() if word not in self.lines: self.lines.add(word) def remove(self, word): if self.__invalid(word): return word = word.lower() if word in self.lines: self.lines.remove(word)
def test_add_remove(self): word = str(uuid.uuid4()).replace("-", "") print(word) list = FileList(self.FILE) n = len(list) self.assertFalse(word in list) list.add(word) self.assertTrue(word in list) self.assertEqual(len(list), n + 1) list.remove(word) self.assertFalse(word in list) self.assertEqual(len(list), n)