import os import json from src.address_book import AddressBook class Boss: def __init__(self): self.full_name = '' self.aliases = [] self.contact = None class Bosses: def __init__(self, data_folder): self.filename = os.path.join(data_folder, 'bosses.json') with open(self.filename, 'r') as f: data = json.load(f) self.bosses = [] addrs = AddressBook(data_folder) for b in data: boss = Boss() for k, val in b.items(): setattr(boss, k, val) boss.contact = addrs.find(boss.full_name) self.bosses.append(boss) def is_boss(self, name): for boss in self.bosses: if boss.full_name in name: return True for m in boss.contact.emails: if m in name: return True for a in boss.aliases: if a in name: return True return False def add_boss(self, name, aliases: list = None): if self.is_boss(name): return b = Boss() b.full_name = name if aliases is not None: b.aliases = aliases def get(self, name): if not self.is_boss(name): return None for i, boss in enumerate(self.bosses): if boss.full_name in name: return i for m in boss.contact.emails: if m in name: return i for a in boss.aliases: if a in name: return i def save(self): data = [{'full_name': boss.full_name, 'aliases': boss.aliases} for boss in self.bosses] with open(self.filename, 'w') as f: json.dump(data, f, indent=4)