def delete_notif_agent(): notif_agents_dict = State.get_notif_agents() creator.print_title("Delete Notification Agent") changes_made = False while True: notif_agents_list = [] for n in notif_agents_dict: notif_agents_list.append(notif_agents_dict[n]) for i in range(len(notif_agents_list)): print(f"{i} - {notif_agents_list[i].name}") print("s - save and quit") print("q - quit without saving") tnum_str = creator.prompt_string("Delete notif_agent") if tnum_str == "s": State.save_notif_agents() State.save_tasks() break elif tnum_str == "q": if changes_made and creator.yes_no("Quit without saving", "n") == "y": return elif not changes_made: return if re.match("[0-9]+$", tnum_str): tnum = int(tnum_str) if tnum >= 0 and tnum < len(notif_agents_list): if creator.yes_no(f"Delete {notif_agents_list[tnum].name}", "y") == "y": used_by = get_tasks_using_notif_agent( notif_agents_dict[notif_agents_list[tnum].id]) if used_by is not None and len(used_by) > 0: task_names = [] for u in used_by: task_names.append(f"'{u.name}'") print( f"Cannot delete notification agent '{notif_agents_list[tnum].name}'. It is being used by: {','.join(task_names)}" ) print( "Delete tasks using this notification agent or remove this notification agent from those tasks first before deleting." ) else: do_delete_notif_agent(notif_agents_list[tnum].id) changes_made = True
def notif_agent_creator(notif_agent): from lib.core.state import State n = notif_agent cur_notif_agents = State.get_notif_agents() modules = State.get_notif_agent_modules() while True: n.name = creator.prompt_string("Name", default=n.name) n.module = create_notif_agent_choose_module(n.module) module = modules[n.module] props = module.get_properties() print("Module Properties") for p in props: default = None if n.module_properties is not None: default = n.module_properties.get(p, None) else: n.module_properties = {} while True: n.module_properties[p] = creator.prompt_string(f"{p}", default=default) is_valid, error_msg = module.is_property_valid( p, n.module_properties[p]) if is_valid: break else: print(error_msg) default = None print() print(f"Id: {n.id}") print(f"Name: {n.name}") print(f"Module: {n.module}") print("-----------------------------") for p in props: print(f"{p}: {n.module_properties[p]}") print("-----------------------------") while True: confirm = creator.prompt_options("Choose an option", ["save", "edit", "test", "quit"]) if confirm == "test": test_notif_agent(notif_agent) continue else: break if confirm == "save": break elif confirm == "edit": continue elif confirm == "quit": return cur_notif_agents[n.id] = notif_agent State.save_notif_agents()
def save(notif_agents): from lib.core.state import State State.save_notif_agents()